Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
| 17 | /* |
| 18 | * Step 1 of support for weak-linking a number of symbols existing on |
| 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
| 31 | #include "structseq.h" |
| 32 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 42 | "This module provides access to operating system functionality that is\n\ |
| 43 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 44 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #if defined(PYOS_OS2) |
| 49 | #define INCL_DOS |
| 50 | #define INCL_DOSERRORS |
| 51 | #define INCL_DOSPROCESS |
| 52 | #define INCL_NOPMAPI |
| 53 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 54 | #if defined(PYCC_GCC) |
| 55 | #include <ctype.h> |
| 56 | #include <io.h> |
| 57 | #include <stdio.h> |
| 58 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 59 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 60 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | #endif /* HAVE_SYS_TYPES_H */ |
| 66 | |
| 67 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYS_WAIT_H |
| 72 | #include <sys/wait.h> /* For WNOHANG */ |
| 73 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 76 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | #ifdef HAVE_FCNTL_H |
| 80 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 81 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 83 | #ifdef HAVE_GRP_H |
| 84 | #include <grp.h> |
| 85 | #endif |
| 86 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 87 | #ifdef HAVE_SYSEXITS_H |
| 88 | #include <sysexits.h> |
| 89 | #endif /* HAVE_SYSEXITS_H */ |
| 90 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYS_LOADAVG_H |
| 92 | #include <sys/loadavg.h> |
| 93 | #endif |
| 94 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 95 | #ifdef HAVE_LANGINFO_H |
| 96 | #include <langinfo.h> |
| 97 | #endif |
| 98 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 100 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #include <process.h> |
| 103 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 104 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
| 106 | #define HAVE_OPENDIR 1 |
| 107 | #define HAVE_SYSTEM 1 |
| 108 | #if defined(__OS2__) |
| 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 111 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | #include <process.h> |
| 113 | #else |
| 114 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 115 | #define HAVE_EXECV 1 |
| 116 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #define HAVE_OPENDIR 1 |
| 118 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 119 | #define HAVE_SYSTEM 1 |
| 120 | #define HAVE_WAIT 1 |
| 121 | #else |
| 122 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 123 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 124 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | #define HAVE_EXECV 1 |
| 126 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 128 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 129 | #define HAVE_FSYNC 1 |
| 130 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 131 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 132 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 133 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #else /* all other compilers */ |
| 135 | /* Unix functions that the configure script doesn't check for */ |
| 136 | #define HAVE_EXECV 1 |
| 137 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 138 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 139 | #define HAVE_FORK1 1 |
| 140 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #define HAVE_GETCWD 1 |
| 142 | #define HAVE_GETEGID 1 |
| 143 | #define HAVE_GETEUID 1 |
| 144 | #define HAVE_GETGID 1 |
| 145 | #define HAVE_GETPPID 1 |
| 146 | #define HAVE_GETUID 1 |
| 147 | #define HAVE_KILL 1 |
| 148 | #define HAVE_OPENDIR 1 |
| 149 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM 1 |
| 151 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 152 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 153 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #endif /* _MSC_VER */ |
| 155 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 156 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 157 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 160 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 161 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 162 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 163 | (default) */ |
| 164 | extern char *ctermid_r(char *); |
| 165 | #endif |
| 166 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 167 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 170 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 171 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 176 | #endif |
| 177 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int chdir(char *); |
| 179 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 180 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int chdir(const char *); |
| 182 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 183 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 184 | #ifdef __BORLANDC__ |
| 185 | extern int chmod(const char *, int); |
| 186 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 188 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 189 | extern int chown(const char *, uid_t, gid_t); |
| 190 | extern char *getcwd(char *, int); |
| 191 | extern char *strerror(int); |
| 192 | extern int link(const char *, const char *); |
| 193 | extern int rename(const char *, const char *); |
| 194 | extern int stat(const char *, struct stat *); |
| 195 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 197 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 198 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 201 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 204 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | #ifdef HAVE_UTIME_H |
| 207 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 208 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 210 | #ifdef HAVE_SYS_UTIME_H |
| 211 | #include <sys/utime.h> |
| 212 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 213 | #endif /* HAVE_SYS_UTIME_H */ |
| 214 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #ifdef HAVE_SYS_TIMES_H |
| 216 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 217 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | |
| 219 | #ifdef HAVE_SYS_PARAM_H |
| 220 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 221 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
| 223 | #ifdef HAVE_SYS_UTSNAME_H |
| 224 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 225 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 229 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 230 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 231 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #include <direct.h> |
| 233 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 234 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 236 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 237 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 238 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 240 | #endif |
| 241 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 243 | #endif |
| 244 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #endif |
| 247 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 249 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 250 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 252 | #endif |
| 253 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 255 | #endif |
| 256 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 258 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 259 | #include "osdefs.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 261 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 262 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 264 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 266 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 267 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 268 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 269 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 270 | #define MAXPATHLEN PATH_MAX |
| 271 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 272 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 273 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 274 | #endif /* MAXPATHLEN */ |
| 275 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 276 | #ifdef UNION_WAIT |
| 277 | /* Emulate some macros on systems that have a union instead of macros */ |
| 278 | |
| 279 | #ifndef WIFEXITED |
| 280 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 281 | #endif |
| 282 | |
| 283 | #ifndef WEXITSTATUS |
| 284 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 285 | #endif |
| 286 | |
| 287 | #ifndef WTERMSIG |
| 288 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 289 | #endif |
| 290 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 291 | #define WAIT_TYPE union wait |
| 292 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 293 | |
| 294 | #else /* !UNION_WAIT */ |
| 295 | #define WAIT_TYPE int |
| 296 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 297 | #endif /* UNION_WAIT */ |
| 298 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 299 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 300 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 301 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 302 | #define USE_CTERMID_R |
| 303 | #endif |
| 304 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 305 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 306 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 307 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 308 | # define STAT win32_stat |
| 309 | # define FSTAT win32_fstat |
| 310 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 311 | #else |
| 312 | # define STAT stat |
| 313 | # define FSTAT fstat |
| 314 | # define STRUCT_STAT struct stat |
| 315 | #endif |
| 316 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 317 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 318 | #include <sys/mkdev.h> |
| 319 | #else |
| 320 | #if defined(MAJOR_IN_SYSMACROS) |
| 321 | #include <sys/sysmacros.h> |
| 322 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 323 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 324 | #include <sys/mkdev.h> |
| 325 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 326 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 329 | #ifdef WITH_NEXT_FRAMEWORK |
| 330 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 331 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 332 | */ |
| 333 | #include <crt_externs.h> |
| 334 | static char **environ; |
| 335 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 337 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 339 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 340 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 342 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 343 | #ifdef MS_WINDOWS |
| 344 | wchar_t **e; |
| 345 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 346 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 347 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 348 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | if (d == NULL) |
| 350 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 351 | #ifdef WITH_NEXT_FRAMEWORK |
| 352 | if (environ == NULL) |
| 353 | environ = *_NSGetEnviron(); |
| 354 | #endif |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 355 | #ifdef MS_WINDOWS |
| 356 | /* _wenviron must be initialized in this way if the program is started |
| 357 | through main() instead of wmain(). */ |
| 358 | _wgetenv(L""); |
| 359 | if (_wenviron == NULL) |
| 360 | return d; |
| 361 | /* This part ignores errors */ |
| 362 | for (e = _wenviron; *e != NULL; e++) { |
| 363 | PyObject *k; |
| 364 | PyObject *v; |
| 365 | wchar_t *p = wcschr(*e, L'='); |
| 366 | if (p == NULL) |
| 367 | continue; |
| 368 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 369 | if (k == NULL) { |
| 370 | PyErr_Clear(); |
| 371 | continue; |
| 372 | } |
| 373 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 374 | if (v == NULL) { |
| 375 | PyErr_Clear(); |
| 376 | Py_DECREF(k); |
| 377 | continue; |
| 378 | } |
| 379 | if (PyDict_GetItem(d, k) == NULL) { |
| 380 | if (PyDict_SetItem(d, k, v) != 0) |
| 381 | PyErr_Clear(); |
| 382 | } |
| 383 | Py_DECREF(k); |
| 384 | Py_DECREF(v); |
| 385 | } |
| 386 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 387 | if (environ == NULL) |
| 388 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 389 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 391 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 392 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | char *p = strchr(*e, '='); |
| 394 | if (p == NULL) |
| 395 | continue; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 396 | k = PyUnicode_FromStringAndSize(*e, (int)(p-*e)); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 397 | if (k == NULL) { |
| 398 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 399 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 400 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 401 | v = PyUnicode_FromString(p+1); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 402 | if (v == NULL) { |
| 403 | PyErr_Clear(); |
| 404 | Py_DECREF(k); |
| 405 | continue; |
| 406 | } |
| 407 | if (PyDict_GetItem(d, k) == NULL) { |
| 408 | if (PyDict_SetItem(d, k, v) != 0) |
| 409 | PyErr_Clear(); |
| 410 | } |
| 411 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 412 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 413 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 414 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 415 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 416 | { |
| 417 | APIRET rc; |
| 418 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 419 | |
| 420 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 421 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 422 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 423 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 424 | Py_DECREF(v); |
| 425 | } |
| 426 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 427 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 428 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 429 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 430 | Py_DECREF(v); |
| 431 | } |
| 432 | } |
| 433 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 434 | return d; |
| 435 | } |
| 436 | |
| 437 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 438 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 439 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 440 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 441 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 442 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 443 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 444 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 445 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 446 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 447 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 448 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 451 | #ifdef Py_WIN_WIDE_FILENAMES |
| 452 | static PyObject * |
| 453 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 454 | { |
| 455 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 456 | } |
| 457 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 458 | |
| 459 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 460 | static PyObject * |
| 461 | posix_error_with_allocated_filename(char* name) |
| 462 | { |
| 463 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 464 | PyMem_Free(name); |
| 465 | return rc; |
| 466 | } |
| 467 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 468 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 469 | static PyObject * |
| 470 | win32_error(char* function, char* filename) |
| 471 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 472 | /* XXX We should pass the function name along in the future. |
| 473 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 474 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 475 | Windows error object, which is non-trivial. |
| 476 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 477 | errno = GetLastError(); |
| 478 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 479 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 480 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 481 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 482 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 483 | |
| 484 | #ifdef Py_WIN_WIDE_FILENAMES |
| 485 | static PyObject * |
| 486 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 487 | { |
| 488 | /* XXX - see win32_error for comments on 'function' */ |
| 489 | errno = GetLastError(); |
| 490 | if (filename) |
| 491 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 492 | else |
| 493 | return PyErr_SetFromWindowsErr(errno); |
| 494 | } |
| 495 | |
| 496 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 497 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /* Function suitable for O& conversion */ |
| 501 | static int |
| 502 | convert_to_unicode(PyObject *arg, void* _param) |
| 503 | { |
| 504 | PyObject **param = (PyObject**)_param; |
| 505 | if (PyUnicode_CheckExact(arg)) { |
| 506 | Py_INCREF(arg); |
| 507 | *param = arg; |
| 508 | } |
| 509 | else if (PyUnicode_Check(arg)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 510 | /* For a Unicode subtype that's not a Unicode object, |
| 511 | return a true Unicode object with the same data. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 512 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg), |
| 513 | PyUnicode_GET_SIZE(arg)); |
| 514 | return *param != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 515 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 516 | else |
| 517 | *param = PyUnicode_FromEncodedObject(arg, |
| 518 | Py_FileSystemDefaultEncoding, |
| 519 | "strict"); |
| 520 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 524 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 525 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 526 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 527 | #if defined(PYOS_OS2) |
| 528 | /********************************************************************** |
| 529 | * Helper Function to Trim and Format OS/2 Messages |
| 530 | **********************************************************************/ |
| 531 | static void |
| 532 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 533 | { |
| 534 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 535 | |
| 536 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 537 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 538 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 539 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 540 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 541 | } |
| 542 | |
| 543 | /* Add Optional Reason Text */ |
| 544 | if (reason) { |
| 545 | strcat(msgbuf, " : "); |
| 546 | strcat(msgbuf, reason); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /********************************************************************** |
| 551 | * Decode an OS/2 Operating System Error Code |
| 552 | * |
| 553 | * A convenience function to lookup an OS/2 error code and return a |
| 554 | * text message we can use to raise a Python exception. |
| 555 | * |
| 556 | * Notes: |
| 557 | * The messages for errors returned from the OS/2 kernel reside in |
| 558 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 559 | * |
| 560 | **********************************************************************/ |
| 561 | static char * |
| 562 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 563 | { |
| 564 | APIRET rc; |
| 565 | ULONG msglen; |
| 566 | |
| 567 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 568 | Py_BEGIN_ALLOW_THREADS |
| 569 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 570 | errorcode, "oso001.msg", &msglen); |
| 571 | Py_END_ALLOW_THREADS |
| 572 | |
| 573 | if (rc == NO_ERROR) |
| 574 | os2_formatmsg(msgbuf, msglen, reason); |
| 575 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 576 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 577 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 578 | |
| 579 | return msgbuf; |
| 580 | } |
| 581 | |
| 582 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 583 | errors are not in a global variable e.g. 'errno' nor are |
| 584 | they congruent with posix error numbers. */ |
| 585 | |
| 586 | static PyObject * os2_error(int code) |
| 587 | { |
| 588 | char text[1024]; |
| 589 | PyObject *v; |
| 590 | |
| 591 | os2_strerror(text, sizeof(text), code, ""); |
| 592 | |
| 593 | v = Py_BuildValue("(is)", code, text); |
| 594 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 595 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 596 | Py_DECREF(v); |
| 597 | } |
| 598 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 599 | } |
| 600 | |
| 601 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 602 | |
| 603 | /* POSIX generic methods */ |
| 604 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 605 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 606 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 607 | { |
| 608 | int fd; |
| 609 | int res; |
| 610 | fd = PyObject_AsFileDescriptor(fdobj); |
| 611 | if (fd < 0) |
| 612 | return NULL; |
| 613 | Py_BEGIN_ALLOW_THREADS |
| 614 | res = (*func)(fd); |
| 615 | Py_END_ALLOW_THREADS |
| 616 | if (res < 0) |
| 617 | return posix_error(); |
| 618 | Py_INCREF(Py_None); |
| 619 | return Py_None; |
| 620 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 621 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 622 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 623 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 624 | unicode_file_names(void) |
| 625 | { |
| 626 | static int canusewide = -1; |
| 627 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 628 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 629 | the Windows NT family. */ |
| 630 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 631 | } |
| 632 | return canusewide; |
| 633 | } |
| 634 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 636 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 637 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 638 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 639 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 640 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 641 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 642 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 643 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 644 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 645 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 646 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 647 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 648 | return posix_error_with_allocated_filename(path1); |
| 649 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 650 | Py_INCREF(Py_None); |
| 651 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 654 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 655 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 656 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 657 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 659 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 660 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 661 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 662 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 663 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 664 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 665 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 666 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 667 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 668 | PyMem_Free(path1); |
| 669 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 670 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 671 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 672 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 673 | Py_INCREF(Py_None); |
| 674 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 677 | #ifdef Py_WIN_WIDE_FILENAMES |
| 678 | static PyObject* |
| 679 | win32_1str(PyObject* args, char* func, |
| 680 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 681 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 682 | { |
| 683 | PyObject *uni; |
| 684 | char *ansi; |
| 685 | BOOL result; |
| 686 | if (unicode_file_names()) { |
| 687 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 688 | PyErr_Clear(); |
| 689 | else { |
| 690 | Py_BEGIN_ALLOW_THREADS |
| 691 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 692 | Py_END_ALLOW_THREADS |
| 693 | if (!result) |
| 694 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 695 | Py_INCREF(Py_None); |
| 696 | return Py_None; |
| 697 | } |
| 698 | } |
| 699 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 700 | return NULL; |
| 701 | Py_BEGIN_ALLOW_THREADS |
| 702 | result = funcA(ansi); |
| 703 | Py_END_ALLOW_THREADS |
| 704 | if (!result) |
| 705 | return win32_error(func, ansi); |
| 706 | Py_INCREF(Py_None); |
| 707 | return Py_None; |
| 708 | |
| 709 | } |
| 710 | |
| 711 | /* This is a reimplementation of the C library's chdir function, |
| 712 | but one that produces Win32 errors instead of DOS error codes. |
| 713 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 714 | it also needs to set "magic" environment variables indicating |
| 715 | the per-drive current directory, which are of the form =<drive>: */ |
| 716 | BOOL __stdcall |
| 717 | win32_chdir(LPCSTR path) |
| 718 | { |
| 719 | char new_path[MAX_PATH+1]; |
| 720 | int result; |
| 721 | char env[4] = "=x:"; |
| 722 | |
| 723 | if(!SetCurrentDirectoryA(path)) |
| 724 | return FALSE; |
| 725 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 726 | if (!result) |
| 727 | return FALSE; |
| 728 | /* In the ANSI API, there should not be any paths longer |
| 729 | than MAX_PATH. */ |
| 730 | assert(result <= MAX_PATH+1); |
| 731 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 732 | strncmp(new_path, "//", 2) == 0) |
| 733 | /* UNC path, nothing to do. */ |
| 734 | return TRUE; |
| 735 | env[1] = new_path[0]; |
| 736 | return SetEnvironmentVariableA(env, new_path); |
| 737 | } |
| 738 | |
| 739 | /* The Unicode version differs from the ANSI version |
| 740 | since the current directory might exceed MAX_PATH characters */ |
| 741 | BOOL __stdcall |
| 742 | win32_wchdir(LPCWSTR path) |
| 743 | { |
| 744 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 745 | int result; |
| 746 | wchar_t env[4] = L"=x:"; |
| 747 | |
| 748 | if(!SetCurrentDirectoryW(path)) |
| 749 | return FALSE; |
| 750 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 751 | if (!result) |
| 752 | return FALSE; |
| 753 | if (result > MAX_PATH+1) { |
| 754 | new_path = malloc(result); |
| 755 | if (!new_path) { |
| 756 | SetLastError(ERROR_OUTOFMEMORY); |
| 757 | return FALSE; |
| 758 | } |
| 759 | } |
| 760 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 761 | wcsncmp(new_path, L"//", 2) == 0) |
| 762 | /* UNC path, nothing to do. */ |
| 763 | return TRUE; |
| 764 | env[1] = new_path[0]; |
| 765 | result = SetEnvironmentVariableW(env, new_path); |
| 766 | if (new_path != _new_path) |
| 767 | free(new_path); |
| 768 | return result; |
| 769 | } |
| 770 | #endif |
| 771 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 772 | #ifdef MS_WINDOWS |
| 773 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 774 | - time stamps are restricted to second resolution |
| 775 | - file modification times suffer from forth-and-back conversions between |
| 776 | UTC and local time |
| 777 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 778 | */ |
| 779 | #define HAVE_STAT_NSEC 1 |
| 780 | |
| 781 | struct win32_stat{ |
| 782 | int st_dev; |
| 783 | __int64 st_ino; |
| 784 | unsigned short st_mode; |
| 785 | int st_nlink; |
| 786 | int st_uid; |
| 787 | int st_gid; |
| 788 | int st_rdev; |
| 789 | __int64 st_size; |
| 790 | int st_atime; |
| 791 | int st_atime_nsec; |
| 792 | int st_mtime; |
| 793 | int st_mtime_nsec; |
| 794 | int st_ctime; |
| 795 | int st_ctime_nsec; |
| 796 | }; |
| 797 | |
| 798 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 799 | |
| 800 | static void |
| 801 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 802 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 803 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 804 | /* Cannot simply cast and dereference in_ptr, |
| 805 | since it might not be aligned properly */ |
| 806 | __int64 in; |
| 807 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 808 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 809 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 810 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 811 | } |
| 812 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 813 | static void |
| 814 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 815 | { |
| 816 | /* XXX endianness */ |
| 817 | __int64 out; |
| 818 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 819 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 820 | memcpy(out_ptr, &out, sizeof(out)); |
| 821 | } |
| 822 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 823 | /* Below, we *know* that ugo+r is 0444 */ |
| 824 | #if _S_IREAD != 0400 |
| 825 | #error Unsupported C library |
| 826 | #endif |
| 827 | static int |
| 828 | attributes_to_mode(DWORD attr) |
| 829 | { |
| 830 | int m = 0; |
| 831 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 832 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 833 | else |
| 834 | m |= _S_IFREG; |
| 835 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 836 | m |= 0444; |
| 837 | else |
| 838 | m |= 0666; |
| 839 | return m; |
| 840 | } |
| 841 | |
| 842 | static int |
| 843 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 844 | { |
| 845 | memset(result, 0, sizeof(*result)); |
| 846 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 847 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 848 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 849 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 850 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 855 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 856 | static int checked = 0; |
| 857 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 858 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 859 | static void |
| 860 | check_gfax() |
| 861 | { |
| 862 | HINSTANCE hKernel32; |
| 863 | if (checked) |
| 864 | return; |
| 865 | checked = 1; |
| 866 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 867 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 868 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 869 | } |
| 870 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 871 | static BOOL |
| 872 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 873 | { |
| 874 | HANDLE hFindFile; |
| 875 | WIN32_FIND_DATAA FileData; |
| 876 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 877 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 878 | return FALSE; |
| 879 | FindClose(hFindFile); |
| 880 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 881 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 882 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 883 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 884 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 885 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 886 | return TRUE; |
| 887 | } |
| 888 | |
| 889 | static BOOL |
| 890 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 891 | { |
| 892 | HANDLE hFindFile; |
| 893 | WIN32_FIND_DATAW FileData; |
| 894 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 895 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 896 | return FALSE; |
| 897 | FindClose(hFindFile); |
| 898 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 899 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 900 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 901 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 902 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 903 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 904 | return TRUE; |
| 905 | } |
| 906 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 907 | static BOOL WINAPI |
| 908 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 909 | GET_FILEEX_INFO_LEVELS level, |
| 910 | LPVOID pv) |
| 911 | { |
| 912 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 913 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 914 | /* First try to use the system's implementation, if that is |
| 915 | available and either succeeds to gives an error other than |
| 916 | that it isn't implemented. */ |
| 917 | check_gfax(); |
| 918 | if (gfaxa) { |
| 919 | result = gfaxa(pszFile, level, pv); |
| 920 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 921 | return result; |
| 922 | } |
| 923 | /* It's either not present, or not implemented. |
| 924 | Emulate using FindFirstFile. */ |
| 925 | if (level != GetFileExInfoStandard) { |
| 926 | SetLastError(ERROR_INVALID_PARAMETER); |
| 927 | return FALSE; |
| 928 | } |
| 929 | /* Use GetFileAttributes to validate that the file name |
| 930 | does not contain wildcards (which FindFirstFile would |
| 931 | accept). */ |
| 932 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 933 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 934 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static BOOL WINAPI |
| 938 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 939 | GET_FILEEX_INFO_LEVELS level, |
| 940 | LPVOID pv) |
| 941 | { |
| 942 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 943 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 944 | /* First try to use the system's implementation, if that is |
| 945 | available and either succeeds to gives an error other than |
| 946 | that it isn't implemented. */ |
| 947 | check_gfax(); |
| 948 | if (gfaxa) { |
| 949 | result = gfaxw(pszFile, level, pv); |
| 950 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 951 | return result; |
| 952 | } |
| 953 | /* It's either not present, or not implemented. |
| 954 | Emulate using FindFirstFile. */ |
| 955 | if (level != GetFileExInfoStandard) { |
| 956 | SetLastError(ERROR_INVALID_PARAMETER); |
| 957 | return FALSE; |
| 958 | } |
| 959 | /* Use GetFileAttributes to validate that the file name |
| 960 | does not contain wildcards (which FindFirstFile would |
| 961 | accept). */ |
| 962 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 963 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 964 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 967 | static int |
| 968 | win32_stat(const char* path, struct win32_stat *result) |
| 969 | { |
| 970 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 971 | int code; |
| 972 | char *dot; |
| 973 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 974 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 975 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 976 | /* Protocol violation: we explicitly clear errno, instead of |
| 977 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 978 | errno = 0; |
| 979 | return -1; |
| 980 | } else { |
| 981 | /* Could not get attributes on open file. Fall back to |
| 982 | reading the directory. */ |
| 983 | if (!attributes_from_dir(path, &info)) { |
| 984 | /* Very strange. This should not fail now */ |
| 985 | errno = 0; |
| 986 | return -1; |
| 987 | } |
| 988 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 989 | } |
| 990 | code = attribute_data_to_stat(&info, result); |
| 991 | if (code != 0) |
| 992 | return code; |
| 993 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 994 | dot = strrchr(path, '.'); |
| 995 | if (dot) { |
| 996 | if (stricmp(dot, ".bat") == 0 || |
| 997 | stricmp(dot, ".cmd") == 0 || |
| 998 | stricmp(dot, ".exe") == 0 || |
| 999 | stricmp(dot, ".com") == 0) |
| 1000 | result->st_mode |= 0111; |
| 1001 | } |
| 1002 | return code; |
| 1003 | } |
| 1004 | |
| 1005 | static int |
| 1006 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1007 | { |
| 1008 | int code; |
| 1009 | const wchar_t *dot; |
| 1010 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1011 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1012 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1013 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1014 | /* Protocol violation: we explicitly clear errno, instead of |
| 1015 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1016 | errno = 0; |
| 1017 | return -1; |
| 1018 | } else { |
| 1019 | /* Could not get attributes on open file. Fall back to |
| 1020 | reading the directory. */ |
| 1021 | if (!attributes_from_dir_w(path, &info)) { |
| 1022 | /* Very strange. This should not fail now */ |
| 1023 | errno = 0; |
| 1024 | return -1; |
| 1025 | } |
| 1026 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1027 | } |
| 1028 | code = attribute_data_to_stat(&info, result); |
| 1029 | if (code < 0) |
| 1030 | return code; |
| 1031 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1032 | dot = wcsrchr(path, '.'); |
| 1033 | if (dot) { |
| 1034 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1035 | _wcsicmp(dot, L".cmd") == 0 || |
| 1036 | _wcsicmp(dot, L".exe") == 0 || |
| 1037 | _wcsicmp(dot, L".com") == 0) |
| 1038 | result->st_mode |= 0111; |
| 1039 | } |
| 1040 | return code; |
| 1041 | } |
| 1042 | |
| 1043 | static int |
| 1044 | win32_fstat(int file_number, struct win32_stat *result) |
| 1045 | { |
| 1046 | BY_HANDLE_FILE_INFORMATION info; |
| 1047 | HANDLE h; |
| 1048 | int type; |
| 1049 | |
| 1050 | h = (HANDLE)_get_osfhandle(file_number); |
| 1051 | |
| 1052 | /* Protocol violation: we explicitly clear errno, instead of |
| 1053 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1054 | errno = 0; |
| 1055 | |
| 1056 | if (h == INVALID_HANDLE_VALUE) { |
| 1057 | /* This is really a C library error (invalid file handle). |
| 1058 | We set the Win32 error to the closes one matching. */ |
| 1059 | SetLastError(ERROR_INVALID_HANDLE); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | memset(result, 0, sizeof(*result)); |
| 1063 | |
| 1064 | type = GetFileType(h); |
| 1065 | if (type == FILE_TYPE_UNKNOWN) { |
| 1066 | DWORD error = GetLastError(); |
| 1067 | if (error != 0) { |
| 1068 | return -1; |
| 1069 | } |
| 1070 | /* else: valid but unknown file */ |
| 1071 | } |
| 1072 | |
| 1073 | if (type != FILE_TYPE_DISK) { |
| 1074 | if (type == FILE_TYPE_CHAR) |
| 1075 | result->st_mode = _S_IFCHR; |
| 1076 | else if (type == FILE_TYPE_PIPE) |
| 1077 | result->st_mode = _S_IFIFO; |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | if (!GetFileInformationByHandle(h, &info)) { |
| 1082 | return -1; |
| 1083 | } |
| 1084 | |
| 1085 | /* similar to stat() */ |
| 1086 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1087 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1088 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1089 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1090 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1091 | /* specific to fstat() */ |
| 1092 | result->st_nlink = info.nNumberOfLinks; |
| 1093 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | #endif /* MS_WINDOWS */ |
| 1098 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1099 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1100 | "stat_result: Result from stat or lstat.\n\n\ |
| 1101 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1102 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1103 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1104 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1105 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1106 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1107 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1108 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1109 | |
| 1110 | static PyStructSequence_Field stat_result_fields[] = { |
| 1111 | {"st_mode", "protection bits"}, |
| 1112 | {"st_ino", "inode"}, |
| 1113 | {"st_dev", "device"}, |
| 1114 | {"st_nlink", "number of hard links"}, |
| 1115 | {"st_uid", "user ID of owner"}, |
| 1116 | {"st_gid", "group ID of owner"}, |
| 1117 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1118 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1119 | {NULL, "integer time of last access"}, |
| 1120 | {NULL, "integer time of last modification"}, |
| 1121 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1122 | {"st_atime", "time of last access"}, |
| 1123 | {"st_mtime", "time of last modification"}, |
| 1124 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1125 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1126 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1127 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1128 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1129 | {"st_blocks", "number of blocks allocated"}, |
| 1130 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1131 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1132 | {"st_rdev", "device type (if inode device)"}, |
| 1133 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1134 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1135 | {"st_flags", "user defined flags for file"}, |
| 1136 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1137 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1138 | {"st_gen", "generation number"}, |
| 1139 | #endif |
| 1140 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1141 | {"st_birthtime", "time of creation"}, |
| 1142 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1143 | {0} |
| 1144 | }; |
| 1145 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1146 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1147 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1148 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1149 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1150 | #endif |
| 1151 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1152 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1153 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1154 | #else |
| 1155 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1156 | #endif |
| 1157 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1158 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1159 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1160 | #else |
| 1161 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1162 | #endif |
| 1163 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1164 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1165 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1166 | #else |
| 1167 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1168 | #endif |
| 1169 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1170 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1171 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1172 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1173 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1174 | #endif |
| 1175 | |
| 1176 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1177 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1178 | #else |
| 1179 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1180 | #endif |
| 1181 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1182 | static PyStructSequence_Desc stat_result_desc = { |
| 1183 | "stat_result", /* name */ |
| 1184 | stat_result__doc__, /* doc */ |
| 1185 | stat_result_fields, |
| 1186 | 10 |
| 1187 | }; |
| 1188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1189 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1190 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1191 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1192 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1193 | or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1194 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1195 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1196 | |
| 1197 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1198 | {"f_bsize", }, |
| 1199 | {"f_frsize", }, |
| 1200 | {"f_blocks", }, |
| 1201 | {"f_bfree", }, |
| 1202 | {"f_bavail", }, |
| 1203 | {"f_files", }, |
| 1204 | {"f_ffree", }, |
| 1205 | {"f_favail", }, |
| 1206 | {"f_flag", }, |
| 1207 | {"f_namemax",}, |
| 1208 | {0} |
| 1209 | }; |
| 1210 | |
| 1211 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1212 | "statvfs_result", /* name */ |
| 1213 | statvfs_result__doc__, /* doc */ |
| 1214 | statvfs_result_fields, |
| 1215 | 10 |
| 1216 | }; |
| 1217 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1218 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1219 | static PyTypeObject StatResultType; |
| 1220 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1221 | static newfunc structseq_new; |
| 1222 | |
| 1223 | static PyObject * |
| 1224 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1225 | { |
| 1226 | PyStructSequence *result; |
| 1227 | int i; |
| 1228 | |
| 1229 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1230 | if (!result) |
| 1231 | return NULL; |
| 1232 | /* If we have been initialized from a tuple, |
| 1233 | st_?time might be set to None. Initialize it |
| 1234 | from the int slots. */ |
| 1235 | for (i = 7; i <= 9; i++) { |
| 1236 | if (result->ob_item[i+3] == Py_None) { |
| 1237 | Py_DECREF(Py_None); |
| 1238 | Py_INCREF(result->ob_item[i]); |
| 1239 | result->ob_item[i+3] = result->ob_item[i]; |
| 1240 | } |
| 1241 | } |
| 1242 | return (PyObject*)result; |
| 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1248 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1249 | |
| 1250 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1251 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1252 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1253 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1254 | future calls return ints. \n\ |
| 1255 | If newval is omitted, return the current setting.\n"); |
| 1256 | |
| 1257 | static PyObject* |
| 1258 | stat_float_times(PyObject* self, PyObject *args) |
| 1259 | { |
| 1260 | int newval = -1; |
| 1261 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1262 | return NULL; |
| 1263 | if (newval == -1) |
| 1264 | /* Return old value */ |
| 1265 | return PyBool_FromLong(_stat_float_times); |
| 1266 | _stat_float_times = newval; |
| 1267 | Py_INCREF(Py_None); |
| 1268 | return Py_None; |
| 1269 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1270 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1271 | static void |
| 1272 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1273 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1274 | PyObject *fval,*ival; |
| 1275 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1276 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1277 | #else |
| 1278 | ival = PyInt_FromLong((long)sec); |
| 1279 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1280 | if (!ival) |
| 1281 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1282 | if (_stat_float_times) { |
| 1283 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1284 | } else { |
| 1285 | fval = ival; |
| 1286 | Py_INCREF(fval); |
| 1287 | } |
| 1288 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1289 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1292 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1293 | (used by posix_stat() and posix_fstat()) */ |
| 1294 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1295 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1296 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1297 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1298 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1299 | if (v == NULL) |
| 1300 | return NULL; |
| 1301 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1302 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1303 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1304 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1305 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1306 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1307 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1308 | #endif |
| 1309 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1310 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1311 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1312 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1313 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1314 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1315 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1316 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1317 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1318 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1319 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1320 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1321 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1322 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1323 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1324 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1325 | #if defined(HAVE_STAT_TV_NSEC) |
| 1326 | ansec = st->st_atim.tv_nsec; |
| 1327 | mnsec = st->st_mtim.tv_nsec; |
| 1328 | cnsec = st->st_ctim.tv_nsec; |
| 1329 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1330 | ansec = st->st_atimespec.tv_nsec; |
| 1331 | mnsec = st->st_mtimespec.tv_nsec; |
| 1332 | cnsec = st->st_ctimespec.tv_nsec; |
| 1333 | #elif defined(HAVE_STAT_NSEC) |
| 1334 | ansec = st->st_atime_nsec; |
| 1335 | mnsec = st->st_mtime_nsec; |
| 1336 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1337 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1338 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1339 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1340 | fill_time(v, 7, st->st_atime, ansec); |
| 1341 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1342 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1343 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1344 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1345 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1346 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1347 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1348 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1349 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1350 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1351 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1352 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1353 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1354 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1355 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1356 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1357 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1358 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1359 | #endif |
| 1360 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1361 | { |
| 1362 | PyObject *val; |
| 1363 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1364 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1365 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1366 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1367 | #else |
| 1368 | bnsec = 0; |
| 1369 | #endif |
| 1370 | if (_stat_float_times) { |
| 1371 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1372 | } else { |
| 1373 | val = PyInt_FromLong((long)bsec); |
| 1374 | } |
| 1375 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1376 | val); |
| 1377 | } |
| 1378 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1379 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1380 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1381 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1382 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1383 | |
| 1384 | if (PyErr_Occurred()) { |
| 1385 | Py_DECREF(v); |
| 1386 | return NULL; |
| 1387 | } |
| 1388 | |
| 1389 | return v; |
| 1390 | } |
| 1391 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1392 | #ifdef MS_WINDOWS |
| 1393 | |
| 1394 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1395 | where / can be used in place of \ and the trailing slash is optional. |
| 1396 | Both SERVER and SHARE must have at least one character. |
| 1397 | */ |
| 1398 | |
| 1399 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1400 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1401 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1402 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1403 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1404 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1405 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1406 | IsUNCRootA(char *path, int pathlen) |
| 1407 | { |
| 1408 | #define ISSLASH ISSLASHA |
| 1409 | |
| 1410 | int i, share; |
| 1411 | |
| 1412 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1413 | /* minimum UNCRoot is \\x\y */ |
| 1414 | return FALSE; |
| 1415 | for (i = 2; i < pathlen ; i++) |
| 1416 | if (ISSLASH(path[i])) break; |
| 1417 | if (i == 2 || i == pathlen) |
| 1418 | /* do not allow \\\SHARE or \\SERVER */ |
| 1419 | return FALSE; |
| 1420 | share = i+1; |
| 1421 | for (i = share; i < pathlen; i++) |
| 1422 | if (ISSLASH(path[i])) break; |
| 1423 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1424 | |
| 1425 | #undef ISSLASH |
| 1426 | } |
| 1427 | |
| 1428 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1429 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1430 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1431 | { |
| 1432 | #define ISSLASH ISSLASHW |
| 1433 | |
| 1434 | int i, share; |
| 1435 | |
| 1436 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1437 | /* minimum UNCRoot is \\x\y */ |
| 1438 | return FALSE; |
| 1439 | for (i = 2; i < pathlen ; i++) |
| 1440 | if (ISSLASH(path[i])) break; |
| 1441 | if (i == 2 || i == pathlen) |
| 1442 | /* do not allow \\\SHARE or \\SERVER */ |
| 1443 | return FALSE; |
| 1444 | share = i+1; |
| 1445 | for (i = share; i < pathlen; i++) |
| 1446 | if (ISSLASH(path[i])) break; |
| 1447 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1448 | |
| 1449 | #undef ISSLASH |
| 1450 | } |
| 1451 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1452 | #endif /* MS_WINDOWS */ |
| 1453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1454 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1455 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1456 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1457 | #ifdef __VMS |
| 1458 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1459 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1460 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1461 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1462 | char *wformat, |
| 1463 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1464 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1465 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1466 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1467 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1468 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1469 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1470 | |
| 1471 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1472 | /* If on wide-character-capable OS see if argument |
| 1473 | is Unicode and if so use wide API. */ |
| 1474 | if (unicode_file_names()) { |
| 1475 | PyUnicodeObject *po; |
| 1476 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1477 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1478 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1479 | Py_BEGIN_ALLOW_THREADS |
| 1480 | /* PyUnicode_AS_UNICODE result OK without |
| 1481 | thread lock as it is a simple dereference. */ |
| 1482 | res = wstatfunc(wpath, &st); |
| 1483 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1484 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1485 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1486 | return win32_error_unicode("stat", wpath); |
| 1487 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1488 | } |
| 1489 | /* Drop the argument parsing error as narrow strings |
| 1490 | are also valid. */ |
| 1491 | PyErr_Clear(); |
| 1492 | } |
| 1493 | #endif |
| 1494 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1495 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1496 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1497 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1498 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1499 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1500 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1501 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1502 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1503 | |
| 1504 | if (res != 0) { |
| 1505 | #ifdef MS_WINDOWS |
| 1506 | result = win32_error("stat", pathfree); |
| 1507 | #else |
| 1508 | result = posix_error_with_filename(pathfree); |
| 1509 | #endif |
| 1510 | } |
| 1511 | else |
| 1512 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1513 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1514 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1515 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1518 | /* POSIX methods */ |
| 1519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1520 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1521 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1522 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1523 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1524 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1525 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1526 | existence, or the inclusive-OR of R_OK, W_OK, and X_OK."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1527 | |
| 1528 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1529 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1530 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1531 | char *path; |
| 1532 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1533 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1534 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1535 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1536 | if (unicode_file_names()) { |
| 1537 | PyUnicodeObject *po; |
| 1538 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1539 | Py_BEGIN_ALLOW_THREADS |
| 1540 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1541 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1542 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1543 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1544 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1545 | } |
| 1546 | /* Drop the argument parsing error as narrow strings |
| 1547 | are also valid. */ |
| 1548 | PyErr_Clear(); |
| 1549 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1550 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1551 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1552 | return 0; |
| 1553 | Py_BEGIN_ALLOW_THREADS |
| 1554 | attr = GetFileAttributesA(path); |
| 1555 | Py_END_ALLOW_THREADS |
| 1556 | PyMem_Free(path); |
| 1557 | finish: |
| 1558 | if (attr == 0xFFFFFFFF) |
| 1559 | /* File does not exist, or cannot read attributes */ |
| 1560 | return PyBool_FromLong(0); |
| 1561 | /* Access is possible if either write access wasn't requested, or |
| 1562 | the file isn't read-only. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1563 | return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1564 | #else |
| 1565 | int res; |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1566 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1567 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1568 | return NULL; |
| 1569 | Py_BEGIN_ALLOW_THREADS |
| 1570 | res = access(path, mode); |
| 1571 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1572 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1573 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1574 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1577 | #ifndef F_OK |
| 1578 | #define F_OK 0 |
| 1579 | #endif |
| 1580 | #ifndef R_OK |
| 1581 | #define R_OK 4 |
| 1582 | #endif |
| 1583 | #ifndef W_OK |
| 1584 | #define W_OK 2 |
| 1585 | #endif |
| 1586 | #ifndef X_OK |
| 1587 | #define X_OK 1 |
| 1588 | #endif |
| 1589 | |
| 1590 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1591 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1592 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1593 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1594 | |
| 1595 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1596 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1597 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1598 | int id; |
| 1599 | char *ret; |
| 1600 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1601 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1602 | return NULL; |
| 1603 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1604 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1605 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1606 | if (id == 0) { |
| 1607 | ret = ttyname(); |
| 1608 | } |
| 1609 | else { |
| 1610 | ret = NULL; |
| 1611 | } |
| 1612 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1613 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1614 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1615 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1616 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1617 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1618 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1619 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1620 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1621 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1622 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1623 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1624 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1625 | |
| 1626 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1627 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1628 | { |
| 1629 | char *ret; |
| 1630 | char buffer[L_ctermid]; |
| 1631 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1632 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1633 | ret = ctermid_r(buffer); |
| 1634 | #else |
| 1635 | ret = ctermid(buffer); |
| 1636 | #endif |
| 1637 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1638 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1639 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1640 | } |
| 1641 | #endif |
| 1642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1643 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1644 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1645 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1646 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1647 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1648 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1649 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1650 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1651 | return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1652 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1653 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1654 | #elif defined(__VMS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1655 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1656 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1657 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1658 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1661 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1662 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1663 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1664 | Change to the directory of the given file descriptor. fildes must be\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1665 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1666 | |
| 1667 | static PyObject * |
| 1668 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1669 | { |
| 1670 | return posix_fildes(fdobj, fchdir); |
| 1671 | } |
| 1672 | #endif /* HAVE_FCHDIR */ |
| 1673 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1675 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1676 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1677 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1678 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1679 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1680 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1681 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1682 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1683 | int i; |
| 1684 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1685 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1686 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1687 | if (unicode_file_names()) { |
| 1688 | PyUnicodeObject *po; |
| 1689 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1690 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1691 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1692 | if (attr != 0xFFFFFFFF) { |
| 1693 | if (i & _S_IWRITE) |
| 1694 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1695 | else |
| 1696 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1697 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1698 | } |
| 1699 | else |
| 1700 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1701 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1702 | if (!res) |
| 1703 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1704 | PyUnicode_AS_UNICODE(po)); |
| 1705 | Py_INCREF(Py_None); |
| 1706 | return Py_None; |
| 1707 | } |
| 1708 | /* Drop the argument parsing error as narrow strings |
| 1709 | are also valid. */ |
| 1710 | PyErr_Clear(); |
| 1711 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1712 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1713 | &path, &i)) |
| 1714 | return NULL; |
| 1715 | Py_BEGIN_ALLOW_THREADS |
| 1716 | attr = GetFileAttributesA(path); |
| 1717 | if (attr != 0xFFFFFFFF) { |
| 1718 | if (i & _S_IWRITE) |
| 1719 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1720 | else |
| 1721 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1722 | res = SetFileAttributesA(path, attr); |
| 1723 | } |
| 1724 | else |
| 1725 | res = 0; |
| 1726 | Py_END_ALLOW_THREADS |
| 1727 | if (!res) { |
| 1728 | win32_error("chmod", path); |
| 1729 | PyMem_Free(path); |
| 1730 | return NULL; |
| 1731 | } |
| 1732 | PyMem_Free(path); |
| 1733 | Py_INCREF(Py_None); |
| 1734 | return Py_None; |
| 1735 | #else /* Py_WIN_WIDE_FILENAMES */ |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1736 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1737 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1738 | return NULL; |
| 1739 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1740 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1741 | Py_END_ALLOW_THREADS |
| 1742 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1743 | return posix_error_with_allocated_filename(path); |
| 1744 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1745 | Py_INCREF(Py_None); |
| 1746 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1747 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1750 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1751 | #ifdef HAVE_CHFLAGS |
| 1752 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1753 | "chflags(path, flags)\n\n\ |
| 1754 | Set file flags."); |
| 1755 | |
| 1756 | static PyObject * |
| 1757 | posix_chflags(PyObject *self, PyObject *args) |
| 1758 | { |
| 1759 | char *path; |
| 1760 | unsigned long flags; |
| 1761 | int res; |
| 1762 | if (!PyArg_ParseTuple(args, "etk:chflags", |
| 1763 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1764 | return NULL; |
| 1765 | Py_BEGIN_ALLOW_THREADS |
| 1766 | res = chflags(path, flags); |
| 1767 | Py_END_ALLOW_THREADS |
| 1768 | if (res < 0) |
| 1769 | return posix_error_with_allocated_filename(path); |
| 1770 | PyMem_Free(path); |
| 1771 | Py_INCREF(Py_None); |
| 1772 | return Py_None; |
| 1773 | } |
| 1774 | #endif /* HAVE_CHFLAGS */ |
| 1775 | |
| 1776 | #ifdef HAVE_LCHFLAGS |
| 1777 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1778 | "lchflags(path, flags)\n\n\ |
| 1779 | Set file flags.\n\ |
| 1780 | This function will not follow symbolic links."); |
| 1781 | |
| 1782 | static PyObject * |
| 1783 | posix_lchflags(PyObject *self, PyObject *args) |
| 1784 | { |
| 1785 | char *path; |
| 1786 | unsigned long flags; |
| 1787 | int res; |
| 1788 | if (!PyArg_ParseTuple(args, "etk:lchflags", |
| 1789 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1790 | return NULL; |
| 1791 | Py_BEGIN_ALLOW_THREADS |
| 1792 | res = lchflags(path, flags); |
| 1793 | Py_END_ALLOW_THREADS |
| 1794 | if (res < 0) |
| 1795 | return posix_error_with_allocated_filename(path); |
| 1796 | PyMem_Free(path); |
| 1797 | Py_INCREF(Py_None); |
| 1798 | return Py_None; |
| 1799 | } |
| 1800 | #endif /* HAVE_LCHFLAGS */ |
| 1801 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1802 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1803 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1804 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1805 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1806 | |
| 1807 | static PyObject * |
| 1808 | posix_chroot(PyObject *self, PyObject *args) |
| 1809 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1810 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1811 | } |
| 1812 | #endif |
| 1813 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1814 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1815 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1816 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1817 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1818 | |
| 1819 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1820 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1821 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1822 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1823 | } |
| 1824 | #endif /* HAVE_FSYNC */ |
| 1825 | |
| 1826 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1827 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1828 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1829 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1830 | #endif |
| 1831 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1832 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1833 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1834 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1835 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1836 | |
| 1837 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1838 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1839 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1840 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1841 | } |
| 1842 | #endif /* HAVE_FDATASYNC */ |
| 1843 | |
| 1844 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1845 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1846 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1847 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1848 | Change the owner and group id of path to the numeric uid and gid."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1849 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1850 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1851 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1852 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1853 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1854 | int uid, gid; |
| 1855 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1856 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1857 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1858 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1859 | return NULL; |
| 1860 | Py_BEGIN_ALLOW_THREADS |
| 1861 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1862 | Py_END_ALLOW_THREADS |
| 1863 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1864 | return posix_error_with_allocated_filename(path); |
| 1865 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1866 | Py_INCREF(Py_None); |
| 1867 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1868 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1869 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1870 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1871 | #ifdef HAVE_LCHOWN |
| 1872 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1873 | "lchown(path, uid, gid)\n\n\ |
| 1874 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1875 | This function will not follow symbolic links."); |
| 1876 | |
| 1877 | static PyObject * |
| 1878 | posix_lchown(PyObject *self, PyObject *args) |
| 1879 | { |
| 1880 | char *path = NULL; |
| 1881 | int uid, gid; |
| 1882 | int res; |
| 1883 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1884 | Py_FileSystemDefaultEncoding, &path, |
| 1885 | &uid, &gid)) |
| 1886 | return NULL; |
| 1887 | Py_BEGIN_ALLOW_THREADS |
| 1888 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1889 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1890 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1891 | return posix_error_with_allocated_filename(path); |
| 1892 | PyMem_Free(path); |
| 1893 | Py_INCREF(Py_None); |
| 1894 | return Py_None; |
| 1895 | } |
| 1896 | #endif /* HAVE_LCHOWN */ |
| 1897 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1898 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1899 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1900 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1901 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1902 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1903 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1904 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1905 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1906 | { |
| 1907 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1908 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1909 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1910 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1911 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1912 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1913 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1914 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1915 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1916 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1917 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1918 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1919 | return PyUnicode_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1920 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1921 | |
| 1922 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1923 | "getcwdu() -> path\n\n\ |
| 1924 | Return a unicode string representing the current working directory."); |
| 1925 | |
| 1926 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1927 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1928 | { |
| 1929 | char buf[1026]; |
| 1930 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1931 | |
| 1932 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1933 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1934 | if (unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1935 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1936 | wchar_t *wbuf2 = wbuf; |
| 1937 | PyObject *resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1938 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1939 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 1940 | /* If the buffer is large enough, len does not include the |
| 1941 | terminating \0. If the buffer is too small, len includes |
| 1942 | the space needed for the terminator. */ |
| 1943 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 1944 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 1945 | if (wbuf2) |
| 1946 | len = GetCurrentDirectoryW(len, wbuf2); |
| 1947 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1948 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1949 | if (!wbuf2) { |
| 1950 | PyErr_NoMemory(); |
| 1951 | return NULL; |
| 1952 | } |
| 1953 | if (!len) { |
| 1954 | if (wbuf2 != wbuf) free(wbuf2); |
| 1955 | return win32_error("getcwdu", NULL); |
| 1956 | } |
| 1957 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 1958 | if (wbuf2 != wbuf) free(wbuf2); |
| 1959 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1960 | } |
| 1961 | #endif |
| 1962 | |
| 1963 | Py_BEGIN_ALLOW_THREADS |
| 1964 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1965 | res = _getcwd2(buf, sizeof buf); |
| 1966 | #else |
| 1967 | res = getcwd(buf, sizeof buf); |
| 1968 | #endif |
| 1969 | Py_END_ALLOW_THREADS |
| 1970 | if (res == NULL) |
| 1971 | return posix_error(); |
| 1972 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1973 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1974 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1975 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1976 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1977 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1978 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1979 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1980 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1981 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1982 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1983 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1984 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1985 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1986 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1987 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1988 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1990 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1991 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1992 | Return a list containing the names of the entries in the directory.\n\ |
| 1993 | \n\ |
| 1994 | path: path of directory to list\n\ |
| 1995 | \n\ |
| 1996 | The list is in arbitrary order. It does not include the special\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1997 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1998 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1999 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2000 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2001 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2002 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2003 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2004 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2005 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2006 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2007 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2008 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2009 | WIN32_FIND_DATA FileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2010 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2011 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2012 | Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2013 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2014 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2015 | /* If on wide-character-capable OS see if argument |
| 2016 | is Unicode and if so use wide API. */ |
| 2017 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2018 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2019 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2020 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2021 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2022 | Py_UNICODE wch; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2023 | /* Overallocate for \\*.*\0 */ |
| 2024 | len = PyUnicode_GET_SIZE(po); |
| 2025 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2026 | if (!wnamebuf) { |
| 2027 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2028 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2029 | } |
| 2030 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2031 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 2032 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2033 | wnamebuf[len++] = L'\\'; |
| 2034 | wcscpy(wnamebuf + len, L"*.*"); |
| 2035 | if ((d = PyList_New(0)) == NULL) { |
| 2036 | free(wnamebuf); |
| 2037 | return NULL; |
| 2038 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2039 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2040 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2041 | int error = GetLastError(); |
| 2042 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2043 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2044 | return d; |
| 2045 | } |
| 2046 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2047 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2048 | free(wnamebuf); |
| 2049 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2050 | } |
| 2051 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2052 | /* Skip over . and .. */ |
| 2053 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2054 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2055 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2056 | if (v == NULL) { |
| 2057 | Py_DECREF(d); |
| 2058 | d = NULL; |
| 2059 | break; |
| 2060 | } |
| 2061 | if (PyList_Append(d, v) != 0) { |
| 2062 | Py_DECREF(v); |
| 2063 | Py_DECREF(d); |
| 2064 | d = NULL; |
| 2065 | break; |
| 2066 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2067 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2068 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2069 | Py_BEGIN_ALLOW_THREADS |
| 2070 | result = FindNextFileW(hFindFile, &wFileData); |
| 2071 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2072 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2073 | it got to the end of the directory. */ |
| 2074 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2075 | Py_DECREF(d); |
| 2076 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2077 | FindClose(hFindFile); |
| 2078 | free(wnamebuf); |
| 2079 | return NULL; |
| 2080 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2081 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2082 | |
| 2083 | if (FindClose(hFindFile) == FALSE) { |
| 2084 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2085 | win32_error_unicode("FindClose", wnamebuf); |
| 2086 | free(wnamebuf); |
| 2087 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2088 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2089 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2090 | return d; |
| 2091 | } |
| 2092 | /* Drop the argument parsing error as narrow strings |
| 2093 | are also valid. */ |
| 2094 | PyErr_Clear(); |
| 2095 | } |
| 2096 | #endif |
| 2097 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2098 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2099 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2100 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2101 | if (len > 0) { |
| 2102 | char ch = namebuf[len-1]; |
| 2103 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2104 | namebuf[len++] = '/'; |
| 2105 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2106 | strcpy(namebuf + len, "*.*"); |
| 2107 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2108 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2109 | return NULL; |
| 2110 | |
| 2111 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2112 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2113 | int error = GetLastError(); |
| 2114 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2115 | return d; |
| 2116 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2117 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2118 | } |
| 2119 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2120 | /* Skip over . and .. */ |
| 2121 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2122 | strcmp(FileData.cFileName, "..") != 0) { |
| 2123 | v = PyString_FromString(FileData.cFileName); |
| 2124 | if (v == NULL) { |
| 2125 | Py_DECREF(d); |
| 2126 | d = NULL; |
| 2127 | break; |
| 2128 | } |
| 2129 | if (PyList_Append(d, v) != 0) { |
| 2130 | Py_DECREF(v); |
| 2131 | Py_DECREF(d); |
| 2132 | d = NULL; |
| 2133 | break; |
| 2134 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2135 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2136 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2137 | Py_BEGIN_ALLOW_THREADS |
| 2138 | result = FindNextFile(hFindFile, &FileData); |
| 2139 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2140 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2141 | it got to the end of the directory. */ |
| 2142 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2143 | Py_DECREF(d); |
| 2144 | win32_error("FindNextFile", namebuf); |
| 2145 | FindClose(hFindFile); |
| 2146 | return NULL; |
| 2147 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2148 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2149 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2150 | if (FindClose(hFindFile) == FALSE) { |
| 2151 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2152 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2153 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2154 | |
| 2155 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2156 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2157 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2158 | |
| 2159 | #ifndef MAX_PATH |
| 2160 | #define MAX_PATH CCHMAXPATH |
| 2161 | #endif |
| 2162 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2163 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2164 | PyObject *d, *v; |
| 2165 | char namebuf[MAX_PATH+5]; |
| 2166 | HDIR hdir = 1; |
| 2167 | ULONG srchcnt = 1; |
| 2168 | FILEFINDBUF3 ep; |
| 2169 | APIRET rc; |
| 2170 | |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 2171 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 2172 | Py_FileSystemDefaultEncoding, &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2173 | return NULL; |
| 2174 | if (len >= MAX_PATH) { |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2175 | PyMem_Free(name); |
| 2176 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2177 | return NULL; |
| 2178 | } |
| 2179 | strcpy(namebuf, name); |
| 2180 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2181 | if (*pt == ALTSEP) |
| 2182 | *pt = SEP; |
| 2183 | if (namebuf[len-1] != SEP) |
| 2184 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2185 | strcpy(namebuf + len, "*.*"); |
| 2186 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2187 | if ((d = PyList_New(0)) == NULL) { |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2188 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2189 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2190 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2191 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2192 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2193 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2194 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2195 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2196 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2197 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2198 | |
| 2199 | if (rc != NO_ERROR) { |
| 2200 | errno = ENOENT; |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2201 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2204 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2205 | do { |
| 2206 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2207 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2208 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2209 | |
| 2210 | strcpy(namebuf, ep.achName); |
| 2211 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2212 | /* Leave Case of Name Alone -- In Native Form */ |
| 2213 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2214 | |
| 2215 | v = PyString_FromString(namebuf); |
| 2216 | if (v == NULL) { |
| 2217 | Py_DECREF(d); |
| 2218 | d = NULL; |
| 2219 | break; |
| 2220 | } |
| 2221 | if (PyList_Append(d, v) != 0) { |
| 2222 | Py_DECREF(v); |
| 2223 | Py_DECREF(d); |
| 2224 | d = NULL; |
| 2225 | break; |
| 2226 | } |
| 2227 | Py_DECREF(v); |
| 2228 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2229 | } |
| 2230 | |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2231 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2232 | return d; |
| 2233 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2234 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2235 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2236 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2237 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2238 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2239 | int arg_is_unicode = 1; |
| 2240 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2241 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2242 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2243 | arg_is_unicode = 0; |
| 2244 | PyErr_Clear(); |
| 2245 | } |
| 2246 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2247 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2248 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2249 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2250 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2251 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2252 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2253 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2254 | return NULL; |
| 2255 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2256 | for (;;) { |
| 2257 | Py_BEGIN_ALLOW_THREADS |
| 2258 | ep = readdir(dirp); |
| 2259 | Py_END_ALLOW_THREADS |
| 2260 | if (ep == NULL) |
| 2261 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2262 | if (ep->d_name[0] == '.' && |
| 2263 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2264 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2265 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2266 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2267 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2268 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2269 | d = NULL; |
| 2270 | break; |
| 2271 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2272 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2273 | PyObject *w; |
| 2274 | |
| 2275 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2276 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2277 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2278 | if (w != NULL) { |
| 2279 | Py_DECREF(v); |
| 2280 | v = w; |
| 2281 | } |
| 2282 | else { |
| 2283 | /* fall back to the original byte string, as |
| 2284 | discussed in patch #683592 */ |
| 2285 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2286 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2287 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2288 | if (PyList_Append(d, v) != 0) { |
| 2289 | Py_DECREF(v); |
| 2290 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2291 | d = NULL; |
| 2292 | break; |
| 2293 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2294 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2295 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2296 | if (errno != 0 && d != NULL) { |
| 2297 | /* readdir() returned NULL and set errno */ |
| 2298 | closedir(dirp); |
| 2299 | Py_DECREF(d); |
| 2300 | return posix_error_with_allocated_filename(name); |
| 2301 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2302 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2303 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2304 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2305 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2306 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2307 | #endif /* which OS */ |
| 2308 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2309 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2310 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2311 | /* A helper function for abspath on win32 */ |
| 2312 | static PyObject * |
| 2313 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2314 | { |
| 2315 | /* assume encoded strings wont more than double no of chars */ |
| 2316 | char inbuf[MAX_PATH*2]; |
| 2317 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2318 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2319 | char outbuf[MAX_PATH*2]; |
| 2320 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2321 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2322 | if (unicode_file_names()) { |
| 2323 | PyUnicodeObject *po; |
| 2324 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2325 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2326 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2327 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2328 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2329 | woutbuf, &wtemp)) |
| 2330 | return win32_error("GetFullPathName", ""); |
| 2331 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2332 | } |
| 2333 | /* Drop the argument parsing error as narrow strings |
| 2334 | are also valid. */ |
| 2335 | PyErr_Clear(); |
| 2336 | } |
| 2337 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2338 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2339 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2340 | &insize)) |
| 2341 | return NULL; |
| 2342 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2343 | outbuf, &temp)) |
| 2344 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2345 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2346 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2347 | Py_FileSystemDefaultEncoding, NULL); |
| 2348 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2349 | return PyString_FromString(outbuf); |
| 2350 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2351 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2352 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2353 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2354 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2355 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2356 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2357 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2358 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2359 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2360 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2361 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2362 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2363 | |
| 2364 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2365 | if (unicode_file_names()) { |
| 2366 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2367 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2368 | Py_BEGIN_ALLOW_THREADS |
| 2369 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2370 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2371 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2372 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2373 | if (!res) |
| 2374 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2375 | Py_INCREF(Py_None); |
| 2376 | return Py_None; |
| 2377 | } |
| 2378 | /* Drop the argument parsing error as narrow strings |
| 2379 | are also valid. */ |
| 2380 | PyErr_Clear(); |
| 2381 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2382 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2383 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2384 | return NULL; |
| 2385 | Py_BEGIN_ALLOW_THREADS |
| 2386 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2387 | it is a simple dereference. */ |
| 2388 | res = CreateDirectoryA(path, NULL); |
| 2389 | Py_END_ALLOW_THREADS |
| 2390 | if (!res) { |
| 2391 | win32_error("mkdir", path); |
| 2392 | PyMem_Free(path); |
| 2393 | return NULL; |
| 2394 | } |
| 2395 | PyMem_Free(path); |
| 2396 | Py_INCREF(Py_None); |
| 2397 | return Py_None; |
| 2398 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2399 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2400 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2401 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2402 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2403 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2404 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2405 | res = mkdir(path); |
| 2406 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2407 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2408 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2409 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2410 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2411 | return posix_error_with_allocated_filename(path); |
| 2412 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2413 | Py_INCREF(Py_None); |
| 2414 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2415 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2418 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2419 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2420 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2421 | #include <sys/resource.h> |
| 2422 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2423 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2424 | |
| 2425 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2426 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2427 | "nice(inc) -> new_priority\n\n\ |
| 2428 | Decrease the priority of process by inc and return the new priority."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2429 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2430 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2431 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2432 | { |
| 2433 | int increment, value; |
| 2434 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2435 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2436 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2437 | |
| 2438 | /* There are two flavours of 'nice': one that returns the new |
| 2439 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2440 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2441 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2442 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2443 | If we are of the nice family that returns the new priority, we |
| 2444 | need to clear errno before the call, and check if errno is filled |
| 2445 | before calling posix_error() on a returnvalue of -1, because the |
| 2446 | -1 may be the actual new priority! */ |
| 2447 | |
| 2448 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2449 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2450 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2451 | if (value == 0) |
| 2452 | value = getpriority(PRIO_PROCESS, 0); |
| 2453 | #endif |
| 2454 | if (value == -1 && errno != 0) |
| 2455 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2456 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2457 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2458 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2459 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2460 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2461 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2462 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2463 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2464 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2465 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2466 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2467 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2468 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2469 | PyObject *o1, *o2; |
| 2470 | char *p1, *p2; |
| 2471 | BOOL result; |
| 2472 | if (unicode_file_names()) { |
| 2473 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2474 | convert_to_unicode, &o1, |
| 2475 | convert_to_unicode, &o2)) |
| 2476 | PyErr_Clear(); |
| 2477 | else { |
| 2478 | Py_BEGIN_ALLOW_THREADS |
| 2479 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2480 | PyUnicode_AsUnicode(o2)); |
| 2481 | Py_END_ALLOW_THREADS |
| 2482 | Py_DECREF(o1); |
| 2483 | Py_DECREF(o2); |
| 2484 | if (!result) |
| 2485 | return win32_error("rename", NULL); |
| 2486 | Py_INCREF(Py_None); |
| 2487 | return Py_None; |
| 2488 | } |
| 2489 | } |
| 2490 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2491 | return NULL; |
| 2492 | Py_BEGIN_ALLOW_THREADS |
| 2493 | result = MoveFileA(p1, p2); |
| 2494 | Py_END_ALLOW_THREADS |
| 2495 | if (!result) |
| 2496 | return win32_error("rename", NULL); |
| 2497 | Py_INCREF(Py_None); |
| 2498 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2499 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2500 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2501 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2504 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2505 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2506 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2507 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2508 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2509 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2510 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2511 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2512 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2513 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2514 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2515 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2516 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2520 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2521 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2522 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2523 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2524 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2525 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2526 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2527 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2528 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2529 | #else |
| 2530 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2531 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2534 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2535 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2536 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2537 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2538 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2539 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2540 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2541 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2542 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2543 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2544 | #ifdef MS_WINDOWS |
| 2545 | wchar_t *command; |
| 2546 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2547 | return NULL; |
| 2548 | #else |
| 2549 | char *command; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2550 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2551 | return NULL; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2552 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2553 | Py_BEGIN_ALLOW_THREADS |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2554 | #ifdef MS_WINDOWS |
| 2555 | sts = _wsystem(command); |
| 2556 | #else |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2557 | sts = system(command); |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2558 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2559 | Py_END_ALLOW_THREADS |
| 2560 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2561 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2562 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2563 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2564 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2565 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2566 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2567 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2568 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2569 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2570 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2571 | { |
| 2572 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2573 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2574 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2575 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2576 | if (i < 0) |
| 2577 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2578 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2581 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2582 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2583 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2584 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2585 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2586 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2587 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2588 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2590 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2591 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2592 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2593 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2594 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2595 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2596 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2597 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2600 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2601 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2602 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2603 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2604 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2605 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2606 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2607 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2608 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2609 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2610 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2611 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2612 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2613 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2614 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2615 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2616 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2617 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2618 | u.sysname, |
| 2619 | u.nodename, |
| 2620 | u.release, |
| 2621 | u.version, |
| 2622 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2623 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2624 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2625 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2626 | static int |
| 2627 | extract_time(PyObject *t, long* sec, long* usec) |
| 2628 | { |
| 2629 | long intval; |
| 2630 | if (PyFloat_Check(t)) { |
| 2631 | double tval = PyFloat_AsDouble(t); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2632 | 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] | 2633 | if (!intobj) |
| 2634 | return -1; |
| 2635 | intval = PyInt_AsLong(intobj); |
| 2636 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2637 | if (intval == -1 && PyErr_Occurred()) |
| 2638 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2639 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2640 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2641 | if (*usec < 0) |
| 2642 | /* If rounding gave us a negative number, |
| 2643 | truncate. */ |
| 2644 | *usec = 0; |
| 2645 | return 0; |
| 2646 | } |
| 2647 | intval = PyInt_AsLong(t); |
| 2648 | if (intval == -1 && PyErr_Occurred()) |
| 2649 | return -1; |
| 2650 | *sec = intval; |
| 2651 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2652 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2653 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2655 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2656 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2657 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2658 | 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] | 2659 | 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] | 2660 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2661 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2662 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2663 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2664 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2665 | PyObject *arg; |
| 2666 | PyUnicodeObject *obwpath; |
| 2667 | wchar_t *wpath = NULL; |
| 2668 | char *apath = NULL; |
| 2669 | HANDLE hFile; |
| 2670 | long atimesec, mtimesec, ausec, musec; |
| 2671 | FILETIME atime, mtime; |
| 2672 | PyObject *result = NULL; |
| 2673 | |
| 2674 | if (unicode_file_names()) { |
| 2675 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2676 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2677 | Py_BEGIN_ALLOW_THREADS |
| 2678 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2679 | NULL, OPEN_EXISTING, |
| 2680 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2681 | Py_END_ALLOW_THREADS |
| 2682 | if (hFile == INVALID_HANDLE_VALUE) |
| 2683 | return win32_error_unicode("utime", wpath); |
| 2684 | } else |
| 2685 | /* Drop the argument parsing error as narrow strings |
| 2686 | are also valid. */ |
| 2687 | PyErr_Clear(); |
| 2688 | } |
| 2689 | if (!wpath) { |
| 2690 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2691 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2692 | return NULL; |
| 2693 | Py_BEGIN_ALLOW_THREADS |
| 2694 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2695 | NULL, OPEN_EXISTING, |
| 2696 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2697 | Py_END_ALLOW_THREADS |
| 2698 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2699 | win32_error("utime", apath); |
| 2700 | PyMem_Free(apath); |
| 2701 | return NULL; |
| 2702 | } |
| 2703 | PyMem_Free(apath); |
| 2704 | } |
| 2705 | |
| 2706 | if (arg == Py_None) { |
| 2707 | SYSTEMTIME now; |
| 2708 | GetSystemTime(&now); |
| 2709 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2710 | !SystemTimeToFileTime(&now, &atime)) { |
| 2711 | win32_error("utime", NULL); |
| 2712 | goto done; |
| 2713 | } |
| 2714 | } |
| 2715 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2716 | PyErr_SetString(PyExc_TypeError, |
| 2717 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2718 | goto done; |
| 2719 | } |
| 2720 | else { |
| 2721 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2722 | &atimesec, &ausec) == -1) |
| 2723 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2724 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2725 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2726 | &mtimesec, &musec) == -1) |
| 2727 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2728 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2729 | } |
| 2730 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2731 | /* Avoid putting the file name into the error here, |
| 2732 | as that may confuse the user into believing that |
| 2733 | something is wrong with the file, when it also |
| 2734 | could be the time stamp that gives a problem. */ |
| 2735 | win32_error("utime", NULL); |
| 2736 | } |
| 2737 | Py_INCREF(Py_None); |
| 2738 | result = Py_None; |
| 2739 | done: |
| 2740 | CloseHandle(hFile); |
| 2741 | return result; |
| 2742 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2743 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2744 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2745 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2746 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2747 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2748 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2749 | #if defined(HAVE_UTIMES) |
| 2750 | struct timeval buf[2]; |
| 2751 | #define ATIME buf[0].tv_sec |
| 2752 | #define MTIME buf[1].tv_sec |
| 2753 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2754 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2755 | struct utimbuf buf; |
| 2756 | #define ATIME buf.actime |
| 2757 | #define MTIME buf.modtime |
| 2758 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2759 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2760 | time_t buf[2]; |
| 2761 | #define ATIME buf[0] |
| 2762 | #define MTIME buf[1] |
| 2763 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2764 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2765 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2766 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2767 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2768 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2769 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2770 | if (arg == Py_None) { |
| 2771 | /* optional time values not given */ |
| 2772 | Py_BEGIN_ALLOW_THREADS |
| 2773 | res = utime(path, NULL); |
| 2774 | Py_END_ALLOW_THREADS |
| 2775 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2776 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2777 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2778 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2779 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2780 | return NULL; |
| 2781 | } |
| 2782 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2783 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2784 | &atime, &ausec) == -1) { |
| 2785 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2786 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2787 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2788 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2789 | &mtime, &musec) == -1) { |
| 2790 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2791 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2792 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2793 | ATIME = atime; |
| 2794 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2795 | #ifdef HAVE_UTIMES |
| 2796 | buf[0].tv_usec = ausec; |
| 2797 | buf[1].tv_usec = musec; |
| 2798 | Py_BEGIN_ALLOW_THREADS |
| 2799 | res = utimes(path, buf); |
| 2800 | Py_END_ALLOW_THREADS |
| 2801 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2802 | Py_BEGIN_ALLOW_THREADS |
| 2803 | res = utime(path, UTIME_ARG); |
| 2804 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2805 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2806 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2807 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2808 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2809 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2810 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2811 | Py_INCREF(Py_None); |
| 2812 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2813 | #undef UTIME_ARG |
| 2814 | #undef ATIME |
| 2815 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2816 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2819 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2820 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2821 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2822 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2823 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2824 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2825 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2826 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2827 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2828 | { |
| 2829 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2830 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2831 | return NULL; |
| 2832 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2833 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2836 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2837 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2838 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2839 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2840 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2841 | for (i = 0; i < count; i++) |
| 2842 | PyMem_Free(array[i]); |
| 2843 | PyMem_DEL(array); |
| 2844 | } |
| 2845 | #endif |
| 2846 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2847 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2848 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2849 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2850 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2851 | Execute an executable path with arguments, replacing current process.\n\ |
| 2852 | \n\ |
| 2853 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2854 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2855 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2856 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2857 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2858 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2859 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2860 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2861 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2862 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2863 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2864 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2865 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2866 | argv is a list or tuple of strings. */ |
| 2867 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2868 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2869 | Py_FileSystemDefaultEncoding, |
| 2870 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2871 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2872 | if (PyList_Check(argv)) { |
| 2873 | argc = PyList_Size(argv); |
| 2874 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2875 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2876 | else if (PyTuple_Check(argv)) { |
| 2877 | argc = PyTuple_Size(argv); |
| 2878 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2879 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2880 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2881 | 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] | 2882 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2883 | return NULL; |
| 2884 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 2885 | if (argc < 1) { |
| 2886 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 2887 | PyMem_Free(path); |
| 2888 | return NULL; |
| 2889 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2890 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2891 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2892 | if (argvlist == NULL) { |
| 2893 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2894 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2895 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2896 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2897 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2898 | Py_FileSystemDefaultEncoding, |
| 2899 | &argvlist[i])) { |
| 2900 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2901 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2902 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2903 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2904 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2905 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2906 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2907 | } |
| 2908 | argvlist[argc] = NULL; |
| 2909 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2910 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2911 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2912 | /* If we get here it's definitely an error */ |
| 2913 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2914 | free_string_array(argvlist, argc); |
| 2915 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2916 | return posix_error(); |
| 2917 | } |
| 2918 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2919 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2920 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2921 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2922 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2923 | \n\ |
| 2924 | path: path of executable file\n\ |
| 2925 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2926 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2927 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2928 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2929 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2930 | { |
| 2931 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2932 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2933 | char **argvlist; |
| 2934 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2935 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2936 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2937 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2938 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2939 | |
| 2940 | /* execve has three arguments: (path, argv, env), where |
| 2941 | argv is a list or tuple of strings and env is a dictionary |
| 2942 | like posix.environ. */ |
| 2943 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2944 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2945 | Py_FileSystemDefaultEncoding, |
| 2946 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2947 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2948 | if (PyList_Check(argv)) { |
| 2949 | argc = PyList_Size(argv); |
| 2950 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2951 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2952 | else if (PyTuple_Check(argv)) { |
| 2953 | argc = PyTuple_Size(argv); |
| 2954 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2955 | } |
| 2956 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2957 | PyErr_SetString(PyExc_TypeError, |
| 2958 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2959 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2960 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2961 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2962 | PyErr_SetString(PyExc_TypeError, |
| 2963 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2964 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2967 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2968 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2969 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2970 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2971 | } |
| 2972 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2973 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2974 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2975 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2976 | &argvlist[i])) |
| 2977 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2978 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2979 | goto fail_1; |
| 2980 | } |
| 2981 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2982 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2983 | argvlist[argc] = NULL; |
| 2984 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2985 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2986 | if (i < 0) |
| 2987 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2988 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2989 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2990 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2991 | goto fail_1; |
| 2992 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2993 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2994 | keys = PyMapping_Keys(env); |
| 2995 | vals = PyMapping_Values(env); |
| 2996 | if (!keys || !vals) |
| 2997 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2998 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2999 | PyErr_SetString(PyExc_TypeError, |
| 3000 | "execve(): env.keys() or env.values() is not a list"); |
| 3001 | goto fail_2; |
| 3002 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3003 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3004 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3005 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3006 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3007 | |
| 3008 | key = PyList_GetItem(keys, pos); |
| 3009 | val = PyList_GetItem(vals, pos); |
| 3010 | if (!key || !val) |
| 3011 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3012 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3013 | if (!PyArg_Parse( |
| 3014 | key, |
| 3015 | "s;execve() arg 3 contains a non-string key", |
| 3016 | &k) || |
| 3017 | !PyArg_Parse( |
| 3018 | val, |
| 3019 | "s;execve() arg 3 contains a non-string value", |
| 3020 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3021 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3022 | goto fail_2; |
| 3023 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3024 | |
| 3025 | #if defined(PYOS_OS2) |
| 3026 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3027 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3028 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3029 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3030 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3031 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3032 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3033 | goto fail_2; |
| 3034 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3035 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3036 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3037 | #if defined(PYOS_OS2) |
| 3038 | } |
| 3039 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3040 | } |
| 3041 | envlist[envc] = 0; |
| 3042 | |
| 3043 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3044 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3045 | /* If we get here it's definitely an error */ |
| 3046 | |
| 3047 | (void) posix_error(); |
| 3048 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3049 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3050 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3051 | PyMem_DEL(envlist[envc]); |
| 3052 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3053 | fail_1: |
| 3054 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3055 | Py_XDECREF(vals); |
| 3056 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3057 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3058 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3059 | return NULL; |
| 3060 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3061 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3062 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3063 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3064 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3065 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3066 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3067 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3068 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3069 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3070 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3071 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3072 | |
| 3073 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3074 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3075 | { |
| 3076 | char *path; |
| 3077 | PyObject *argv; |
| 3078 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3079 | int mode, i; |
| 3080 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3081 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3082 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3083 | |
| 3084 | /* spawnv has three arguments: (mode, path, argv), where |
| 3085 | argv is a list or tuple of strings. */ |
| 3086 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3087 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3088 | Py_FileSystemDefaultEncoding, |
| 3089 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3090 | return NULL; |
| 3091 | if (PyList_Check(argv)) { |
| 3092 | argc = PyList_Size(argv); |
| 3093 | getitem = PyList_GetItem; |
| 3094 | } |
| 3095 | else if (PyTuple_Check(argv)) { |
| 3096 | argc = PyTuple_Size(argv); |
| 3097 | getitem = PyTuple_GetItem; |
| 3098 | } |
| 3099 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3100 | PyErr_SetString(PyExc_TypeError, |
| 3101 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3102 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3103 | return NULL; |
| 3104 | } |
| 3105 | |
| 3106 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3107 | if (argvlist == NULL) { |
| 3108 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3109 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3110 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3111 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3112 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3113 | Py_FileSystemDefaultEncoding, |
| 3114 | &argvlist[i])) { |
| 3115 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3116 | PyErr_SetString( |
| 3117 | PyExc_TypeError, |
| 3118 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3119 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3120 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | argvlist[argc] = NULL; |
| 3124 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3125 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3126 | Py_BEGIN_ALLOW_THREADS |
| 3127 | spawnval = spawnv(mode, path, argvlist); |
| 3128 | Py_END_ALLOW_THREADS |
| 3129 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3130 | if (mode == _OLD_P_OVERLAY) |
| 3131 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3132 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3133 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3134 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3135 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3136 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3137 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3138 | free_string_array(argvlist, argc); |
| 3139 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3140 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3141 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3142 | return posix_error(); |
| 3143 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3144 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3145 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3146 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3147 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3148 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3152 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3153 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3154 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3155 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3156 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3157 | path: path of executable file\n\ |
| 3158 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3159 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3160 | |
| 3161 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3162 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3163 | { |
| 3164 | char *path; |
| 3165 | PyObject *argv, *env; |
| 3166 | char **argvlist; |
| 3167 | char **envlist; |
| 3168 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3169 | int mode, pos, envc; |
| 3170 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3171 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3172 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3173 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3174 | |
| 3175 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3176 | argv is a list or tuple of strings and env is a dictionary |
| 3177 | like posix.environ. */ |
| 3178 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3179 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3180 | Py_FileSystemDefaultEncoding, |
| 3181 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3182 | return NULL; |
| 3183 | if (PyList_Check(argv)) { |
| 3184 | argc = PyList_Size(argv); |
| 3185 | getitem = PyList_GetItem; |
| 3186 | } |
| 3187 | else if (PyTuple_Check(argv)) { |
| 3188 | argc = PyTuple_Size(argv); |
| 3189 | getitem = PyTuple_GetItem; |
| 3190 | } |
| 3191 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3192 | PyErr_SetString(PyExc_TypeError, |
| 3193 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3194 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3195 | } |
| 3196 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3197 | PyErr_SetString(PyExc_TypeError, |
| 3198 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3199 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | argvlist = PyMem_NEW(char *, argc+1); |
| 3203 | if (argvlist == NULL) { |
| 3204 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3205 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3206 | } |
| 3207 | for (i = 0; i < argc; i++) { |
| 3208 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3209 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3210 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3211 | &argvlist[i])) |
| 3212 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3213 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3214 | goto fail_1; |
| 3215 | } |
| 3216 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3217 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3218 | argvlist[argc] = NULL; |
| 3219 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3220 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3221 | if (i < 0) |
| 3222 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3223 | envlist = PyMem_NEW(char *, i + 1); |
| 3224 | if (envlist == NULL) { |
| 3225 | PyErr_NoMemory(); |
| 3226 | goto fail_1; |
| 3227 | } |
| 3228 | envc = 0; |
| 3229 | keys = PyMapping_Keys(env); |
| 3230 | vals = PyMapping_Values(env); |
| 3231 | if (!keys || !vals) |
| 3232 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3233 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3234 | PyErr_SetString(PyExc_TypeError, |
| 3235 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3236 | goto fail_2; |
| 3237 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3238 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3239 | for (pos = 0; pos < i; pos++) { |
| 3240 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3241 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3242 | |
| 3243 | key = PyList_GetItem(keys, pos); |
| 3244 | val = PyList_GetItem(vals, pos); |
| 3245 | if (!key || !val) |
| 3246 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3247 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3248 | if (!PyArg_Parse( |
| 3249 | key, |
| 3250 | "s;spawnve() arg 3 contains a non-string key", |
| 3251 | &k) || |
| 3252 | !PyArg_Parse( |
| 3253 | val, |
| 3254 | "s;spawnve() arg 3 contains a non-string value", |
| 3255 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3256 | { |
| 3257 | goto fail_2; |
| 3258 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3259 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3260 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3261 | if (p == NULL) { |
| 3262 | PyErr_NoMemory(); |
| 3263 | goto fail_2; |
| 3264 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3265 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3266 | envlist[envc++] = p; |
| 3267 | } |
| 3268 | envlist[envc] = 0; |
| 3269 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3270 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3271 | Py_BEGIN_ALLOW_THREADS |
| 3272 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3273 | Py_END_ALLOW_THREADS |
| 3274 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3275 | if (mode == _OLD_P_OVERLAY) |
| 3276 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3277 | |
| 3278 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3279 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3280 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3281 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3282 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3283 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3284 | (void) posix_error(); |
| 3285 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3286 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3287 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3288 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3289 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3290 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3291 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3292 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3293 | while (--envc >= 0) |
| 3294 | PyMem_DEL(envlist[envc]); |
| 3295 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3296 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3297 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3298 | Py_XDECREF(vals); |
| 3299 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3300 | fail_0: |
| 3301 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3302 | return res; |
| 3303 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3304 | |
| 3305 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3306 | #if defined(PYOS_OS2) |
| 3307 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3308 | "spawnvp(mode, file, args)\n\n\ |
| 3309 | Execute the program 'file' in a new process, using the environment\n\ |
| 3310 | search path to find the file.\n\ |
| 3311 | \n\ |
| 3312 | mode: mode of process creation\n\ |
| 3313 | file: executable file name\n\ |
| 3314 | args: tuple or list of strings"); |
| 3315 | |
| 3316 | static PyObject * |
| 3317 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3318 | { |
| 3319 | char *path; |
| 3320 | PyObject *argv; |
| 3321 | char **argvlist; |
| 3322 | int mode, i, argc; |
| 3323 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3324 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3325 | |
| 3326 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3327 | argv is a list or tuple of strings. */ |
| 3328 | |
| 3329 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3330 | Py_FileSystemDefaultEncoding, |
| 3331 | &path, &argv)) |
| 3332 | return NULL; |
| 3333 | if (PyList_Check(argv)) { |
| 3334 | argc = PyList_Size(argv); |
| 3335 | getitem = PyList_GetItem; |
| 3336 | } |
| 3337 | else if (PyTuple_Check(argv)) { |
| 3338 | argc = PyTuple_Size(argv); |
| 3339 | getitem = PyTuple_GetItem; |
| 3340 | } |
| 3341 | else { |
| 3342 | PyErr_SetString(PyExc_TypeError, |
| 3343 | "spawnvp() arg 2 must be a tuple or list"); |
| 3344 | PyMem_Free(path); |
| 3345 | return NULL; |
| 3346 | } |
| 3347 | |
| 3348 | argvlist = PyMem_NEW(char *, argc+1); |
| 3349 | if (argvlist == NULL) { |
| 3350 | PyMem_Free(path); |
| 3351 | return PyErr_NoMemory(); |
| 3352 | } |
| 3353 | for (i = 0; i < argc; i++) { |
| 3354 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3355 | Py_FileSystemDefaultEncoding, |
| 3356 | &argvlist[i])) { |
| 3357 | free_string_array(argvlist, i); |
| 3358 | PyErr_SetString( |
| 3359 | PyExc_TypeError, |
| 3360 | "spawnvp() arg 2 must contain only strings"); |
| 3361 | PyMem_Free(path); |
| 3362 | return NULL; |
| 3363 | } |
| 3364 | } |
| 3365 | argvlist[argc] = NULL; |
| 3366 | |
| 3367 | Py_BEGIN_ALLOW_THREADS |
| 3368 | #if defined(PYCC_GCC) |
| 3369 | spawnval = spawnvp(mode, path, argvlist); |
| 3370 | #else |
| 3371 | spawnval = _spawnvp(mode, path, argvlist); |
| 3372 | #endif |
| 3373 | Py_END_ALLOW_THREADS |
| 3374 | |
| 3375 | free_string_array(argvlist, argc); |
| 3376 | PyMem_Free(path); |
| 3377 | |
| 3378 | if (spawnval == -1) |
| 3379 | return posix_error(); |
| 3380 | else |
| 3381 | return Py_BuildValue("l", (long) spawnval); |
| 3382 | } |
| 3383 | |
| 3384 | |
| 3385 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3386 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3387 | Execute the program 'file' in a new process, using the environment\n\ |
| 3388 | search path to find the file.\n\ |
| 3389 | \n\ |
| 3390 | mode: mode of process creation\n\ |
| 3391 | file: executable file name\n\ |
| 3392 | args: tuple or list of arguments\n\ |
| 3393 | env: dictionary of strings mapping to strings"); |
| 3394 | |
| 3395 | static PyObject * |
| 3396 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3397 | { |
| 3398 | char *path; |
| 3399 | PyObject *argv, *env; |
| 3400 | char **argvlist; |
| 3401 | char **envlist; |
| 3402 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3403 | int mode, i, pos, argc, envc; |
| 3404 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3405 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3406 | int lastarg = 0; |
| 3407 | |
| 3408 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3409 | argv is a list or tuple of strings and env is a dictionary |
| 3410 | like posix.environ. */ |
| 3411 | |
| 3412 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3413 | Py_FileSystemDefaultEncoding, |
| 3414 | &path, &argv, &env)) |
| 3415 | return NULL; |
| 3416 | if (PyList_Check(argv)) { |
| 3417 | argc = PyList_Size(argv); |
| 3418 | getitem = PyList_GetItem; |
| 3419 | } |
| 3420 | else if (PyTuple_Check(argv)) { |
| 3421 | argc = PyTuple_Size(argv); |
| 3422 | getitem = PyTuple_GetItem; |
| 3423 | } |
| 3424 | else { |
| 3425 | PyErr_SetString(PyExc_TypeError, |
| 3426 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3427 | goto fail_0; |
| 3428 | } |
| 3429 | if (!PyMapping_Check(env)) { |
| 3430 | PyErr_SetString(PyExc_TypeError, |
| 3431 | "spawnvpe() arg 3 must be a mapping object"); |
| 3432 | goto fail_0; |
| 3433 | } |
| 3434 | |
| 3435 | argvlist = PyMem_NEW(char *, argc+1); |
| 3436 | if (argvlist == NULL) { |
| 3437 | PyErr_NoMemory(); |
| 3438 | goto fail_0; |
| 3439 | } |
| 3440 | for (i = 0; i < argc; i++) { |
| 3441 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3442 | "et;spawnvpe() arg 2 must contain only strings", |
| 3443 | Py_FileSystemDefaultEncoding, |
| 3444 | &argvlist[i])) |
| 3445 | { |
| 3446 | lastarg = i; |
| 3447 | goto fail_1; |
| 3448 | } |
| 3449 | } |
| 3450 | lastarg = argc; |
| 3451 | argvlist[argc] = NULL; |
| 3452 | |
| 3453 | i = PyMapping_Size(env); |
| 3454 | if (i < 0) |
| 3455 | goto fail_1; |
| 3456 | envlist = PyMem_NEW(char *, i + 1); |
| 3457 | if (envlist == NULL) { |
| 3458 | PyErr_NoMemory(); |
| 3459 | goto fail_1; |
| 3460 | } |
| 3461 | envc = 0; |
| 3462 | keys = PyMapping_Keys(env); |
| 3463 | vals = PyMapping_Values(env); |
| 3464 | if (!keys || !vals) |
| 3465 | goto fail_2; |
| 3466 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3467 | PyErr_SetString(PyExc_TypeError, |
| 3468 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3469 | goto fail_2; |
| 3470 | } |
| 3471 | |
| 3472 | for (pos = 0; pos < i; pos++) { |
| 3473 | char *p, *k, *v; |
| 3474 | size_t len; |
| 3475 | |
| 3476 | key = PyList_GetItem(keys, pos); |
| 3477 | val = PyList_GetItem(vals, pos); |
| 3478 | if (!key || !val) |
| 3479 | goto fail_2; |
| 3480 | |
| 3481 | if (!PyArg_Parse( |
| 3482 | key, |
| 3483 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3484 | &k) || |
| 3485 | !PyArg_Parse( |
| 3486 | val, |
| 3487 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3488 | &v)) |
| 3489 | { |
| 3490 | goto fail_2; |
| 3491 | } |
| 3492 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3493 | p = PyMem_NEW(char, len); |
| 3494 | if (p == NULL) { |
| 3495 | PyErr_NoMemory(); |
| 3496 | goto fail_2; |
| 3497 | } |
| 3498 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3499 | envlist[envc++] = p; |
| 3500 | } |
| 3501 | envlist[envc] = 0; |
| 3502 | |
| 3503 | Py_BEGIN_ALLOW_THREADS |
| 3504 | #if defined(PYCC_GCC) |
| 3505 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3506 | #else |
| 3507 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3508 | #endif |
| 3509 | Py_END_ALLOW_THREADS |
| 3510 | |
| 3511 | if (spawnval == -1) |
| 3512 | (void) posix_error(); |
| 3513 | else |
| 3514 | res = Py_BuildValue("l", (long) spawnval); |
| 3515 | |
| 3516 | fail_2: |
| 3517 | while (--envc >= 0) |
| 3518 | PyMem_DEL(envlist[envc]); |
| 3519 | PyMem_DEL(envlist); |
| 3520 | fail_1: |
| 3521 | free_string_array(argvlist, lastarg); |
| 3522 | Py_XDECREF(vals); |
| 3523 | Py_XDECREF(keys); |
| 3524 | fail_0: |
| 3525 | PyMem_Free(path); |
| 3526 | return res; |
| 3527 | } |
| 3528 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3529 | #endif /* HAVE_SPAWNV */ |
| 3530 | |
| 3531 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3532 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3533 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3534 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3535 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3536 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3537 | 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] | 3538 | |
| 3539 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3540 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3541 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3542 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3543 | if (pid == -1) |
| 3544 | return posix_error(); |
| 3545 | PyOS_AfterFork(); |
| 3546 | return PyInt_FromLong((long)pid); |
| 3547 | } |
| 3548 | #endif |
| 3549 | |
| 3550 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3551 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3552 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3553 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3554 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3555 | 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] | 3556 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3557 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3558 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3559 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3560 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3561 | if (pid == -1) |
| 3562 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3563 | if (pid == 0) |
| 3564 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3565 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3566 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3567 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3568 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3569 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3570 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3571 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3572 | #define DEV_PTY_FILE "/dev/ptc" |
| 3573 | #define HAVE_DEV_PTMX |
| 3574 | #else |
| 3575 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3576 | #endif |
| 3577 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3578 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3579 | #ifdef HAVE_PTY_H |
| 3580 | #include <pty.h> |
| 3581 | #else |
| 3582 | #ifdef HAVE_LIBUTIL_H |
| 3583 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3584 | #endif /* HAVE_LIBUTIL_H */ |
| 3585 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3586 | #ifdef HAVE_STROPTS_H |
| 3587 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3588 | #endif |
| 3589 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3590 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3591 | #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] | 3592 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3593 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3594 | 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] | 3595 | |
| 3596 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3597 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3598 | { |
| 3599 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3600 | #ifndef HAVE_OPENPTY |
| 3601 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3602 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3603 | #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] | 3604 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3605 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3606 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3607 | #endif |
| 3608 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3609 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3610 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3611 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3612 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3613 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3614 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3615 | if (slave_name == NULL) |
| 3616 | return posix_error(); |
| 3617 | |
| 3618 | slave_fd = open(slave_name, O_RDWR); |
| 3619 | if (slave_fd < 0) |
| 3620 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3621 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3622 | 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] | 3623 | if (master_fd < 0) |
| 3624 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3625 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3626 | /* change permission of slave */ |
| 3627 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3628 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3629 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3630 | } |
| 3631 | /* unlock slave */ |
| 3632 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3633 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3634 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3635 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3636 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3637 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3638 | if (slave_name == NULL) |
| 3639 | return posix_error(); |
| 3640 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3641 | if (slave_fd < 0) |
| 3642 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3643 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3644 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3645 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3646 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3647 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3648 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3649 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3650 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3651 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3652 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3653 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3654 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3655 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3656 | |
| 3657 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3658 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3659 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3660 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3661 | 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] | 3662 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3663 | |
| 3664 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3665 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3666 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3667 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3668 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3669 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3670 | if (pid == -1) |
| 3671 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3672 | if (pid == 0) |
| 3673 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3674 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3675 | } |
| 3676 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3677 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3678 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3679 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3680 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3681 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3682 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3683 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3684 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3685 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3686 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3687 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3688 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3689 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3690 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3691 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3692 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3693 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3694 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3695 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3696 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3697 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3698 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3699 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3700 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3701 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3702 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3703 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3704 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3705 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3706 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3707 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3708 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3709 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3710 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3711 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3712 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3713 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3714 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3715 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3717 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3718 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3719 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3721 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3722 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3723 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3724 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3727 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3728 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3729 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3730 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3731 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3732 | |
| 3733 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3734 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3735 | { |
| 3736 | PyObject *result = NULL; |
| 3737 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3738 | #ifdef NGROUPS_MAX |
| 3739 | #define MAX_GROUPS NGROUPS_MAX |
| 3740 | #else |
| 3741 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3742 | #define MAX_GROUPS 64 |
| 3743 | #endif |
| 3744 | gid_t grouplist[MAX_GROUPS]; |
| 3745 | int n; |
| 3746 | |
| 3747 | n = getgroups(MAX_GROUPS, grouplist); |
| 3748 | if (n < 0) |
| 3749 | posix_error(); |
| 3750 | else { |
| 3751 | result = PyList_New(n); |
| 3752 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3753 | int i; |
| 3754 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3755 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3756 | if (o == NULL) { |
| 3757 | Py_DECREF(result); |
| 3758 | result = NULL; |
| 3759 | break; |
| 3760 | } |
| 3761 | PyList_SET_ITEM(result, i, o); |
| 3762 | } |
| 3763 | } |
| 3764 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3765 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3766 | return result; |
| 3767 | } |
| 3768 | #endif |
| 3769 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3770 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3771 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3772 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3773 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3774 | |
| 3775 | static PyObject * |
| 3776 | posix_getpgid(PyObject *self, PyObject *args) |
| 3777 | { |
| 3778 | int pid, pgid; |
| 3779 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3780 | return NULL; |
| 3781 | pgid = getpgid(pid); |
| 3782 | if (pgid < 0) |
| 3783 | return posix_error(); |
| 3784 | return PyInt_FromLong((long)pgid); |
| 3785 | } |
| 3786 | #endif /* HAVE_GETPGID */ |
| 3787 | |
| 3788 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3789 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3790 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3791 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3792 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3793 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3794 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3795 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3796 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3797 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3798 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3799 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3800 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3801 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3802 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3803 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3805 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3806 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3807 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3808 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3809 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3810 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3811 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3812 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3813 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3814 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3815 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3816 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3817 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3818 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3819 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3820 | Py_INCREF(Py_None); |
| 3821 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3824 | #endif /* HAVE_SETPGRP */ |
| 3825 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3826 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3827 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3828 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3829 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3830 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3831 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3832 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3833 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3834 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3835 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3836 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3837 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3838 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3839 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3840 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3841 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3842 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3843 | |
| 3844 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3845 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3846 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3847 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3848 | char *name; |
| 3849 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3850 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3851 | errno = 0; |
| 3852 | name = getlogin(); |
| 3853 | if (name == NULL) { |
| 3854 | if (errno) |
| 3855 | posix_error(); |
| 3856 | else |
| 3857 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3858 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3859 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3860 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 3861 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3862 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3863 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3864 | return result; |
| 3865 | } |
| 3866 | #endif |
| 3867 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3868 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3869 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3870 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3871 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3872 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3873 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3874 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3875 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3876 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3877 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3878 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3879 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3880 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3881 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3882 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3883 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3884 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3885 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3886 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3887 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3888 | { |
| 3889 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3890 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3891 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3892 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3893 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3894 | APIRET rc; |
| 3895 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3896 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3897 | |
| 3898 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3899 | APIRET rc; |
| 3900 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3901 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3902 | |
| 3903 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3904 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3905 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3906 | if (kill(pid, sig) == -1) |
| 3907 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3908 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3909 | Py_INCREF(Py_None); |
| 3910 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3911 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3912 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3913 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3914 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3915 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3916 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3917 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3918 | |
| 3919 | static PyObject * |
| 3920 | posix_killpg(PyObject *self, PyObject *args) |
| 3921 | { |
| 3922 | int pgid, sig; |
| 3923 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3924 | return NULL; |
| 3925 | if (killpg(pgid, sig) == -1) |
| 3926 | return posix_error(); |
| 3927 | Py_INCREF(Py_None); |
| 3928 | return Py_None; |
| 3929 | } |
| 3930 | #endif |
| 3931 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3932 | #ifdef HAVE_PLOCK |
| 3933 | |
| 3934 | #ifdef HAVE_SYS_LOCK_H |
| 3935 | #include <sys/lock.h> |
| 3936 | #endif |
| 3937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3938 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3939 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3940 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3941 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3942 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3943 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3944 | { |
| 3945 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3946 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3947 | return NULL; |
| 3948 | if (plock(op) == -1) |
| 3949 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3950 | Py_INCREF(Py_None); |
| 3951 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3952 | } |
| 3953 | #endif |
| 3954 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3955 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3956 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3957 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3958 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3959 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3960 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3961 | Set the current process's user id."); |
| 3962 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3963 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3964 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3965 | { |
| 3966 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3967 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3968 | return NULL; |
| 3969 | if (setuid(uid) < 0) |
| 3970 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3971 | Py_INCREF(Py_None); |
| 3972 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3973 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3974 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3975 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3976 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3977 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3978 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3979 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3980 | Set the current process's effective user id."); |
| 3981 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3982 | static PyObject * |
| 3983 | posix_seteuid (PyObject *self, PyObject *args) |
| 3984 | { |
| 3985 | int euid; |
| 3986 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3987 | return NULL; |
| 3988 | } else if (seteuid(euid) < 0) { |
| 3989 | return posix_error(); |
| 3990 | } else { |
| 3991 | Py_INCREF(Py_None); |
| 3992 | return Py_None; |
| 3993 | } |
| 3994 | } |
| 3995 | #endif /* HAVE_SETEUID */ |
| 3996 | |
| 3997 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3998 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3999 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4000 | Set the current process's effective group id."); |
| 4001 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4002 | static PyObject * |
| 4003 | posix_setegid (PyObject *self, PyObject *args) |
| 4004 | { |
| 4005 | int egid; |
| 4006 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4007 | return NULL; |
| 4008 | } else if (setegid(egid) < 0) { |
| 4009 | return posix_error(); |
| 4010 | } else { |
| 4011 | Py_INCREF(Py_None); |
| 4012 | return Py_None; |
| 4013 | } |
| 4014 | } |
| 4015 | #endif /* HAVE_SETEGID */ |
| 4016 | |
| 4017 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4018 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4019 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4020 | Set the current process's real and effective user ids."); |
| 4021 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4022 | static PyObject * |
| 4023 | posix_setreuid (PyObject *self, PyObject *args) |
| 4024 | { |
| 4025 | int ruid, euid; |
| 4026 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4027 | return NULL; |
| 4028 | } else if (setreuid(ruid, euid) < 0) { |
| 4029 | return posix_error(); |
| 4030 | } else { |
| 4031 | Py_INCREF(Py_None); |
| 4032 | return Py_None; |
| 4033 | } |
| 4034 | } |
| 4035 | #endif /* HAVE_SETREUID */ |
| 4036 | |
| 4037 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4038 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4039 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4040 | Set the current process's real and effective group ids."); |
| 4041 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4042 | static PyObject * |
| 4043 | posix_setregid (PyObject *self, PyObject *args) |
| 4044 | { |
| 4045 | int rgid, egid; |
| 4046 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4047 | return NULL; |
| 4048 | } else if (setregid(rgid, egid) < 0) { |
| 4049 | return posix_error(); |
| 4050 | } else { |
| 4051 | Py_INCREF(Py_None); |
| 4052 | return Py_None; |
| 4053 | } |
| 4054 | } |
| 4055 | #endif /* HAVE_SETREGID */ |
| 4056 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4057 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4058 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4059 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4060 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4061 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4062 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4063 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4064 | { |
| 4065 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4066 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4067 | return NULL; |
| 4068 | if (setgid(gid) < 0) |
| 4069 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4070 | Py_INCREF(Py_None); |
| 4071 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4072 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4073 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4074 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4075 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4076 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4077 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4078 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4079 | |
| 4080 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4081 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4082 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4083 | int i, len; |
| 4084 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4085 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4086 | if (!PySequence_Check(groups)) { |
| 4087 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4088 | return NULL; |
| 4089 | } |
| 4090 | len = PySequence_Size(groups); |
| 4091 | if (len > MAX_GROUPS) { |
| 4092 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4093 | return NULL; |
| 4094 | } |
| 4095 | for(i = 0; i < len; i++) { |
| 4096 | PyObject *elem; |
| 4097 | elem = PySequence_GetItem(groups, i); |
| 4098 | if (!elem) |
| 4099 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4100 | if (!PyLong_Check(elem)) { |
| 4101 | PyErr_SetString(PyExc_TypeError, |
| 4102 | "groups must be integers"); |
| 4103 | Py_DECREF(elem); |
| 4104 | return NULL; |
| 4105 | } else { |
| 4106 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4107 | if (PyErr_Occurred()) { |
| 4108 | PyErr_SetString(PyExc_TypeError, |
| 4109 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4110 | Py_DECREF(elem); |
| 4111 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4112 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4113 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4114 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4115 | if (grouplist[i] != x) { |
| 4116 | PyErr_SetString(PyExc_TypeError, |
| 4117 | "group id too big"); |
| 4118 | Py_DECREF(elem); |
| 4119 | return NULL; |
| 4120 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4121 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4122 | Py_DECREF(elem); |
| 4123 | } |
| 4124 | |
| 4125 | if (setgroups(len, grouplist) < 0) |
| 4126 | return posix_error(); |
| 4127 | Py_INCREF(Py_None); |
| 4128 | return Py_None; |
| 4129 | } |
| 4130 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4131 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4132 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4133 | static PyObject * |
| 4134 | wait_helper(int pid, int status, struct rusage *ru) |
| 4135 | { |
| 4136 | PyObject *result; |
| 4137 | static PyObject *struct_rusage; |
| 4138 | |
| 4139 | if (pid == -1) |
| 4140 | return posix_error(); |
| 4141 | |
| 4142 | if (struct_rusage == NULL) { |
| 4143 | PyObject *m = PyImport_ImportModule("resource"); |
| 4144 | if (m == NULL) |
| 4145 | return NULL; |
| 4146 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4147 | Py_DECREF(m); |
| 4148 | if (struct_rusage == NULL) |
| 4149 | return NULL; |
| 4150 | } |
| 4151 | |
| 4152 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4153 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4154 | if (!result) |
| 4155 | return NULL; |
| 4156 | |
| 4157 | #ifndef doubletime |
| 4158 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4159 | #endif |
| 4160 | |
| 4161 | PyStructSequence_SET_ITEM(result, 0, |
| 4162 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4163 | PyStructSequence_SET_ITEM(result, 1, |
| 4164 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4165 | #define SET_INT(result, index, value)\ |
| 4166 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 4167 | SET_INT(result, 2, ru->ru_maxrss); |
| 4168 | SET_INT(result, 3, ru->ru_ixrss); |
| 4169 | SET_INT(result, 4, ru->ru_idrss); |
| 4170 | SET_INT(result, 5, ru->ru_isrss); |
| 4171 | SET_INT(result, 6, ru->ru_minflt); |
| 4172 | SET_INT(result, 7, ru->ru_majflt); |
| 4173 | SET_INT(result, 8, ru->ru_nswap); |
| 4174 | SET_INT(result, 9, ru->ru_inblock); |
| 4175 | SET_INT(result, 10, ru->ru_oublock); |
| 4176 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4177 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4178 | SET_INT(result, 13, ru->ru_nsignals); |
| 4179 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4180 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4181 | #undef SET_INT |
| 4182 | |
| 4183 | if (PyErr_Occurred()) { |
| 4184 | Py_DECREF(result); |
| 4185 | return NULL; |
| 4186 | } |
| 4187 | |
| 4188 | return Py_BuildValue("iiN", pid, status, result); |
| 4189 | } |
| 4190 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4191 | |
| 4192 | #ifdef HAVE_WAIT3 |
| 4193 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4194 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4195 | Wait for completion of a child process."); |
| 4196 | |
| 4197 | static PyObject * |
| 4198 | posix_wait3(PyObject *self, PyObject *args) |
| 4199 | { |
| 4200 | int pid, options; |
| 4201 | struct rusage ru; |
| 4202 | WAIT_TYPE status; |
| 4203 | WAIT_STATUS_INT(status) = 0; |
| 4204 | |
| 4205 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4206 | return NULL; |
| 4207 | |
| 4208 | Py_BEGIN_ALLOW_THREADS |
| 4209 | pid = wait3(&status, options, &ru); |
| 4210 | Py_END_ALLOW_THREADS |
| 4211 | |
| 4212 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4213 | } |
| 4214 | #endif /* HAVE_WAIT3 */ |
| 4215 | |
| 4216 | #ifdef HAVE_WAIT4 |
| 4217 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4218 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4219 | Wait for completion of a given child process."); |
| 4220 | |
| 4221 | static PyObject * |
| 4222 | posix_wait4(PyObject *self, PyObject *args) |
| 4223 | { |
| 4224 | int pid, options; |
| 4225 | struct rusage ru; |
| 4226 | WAIT_TYPE status; |
| 4227 | WAIT_STATUS_INT(status) = 0; |
| 4228 | |
| 4229 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4230 | return NULL; |
| 4231 | |
| 4232 | Py_BEGIN_ALLOW_THREADS |
| 4233 | pid = wait4(pid, &status, options, &ru); |
| 4234 | Py_END_ALLOW_THREADS |
| 4235 | |
| 4236 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4237 | } |
| 4238 | #endif /* HAVE_WAIT4 */ |
| 4239 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4240 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4241 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4242 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4243 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4244 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4245 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4246 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4247 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4248 | int pid, options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4249 | WAIT_TYPE status; |
| 4250 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4251 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4252 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4253 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4254 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4255 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4256 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4257 | if (pid == -1) |
| 4258 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4259 | |
| 4260 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4263 | #elif defined(HAVE_CWAIT) |
| 4264 | |
| 4265 | /* 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] | 4266 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4267 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4268 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4269 | |
| 4270 | static PyObject * |
| 4271 | posix_waitpid(PyObject *self, PyObject *args) |
| 4272 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4273 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4274 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4275 | |
| 4276 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4277 | return NULL; |
| 4278 | Py_BEGIN_ALLOW_THREADS |
| 4279 | pid = _cwait(&status, pid, options); |
| 4280 | Py_END_ALLOW_THREADS |
| 4281 | if (pid == -1) |
| 4282 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4283 | |
| 4284 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4285 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4286 | } |
| 4287 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4288 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4289 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4290 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4291 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4292 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4293 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4294 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4295 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4296 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4297 | int pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4298 | WAIT_TYPE status; |
| 4299 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4300 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4301 | Py_BEGIN_ALLOW_THREADS |
| 4302 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4303 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4304 | if (pid == -1) |
| 4305 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4306 | |
| 4307 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4308 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4309 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4310 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4311 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4312 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4313 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4314 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4315 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4316 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4317 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4318 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4319 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4320 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4321 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4322 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4323 | 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] | 4324 | #else |
| 4325 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4326 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4327 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4330 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4331 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4332 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4333 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4334 | 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] | 4335 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4336 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4337 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4338 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4339 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4340 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4341 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4342 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4343 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4344 | |
| 4345 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 4346 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4347 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4348 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4349 | if (v == NULL) { |
| 4350 | PyMem_Free(path); |
| 4351 | return NULL; |
| 4352 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4353 | |
| 4354 | if (PyUnicode_Check(v)) { |
| 4355 | arg_is_unicode = 1; |
| 4356 | } |
| 4357 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4358 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4359 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4360 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4361 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4362 | if (n < 0) |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4363 | return posix_error_with_allocated_filename(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4364 | |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4365 | PyMem_Free(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4366 | v = PyString_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4367 | if (arg_is_unicode) { |
| 4368 | PyObject *w; |
| 4369 | |
| 4370 | w = PyUnicode_FromEncodedObject(v, |
| 4371 | Py_FileSystemDefaultEncoding, |
| 4372 | "strict"); |
| 4373 | if (w != NULL) { |
| 4374 | Py_DECREF(v); |
| 4375 | v = w; |
| 4376 | } |
| 4377 | else { |
| 4378 | /* fall back to the original byte string, as |
| 4379 | discussed in patch #683592 */ |
| 4380 | PyErr_Clear(); |
| 4381 | } |
| 4382 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4383 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4384 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4385 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4386 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4387 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4388 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4389 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4390 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4391 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4392 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4393 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4394 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4395 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4396 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4397 | } |
| 4398 | #endif /* HAVE_SYMLINK */ |
| 4399 | |
| 4400 | |
| 4401 | #ifdef HAVE_TIMES |
| 4402 | #ifndef HZ |
| 4403 | #define HZ 60 /* Universal constant :-) */ |
| 4404 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4405 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4406 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4407 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4408 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4409 | { |
| 4410 | ULONG value = 0; |
| 4411 | |
| 4412 | Py_BEGIN_ALLOW_THREADS |
| 4413 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4414 | Py_END_ALLOW_THREADS |
| 4415 | |
| 4416 | return value; |
| 4417 | } |
| 4418 | |
| 4419 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4420 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4421 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4422 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4423 | return Py_BuildValue("ddddd", |
| 4424 | (double)0 /* t.tms_utime / HZ */, |
| 4425 | (double)0 /* t.tms_stime / HZ */, |
| 4426 | (double)0 /* t.tms_cutime / HZ */, |
| 4427 | (double)0 /* t.tms_cstime / HZ */, |
| 4428 | (double)system_uptime() / 1000); |
| 4429 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4430 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4431 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4432 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4433 | { |
| 4434 | struct tms t; |
| 4435 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4436 | errno = 0; |
| 4437 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4438 | if (c == (clock_t) -1) |
| 4439 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4440 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4441 | (double)t.tms_utime / HZ, |
| 4442 | (double)t.tms_stime / HZ, |
| 4443 | (double)t.tms_cutime / HZ, |
| 4444 | (double)t.tms_cstime / HZ, |
| 4445 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4446 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4447 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4448 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4449 | |
| 4450 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4451 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4452 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4453 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4454 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4455 | { |
| 4456 | FILETIME create, exit, kernel, user; |
| 4457 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4458 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4459 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4460 | /* The fields of a FILETIME structure are the hi and lo part |
| 4461 | of a 64-bit value expressed in 100 nanosecond units. |
| 4462 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4463 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4464 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4465 | return Py_BuildValue( |
| 4466 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4467 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4468 | kernel.dwLowDateTime*1e-7), |
| 4469 | (double)(user.dwHighDateTime*429.4967296 + |
| 4470 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4471 | (double)0, |
| 4472 | (double)0, |
| 4473 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4474 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4475 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4476 | |
| 4477 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4478 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4479 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4480 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4481 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4482 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4483 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4484 | #ifdef HAVE_GETSID |
| 4485 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4486 | "getsid(pid) -> sid\n\n\ |
| 4487 | Call the system call getsid()."); |
| 4488 | |
| 4489 | static PyObject * |
| 4490 | posix_getsid(PyObject *self, PyObject *args) |
| 4491 | { |
| 4492 | int pid, sid; |
| 4493 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4494 | return NULL; |
| 4495 | sid = getsid(pid); |
| 4496 | if (sid < 0) |
| 4497 | return posix_error(); |
| 4498 | return PyInt_FromLong((long)sid); |
| 4499 | } |
| 4500 | #endif /* HAVE_GETSID */ |
| 4501 | |
| 4502 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4503 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4504 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4505 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4506 | Call the system call setsid()."); |
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 * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4509 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4510 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4511 | if (setsid() < 0) |
| 4512 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4513 | Py_INCREF(Py_None); |
| 4514 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4515 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4516 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4517 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4518 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4519 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4520 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4521 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4522 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4523 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4524 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4525 | { |
| 4526 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4527 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4528 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4529 | if (setpgid(pid, pgrp) < 0) |
| 4530 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4531 | Py_INCREF(Py_None); |
| 4532 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4533 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4534 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4535 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4536 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4537 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4538 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4539 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4540 | 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] | 4541 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4542 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4543 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4544 | { |
| 4545 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4546 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4547 | return NULL; |
| 4548 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4549 | if (pgid < 0) |
| 4550 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4551 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4552 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4553 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4554 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4555 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4556 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4557 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4558 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4559 | 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] | 4560 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4561 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4562 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4563 | { |
| 4564 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4565 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4566 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4567 | if (tcsetpgrp(fd, pgid) < 0) |
| 4568 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4569 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4570 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4571 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4572 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4573 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4574 | /* Functions acting on file descriptors */ |
| 4575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4576 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4577 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4578 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4579 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4580 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4581 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4582 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4583 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4584 | int flag; |
| 4585 | int mode = 0777; |
| 4586 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4587 | |
| 4588 | #ifdef MS_WINDOWS |
| 4589 | if (unicode_file_names()) { |
| 4590 | PyUnicodeObject *po; |
| 4591 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4592 | Py_BEGIN_ALLOW_THREADS |
| 4593 | /* PyUnicode_AS_UNICODE OK without thread |
| 4594 | lock as it is a simple dereference. */ |
| 4595 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4596 | Py_END_ALLOW_THREADS |
| 4597 | if (fd < 0) |
| 4598 | return posix_error(); |
| 4599 | return PyInt_FromLong((long)fd); |
| 4600 | } |
| 4601 | /* Drop the argument parsing error as narrow strings |
| 4602 | are also valid. */ |
| 4603 | PyErr_Clear(); |
| 4604 | } |
| 4605 | #endif |
| 4606 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4607 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4608 | Py_FileSystemDefaultEncoding, &file, |
| 4609 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4610 | return NULL; |
| 4611 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4612 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4613 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4614 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4615 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4616 | return posix_error_with_allocated_filename(file); |
| 4617 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4618 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4621 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4622 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4623 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4624 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4625 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4626 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4627 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4628 | { |
| 4629 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4630 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4631 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4632 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4633 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4634 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4635 | if (res < 0) |
| 4636 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4637 | Py_INCREF(Py_None); |
| 4638 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4641 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4642 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4643 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4644 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4645 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4646 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4647 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4648 | { |
| 4649 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4650 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4651 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4652 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4653 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4654 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4655 | if (fd < 0) |
| 4656 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4657 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4661 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4662 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4663 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4664 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4665 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4666 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4667 | { |
| 4668 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4669 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4670 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4671 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4672 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4673 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4674 | if (res < 0) |
| 4675 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4676 | Py_INCREF(Py_None); |
| 4677 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4680 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4681 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4682 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4683 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4684 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4685 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4686 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4687 | { |
| 4688 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4689 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4690 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4691 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4692 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4693 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4694 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4695 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4696 | return NULL; |
| 4697 | #ifdef SEEK_SET |
| 4698 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4699 | switch (how) { |
| 4700 | case 0: how = SEEK_SET; break; |
| 4701 | case 1: how = SEEK_CUR; break; |
| 4702 | case 2: how = SEEK_END; break; |
| 4703 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4704 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4705 | |
| 4706 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4707 | pos = PyInt_AsLong(posobj); |
| 4708 | #else |
| 4709 | pos = PyLong_Check(posobj) ? |
| 4710 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 4711 | #endif |
| 4712 | if (PyErr_Occurred()) |
| 4713 | return NULL; |
| 4714 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4715 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4716 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4717 | res = _lseeki64(fd, pos, how); |
| 4718 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4719 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4720 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4721 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4722 | if (res < 0) |
| 4723 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4724 | |
| 4725 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4726 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4727 | #else |
| 4728 | return PyLong_FromLongLong(res); |
| 4729 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4732 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4733 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4734 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4735 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4736 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4737 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4738 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4739 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4740 | int fd, size; |
| 4741 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4742 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4743 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4744 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 4745 | if (size < 0) { |
| 4746 | errno = EINVAL; |
| 4747 | return posix_error(); |
| 4748 | } |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4749 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4750 | if (buffer == NULL) |
| 4751 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4752 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4753 | n = read(fd, PyBytes_AsString(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4754 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4755 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4756 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4757 | return posix_error(); |
| 4758 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4759 | if (n != size) |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4760 | PyBytes_Resize(buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4761 | return buffer; |
| 4762 | } |
| 4763 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4765 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4766 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4767 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4768 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4769 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4770 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4771 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4772 | int fd; |
| 4773 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4774 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4775 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4776 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4777 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4778 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4779 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4780 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4781 | if (size < 0) |
| 4782 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4783 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4784 | } |
| 4785 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4786 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4787 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4788 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4789 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4790 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4791 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4792 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4793 | { |
| 4794 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4795 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4796 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4797 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4798 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 4799 | #ifdef __VMS |
| 4800 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 4801 | fsync(fd); |
| 4802 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4803 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4804 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4805 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4806 | if (res != 0) { |
| 4807 | #ifdef MS_WINDOWS |
| 4808 | return win32_error("fstat", NULL); |
| 4809 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4810 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4811 | #endif |
| 4812 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4813 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4814 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4817 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4818 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4819 | 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] | 4820 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4821 | |
| 4822 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4823 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4824 | { |
| 4825 | int fd; |
| 4826 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 4827 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4828 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4829 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4830 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4831 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4832 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4833 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4834 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4835 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4836 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4837 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4838 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4839 | #if defined(PYOS_OS2) |
| 4840 | HFILE read, write; |
| 4841 | APIRET rc; |
| 4842 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4843 | Py_BEGIN_ALLOW_THREADS |
| 4844 | rc = DosCreatePipe( &read, &write, 4096); |
| 4845 | Py_END_ALLOW_THREADS |
| 4846 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4847 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4848 | |
| 4849 | return Py_BuildValue("(ii)", read, write); |
| 4850 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4851 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4852 | int fds[2]; |
| 4853 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4854 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4855 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4856 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4857 | if (res != 0) |
| 4858 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4859 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4860 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4861 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4862 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4863 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4864 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4865 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4866 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4867 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4868 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 4869 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 4870 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4871 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4872 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4873 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4874 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4875 | #endif /* HAVE_PIPE */ |
| 4876 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4877 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4878 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4879 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4880 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4881 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4882 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4883 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4884 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4885 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4886 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4887 | int mode = 0666; |
| 4888 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4889 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4890 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4891 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4892 | res = mkfifo(filename, mode); |
| 4893 | Py_END_ALLOW_THREADS |
| 4894 | if (res < 0) |
| 4895 | return posix_error(); |
| 4896 | Py_INCREF(Py_None); |
| 4897 | return Py_None; |
| 4898 | } |
| 4899 | #endif |
| 4900 | |
| 4901 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 4902 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4903 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4904 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4905 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 4906 | named filename. mode specifies both the permissions to use and the\n\ |
| 4907 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 4908 | 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] | 4909 | device defines the newly created device special file (probably using\n\ |
| 4910 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4911 | |
| 4912 | |
| 4913 | static PyObject * |
| 4914 | posix_mknod(PyObject *self, PyObject *args) |
| 4915 | { |
| 4916 | char *filename; |
| 4917 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4918 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4919 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 4920 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4921 | return NULL; |
| 4922 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4923 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4924 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4925 | if (res < 0) |
| 4926 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4927 | Py_INCREF(Py_None); |
| 4928 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4929 | } |
| 4930 | #endif |
| 4931 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4932 | #ifdef HAVE_DEVICE_MACROS |
| 4933 | PyDoc_STRVAR(posix_major__doc__, |
| 4934 | "major(device) -> major number\n\ |
| 4935 | Extracts a device major number from a raw device number."); |
| 4936 | |
| 4937 | static PyObject * |
| 4938 | posix_major(PyObject *self, PyObject *args) |
| 4939 | { |
| 4940 | int device; |
| 4941 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 4942 | return NULL; |
| 4943 | return PyInt_FromLong((long)major(device)); |
| 4944 | } |
| 4945 | |
| 4946 | PyDoc_STRVAR(posix_minor__doc__, |
| 4947 | "minor(device) -> minor number\n\ |
| 4948 | Extracts a device minor number from a raw device number."); |
| 4949 | |
| 4950 | static PyObject * |
| 4951 | posix_minor(PyObject *self, PyObject *args) |
| 4952 | { |
| 4953 | int device; |
| 4954 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 4955 | return NULL; |
| 4956 | return PyInt_FromLong((long)minor(device)); |
| 4957 | } |
| 4958 | |
| 4959 | PyDoc_STRVAR(posix_makedev__doc__, |
| 4960 | "makedev(major, minor) -> device number\n\ |
| 4961 | Composes a raw device number from the major and minor device numbers."); |
| 4962 | |
| 4963 | static PyObject * |
| 4964 | posix_makedev(PyObject *self, PyObject *args) |
| 4965 | { |
| 4966 | int major, minor; |
| 4967 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 4968 | return NULL; |
| 4969 | return PyInt_FromLong((long)makedev(major, minor)); |
| 4970 | } |
| 4971 | #endif /* device macros */ |
| 4972 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4973 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4974 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4975 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4976 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4977 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4978 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4979 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4980 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4981 | { |
| 4982 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4983 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4984 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4985 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4986 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4987 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4988 | return NULL; |
| 4989 | |
| 4990 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4991 | length = PyInt_AsLong(lenobj); |
| 4992 | #else |
| 4993 | length = PyLong_Check(lenobj) ? |
| 4994 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 4995 | #endif |
| 4996 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4997 | return NULL; |
| 4998 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4999 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5000 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5001 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5002 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5003 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5004 | return NULL; |
| 5005 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5006 | Py_INCREF(Py_None); |
| 5007 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5008 | } |
| 5009 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5010 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5011 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5012 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5013 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5014 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5015 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5016 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5017 | * get re-set with another call for the same key. */ |
| 5018 | static PyObject *posix_putenv_garbage; |
| 5019 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5020 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5021 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5022 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5023 | #ifdef MS_WINDOWS |
| 5024 | wchar_t *s1, *s2; |
| 5025 | wchar_t *newenv; |
| 5026 | #else |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5027 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5028 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5029 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5030 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5031 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5032 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5033 | if (!PyArg_ParseTuple(args, |
| 5034 | #ifdef MS_WINDOWS |
| 5035 | "uu:putenv", |
| 5036 | #else |
| 5037 | "ss:putenv", |
| 5038 | #endif |
| 5039 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5040 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5041 | |
| 5042 | #if defined(PYOS_OS2) |
| 5043 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5044 | APIRET rc; |
| 5045 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5046 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5047 | if (rc != NO_ERROR) |
| 5048 | return os2_error(rc); |
| 5049 | |
| 5050 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5051 | APIRET rc; |
| 5052 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5053 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5054 | if (rc != NO_ERROR) |
| 5055 | return os2_error(rc); |
| 5056 | } else { |
| 5057 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5058 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5059 | /* len includes space for a trailing \0; the size arg to |
| 5060 | PyString_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5061 | #ifdef MS_WINDOWS |
| 5062 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5063 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5064 | #else |
| 5065 | len = strlen(s1) + strlen(s2) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5066 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5067 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5068 | if (newstr == NULL) |
| 5069 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5070 | #ifdef MS_WINDOWS |
| 5071 | newenv = PyUnicode_AsUnicode(newstr); |
| 5072 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5073 | if (_wputenv(newenv)) { |
| 5074 | Py_DECREF(newstr); |
| 5075 | posix_error(); |
| 5076 | return NULL; |
| 5077 | } |
| 5078 | #else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5079 | newenv = PyString_AS_STRING(newstr); |
| 5080 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5081 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5082 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5083 | posix_error(); |
| 5084 | return NULL; |
| 5085 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5086 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5087 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5088 | * this will cause previous value to be collected. This has to |
| 5089 | * happen after the real putenv() call because the old value |
| 5090 | * was still accessible until then. */ |
| 5091 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5092 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5093 | /* really not much we can do; just leak */ |
| 5094 | PyErr_Clear(); |
| 5095 | } |
| 5096 | else { |
| 5097 | Py_DECREF(newstr); |
| 5098 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5099 | |
| 5100 | #if defined(PYOS_OS2) |
| 5101 | } |
| 5102 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5103 | Py_INCREF(Py_None); |
| 5104 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5105 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5106 | #endif /* putenv */ |
| 5107 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5108 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5109 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5110 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5111 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5112 | |
| 5113 | static PyObject * |
| 5114 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5115 | { |
| 5116 | char *s1; |
| 5117 | |
| 5118 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5119 | return NULL; |
| 5120 | |
| 5121 | unsetenv(s1); |
| 5122 | |
| 5123 | /* Remove the key from posix_putenv_garbage; |
| 5124 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5125 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5126 | * old value was still accessible until then. |
| 5127 | */ |
| 5128 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5129 | PyTuple_GET_ITEM(args, 0))) { |
| 5130 | /* really not much we can do; just leak */ |
| 5131 | PyErr_Clear(); |
| 5132 | } |
| 5133 | |
| 5134 | Py_INCREF(Py_None); |
| 5135 | return Py_None; |
| 5136 | } |
| 5137 | #endif /* unsetenv */ |
| 5138 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5139 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5140 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5141 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5142 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5143 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5144 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5145 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5146 | { |
| 5147 | int code; |
| 5148 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5149 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5150 | return NULL; |
| 5151 | message = strerror(code); |
| 5152 | if (message == NULL) { |
| 5153 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5154 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5155 | return NULL; |
| 5156 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5157 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5158 | } |
| 5159 | #endif /* strerror */ |
| 5160 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5161 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5162 | #ifdef HAVE_SYS_WAIT_H |
| 5163 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5164 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5165 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5166 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5167 | 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] | 5168 | |
| 5169 | static PyObject * |
| 5170 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5171 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5172 | WAIT_TYPE status; |
| 5173 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5174 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5175 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5176 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5177 | |
| 5178 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5179 | } |
| 5180 | #endif /* WCOREDUMP */ |
| 5181 | |
| 5182 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5183 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5184 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5185 | 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] | 5186 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5187 | |
| 5188 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5189 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5190 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5191 | WAIT_TYPE status; |
| 5192 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5193 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5194 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5195 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5196 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5197 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5198 | } |
| 5199 | #endif /* WIFCONTINUED */ |
| 5200 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5201 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5202 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5203 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5204 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5205 | |
| 5206 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5207 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5208 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5209 | WAIT_TYPE status; |
| 5210 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5211 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5212 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5213 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5214 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5215 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5216 | } |
| 5217 | #endif /* WIFSTOPPED */ |
| 5218 | |
| 5219 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5220 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5221 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5222 | 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] | 5223 | |
| 5224 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5225 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5226 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5227 | WAIT_TYPE status; |
| 5228 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5229 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5230 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5231 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5232 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5233 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5234 | } |
| 5235 | #endif /* WIFSIGNALED */ |
| 5236 | |
| 5237 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5238 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5239 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5240 | 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] | 5241 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5242 | |
| 5243 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5244 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5245 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5246 | WAIT_TYPE status; |
| 5247 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5248 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5249 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5250 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5251 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5252 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5253 | } |
| 5254 | #endif /* WIFEXITED */ |
| 5255 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5256 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5257 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5258 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5259 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5260 | |
| 5261 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5262 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5263 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5264 | WAIT_TYPE status; |
| 5265 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5266 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5267 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5268 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5269 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5270 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5271 | } |
| 5272 | #endif /* WEXITSTATUS */ |
| 5273 | |
| 5274 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5275 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5276 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5277 | 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] | 5278 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5279 | |
| 5280 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5281 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5282 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5283 | WAIT_TYPE status; |
| 5284 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5285 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5286 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5287 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5288 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5289 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5290 | } |
| 5291 | #endif /* WTERMSIG */ |
| 5292 | |
| 5293 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5294 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5295 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5296 | Return the signal that stopped the process that provided\n\ |
| 5297 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5298 | |
| 5299 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5300 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5301 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5302 | WAIT_TYPE status; |
| 5303 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5304 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5305 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5306 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5307 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5308 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5309 | } |
| 5310 | #endif /* WSTOPSIG */ |
| 5311 | |
| 5312 | #endif /* HAVE_SYS_WAIT_H */ |
| 5313 | |
| 5314 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5315 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5316 | #ifdef _SCO_DS |
| 5317 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5318 | needed definitions in sys/statvfs.h */ |
| 5319 | #define _SVID3 |
| 5320 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5321 | #include <sys/statvfs.h> |
| 5322 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5323 | static PyObject* |
| 5324 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5325 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5326 | if (v == NULL) |
| 5327 | return NULL; |
| 5328 | |
| 5329 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5330 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5331 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5332 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5333 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5334 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5335 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5336 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5337 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5338 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5339 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5340 | #else |
| 5341 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5342 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5343 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5344 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5345 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5346 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5347 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5348 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5349 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5350 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5351 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5352 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5353 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5354 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5355 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5356 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5357 | #endif |
| 5358 | |
| 5359 | return v; |
| 5360 | } |
| 5361 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5362 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5363 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5364 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5365 | |
| 5366 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5367 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5368 | { |
| 5369 | int fd, res; |
| 5370 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5371 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5372 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5373 | return NULL; |
| 5374 | Py_BEGIN_ALLOW_THREADS |
| 5375 | res = fstatvfs(fd, &st); |
| 5376 | Py_END_ALLOW_THREADS |
| 5377 | if (res != 0) |
| 5378 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5379 | |
| 5380 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5381 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5382 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5383 | |
| 5384 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5385 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5386 | #include <sys/statvfs.h> |
| 5387 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5388 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5389 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5390 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5391 | |
| 5392 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5393 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5394 | { |
| 5395 | char *path; |
| 5396 | int res; |
| 5397 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5398 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5399 | return NULL; |
| 5400 | Py_BEGIN_ALLOW_THREADS |
| 5401 | res = statvfs(path, &st); |
| 5402 | Py_END_ALLOW_THREADS |
| 5403 | if (res != 0) |
| 5404 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5405 | |
| 5406 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5407 | } |
| 5408 | #endif /* HAVE_STATVFS */ |
| 5409 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5410 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5411 | * It maps strings representing configuration variable names to |
| 5412 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5413 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5414 | * rarely-used constants. There are three separate tables that use |
| 5415 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5416 | * |
| 5417 | * This code is always included, even if none of the interfaces that |
| 5418 | * need it are included. The #if hackery needed to avoid it would be |
| 5419 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5420 | */ |
| 5421 | struct constdef { |
| 5422 | char *name; |
| 5423 | long value; |
| 5424 | }; |
| 5425 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5426 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5427 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5428 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5429 | { |
| 5430 | if (PyInt_Check(arg)) { |
| 5431 | *valuep = PyInt_AS_LONG(arg); |
| 5432 | return 1; |
| 5433 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5434 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5435 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5436 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5437 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5438 | size_t hi = tablesize; |
| 5439 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5440 | const char *confname; |
| 5441 | Py_ssize_t namelen; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5442 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5443 | PyErr_SetString(PyExc_TypeError, |
| 5444 | "configuration names must be strings or integers"); |
| 5445 | return 0; |
| 5446 | } |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5447 | confname = PyUnicode_AsString(arg); |
| 5448 | if (confname == NULL) |
| 5449 | return 0; |
| 5450 | namelen = strlen(confname); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5451 | while (lo < hi) { |
| 5452 | mid = (lo + hi) / 2; |
| 5453 | cmp = strcmp(confname, table[mid].name); |
| 5454 | if (cmp < 0) |
| 5455 | hi = mid; |
| 5456 | else if (cmp > 0) |
| 5457 | lo = mid + 1; |
| 5458 | else { |
| 5459 | *valuep = table[mid].value; |
| 5460 | return 1; |
| 5461 | } |
| 5462 | } |
| 5463 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5464 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5465 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | |
| 5469 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5470 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5471 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5472 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5473 | #endif |
| 5474 | #ifdef _PC_ABI_ASYNC_IO |
| 5475 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5476 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5477 | #ifdef _PC_ASYNC_IO |
| 5478 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5479 | #endif |
| 5480 | #ifdef _PC_CHOWN_RESTRICTED |
| 5481 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5482 | #endif |
| 5483 | #ifdef _PC_FILESIZEBITS |
| 5484 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5485 | #endif |
| 5486 | #ifdef _PC_LAST |
| 5487 | {"PC_LAST", _PC_LAST}, |
| 5488 | #endif |
| 5489 | #ifdef _PC_LINK_MAX |
| 5490 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5491 | #endif |
| 5492 | #ifdef _PC_MAX_CANON |
| 5493 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5494 | #endif |
| 5495 | #ifdef _PC_MAX_INPUT |
| 5496 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5497 | #endif |
| 5498 | #ifdef _PC_NAME_MAX |
| 5499 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5500 | #endif |
| 5501 | #ifdef _PC_NO_TRUNC |
| 5502 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5503 | #endif |
| 5504 | #ifdef _PC_PATH_MAX |
| 5505 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5506 | #endif |
| 5507 | #ifdef _PC_PIPE_BUF |
| 5508 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5509 | #endif |
| 5510 | #ifdef _PC_PRIO_IO |
| 5511 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5512 | #endif |
| 5513 | #ifdef _PC_SOCK_MAXBUF |
| 5514 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5515 | #endif |
| 5516 | #ifdef _PC_SYNC_IO |
| 5517 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5518 | #endif |
| 5519 | #ifdef _PC_VDISABLE |
| 5520 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5521 | #endif |
| 5522 | }; |
| 5523 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5524 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5525 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5526 | { |
| 5527 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5528 | sizeof(posix_constants_pathconf) |
| 5529 | / sizeof(struct constdef)); |
| 5530 | } |
| 5531 | #endif |
| 5532 | |
| 5533 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5534 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5535 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5536 | 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] | 5537 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5538 | |
| 5539 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5540 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5541 | { |
| 5542 | PyObject *result = NULL; |
| 5543 | int name, fd; |
| 5544 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5545 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5546 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5547 | long limit; |
| 5548 | |
| 5549 | errno = 0; |
| 5550 | limit = fpathconf(fd, name); |
| 5551 | if (limit == -1 && errno != 0) |
| 5552 | posix_error(); |
| 5553 | else |
| 5554 | result = PyInt_FromLong(limit); |
| 5555 | } |
| 5556 | return result; |
| 5557 | } |
| 5558 | #endif |
| 5559 | |
| 5560 | |
| 5561 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5562 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5563 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5564 | 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] | 5565 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5566 | |
| 5567 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5568 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5569 | { |
| 5570 | PyObject *result = NULL; |
| 5571 | int name; |
| 5572 | char *path; |
| 5573 | |
| 5574 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5575 | conv_path_confname, &name)) { |
| 5576 | long limit; |
| 5577 | |
| 5578 | errno = 0; |
| 5579 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5580 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5581 | if (errno == EINVAL) |
| 5582 | /* could be a path or name problem */ |
| 5583 | posix_error(); |
| 5584 | else |
| 5585 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5586 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5587 | else |
| 5588 | result = PyInt_FromLong(limit); |
| 5589 | } |
| 5590 | return result; |
| 5591 | } |
| 5592 | #endif |
| 5593 | |
| 5594 | #ifdef HAVE_CONFSTR |
| 5595 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5596 | #ifdef _CS_ARCHITECTURE |
| 5597 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5598 | #endif |
| 5599 | #ifdef _CS_HOSTNAME |
| 5600 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5601 | #endif |
| 5602 | #ifdef _CS_HW_PROVIDER |
| 5603 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5604 | #endif |
| 5605 | #ifdef _CS_HW_SERIAL |
| 5606 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5607 | #endif |
| 5608 | #ifdef _CS_INITTAB_NAME |
| 5609 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5610 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5611 | #ifdef _CS_LFS64_CFLAGS |
| 5612 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5613 | #endif |
| 5614 | #ifdef _CS_LFS64_LDFLAGS |
| 5615 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5616 | #endif |
| 5617 | #ifdef _CS_LFS64_LIBS |
| 5618 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5619 | #endif |
| 5620 | #ifdef _CS_LFS64_LINTFLAGS |
| 5621 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5622 | #endif |
| 5623 | #ifdef _CS_LFS_CFLAGS |
| 5624 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5625 | #endif |
| 5626 | #ifdef _CS_LFS_LDFLAGS |
| 5627 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5628 | #endif |
| 5629 | #ifdef _CS_LFS_LIBS |
| 5630 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5631 | #endif |
| 5632 | #ifdef _CS_LFS_LINTFLAGS |
| 5633 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5634 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5635 | #ifdef _CS_MACHINE |
| 5636 | {"CS_MACHINE", _CS_MACHINE}, |
| 5637 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5638 | #ifdef _CS_PATH |
| 5639 | {"CS_PATH", _CS_PATH}, |
| 5640 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5641 | #ifdef _CS_RELEASE |
| 5642 | {"CS_RELEASE", _CS_RELEASE}, |
| 5643 | #endif |
| 5644 | #ifdef _CS_SRPC_DOMAIN |
| 5645 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5646 | #endif |
| 5647 | #ifdef _CS_SYSNAME |
| 5648 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5649 | #endif |
| 5650 | #ifdef _CS_VERSION |
| 5651 | {"CS_VERSION", _CS_VERSION}, |
| 5652 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5653 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5654 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5655 | #endif |
| 5656 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5657 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5658 | #endif |
| 5659 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5660 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5661 | #endif |
| 5662 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5663 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5664 | #endif |
| 5665 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5666 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5667 | #endif |
| 5668 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5669 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5670 | #endif |
| 5671 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5672 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5673 | #endif |
| 5674 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5675 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5676 | #endif |
| 5677 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5678 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5679 | #endif |
| 5680 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5681 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5682 | #endif |
| 5683 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5684 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5685 | #endif |
| 5686 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5687 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5688 | #endif |
| 5689 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5690 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5691 | #endif |
| 5692 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5693 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5694 | #endif |
| 5695 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5696 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5697 | #endif |
| 5698 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5699 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5700 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5701 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5702 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5703 | #endif |
| 5704 | #ifdef _MIPS_CS_BASE |
| 5705 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5706 | #endif |
| 5707 | #ifdef _MIPS_CS_HOSTID |
| 5708 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5709 | #endif |
| 5710 | #ifdef _MIPS_CS_HW_NAME |
| 5711 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5712 | #endif |
| 5713 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5714 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5715 | #endif |
| 5716 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5717 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5718 | #endif |
| 5719 | #ifdef _MIPS_CS_OSREL_MIN |
| 5720 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5721 | #endif |
| 5722 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5723 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5724 | #endif |
| 5725 | #ifdef _MIPS_CS_OS_NAME |
| 5726 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 5727 | #endif |
| 5728 | #ifdef _MIPS_CS_OS_PROVIDER |
| 5729 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 5730 | #endif |
| 5731 | #ifdef _MIPS_CS_PROCESSORS |
| 5732 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 5733 | #endif |
| 5734 | #ifdef _MIPS_CS_SERIAL |
| 5735 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 5736 | #endif |
| 5737 | #ifdef _MIPS_CS_VENDOR |
| 5738 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 5739 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5740 | }; |
| 5741 | |
| 5742 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5743 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5744 | { |
| 5745 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 5746 | sizeof(posix_constants_confstr) |
| 5747 | / sizeof(struct constdef)); |
| 5748 | } |
| 5749 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5750 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5751 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5752 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5753 | |
| 5754 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5755 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5756 | { |
| 5757 | PyObject *result = NULL; |
| 5758 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5759 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5760 | |
| 5761 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5762 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5763 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5764 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5765 | len = confstr(name, buffer, sizeof(buffer)); |
| 5766 | if (len == 0) { |
| 5767 | if (errno) { |
| 5768 | posix_error(); |
| 5769 | } |
| 5770 | else { |
| 5771 | result = Py_None; |
| 5772 | Py_INCREF(Py_None); |
| 5773 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5774 | } |
| 5775 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5776 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5777 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5778 | if (result != NULL) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5779 | confstr(name, PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5780 | } |
| 5781 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5782 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5783 | } |
| 5784 | } |
| 5785 | return result; |
| 5786 | } |
| 5787 | #endif |
| 5788 | |
| 5789 | |
| 5790 | #ifdef HAVE_SYSCONF |
| 5791 | static struct constdef posix_constants_sysconf[] = { |
| 5792 | #ifdef _SC_2_CHAR_TERM |
| 5793 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 5794 | #endif |
| 5795 | #ifdef _SC_2_C_BIND |
| 5796 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 5797 | #endif |
| 5798 | #ifdef _SC_2_C_DEV |
| 5799 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 5800 | #endif |
| 5801 | #ifdef _SC_2_C_VERSION |
| 5802 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 5803 | #endif |
| 5804 | #ifdef _SC_2_FORT_DEV |
| 5805 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 5806 | #endif |
| 5807 | #ifdef _SC_2_FORT_RUN |
| 5808 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 5809 | #endif |
| 5810 | #ifdef _SC_2_LOCALEDEF |
| 5811 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 5812 | #endif |
| 5813 | #ifdef _SC_2_SW_DEV |
| 5814 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 5815 | #endif |
| 5816 | #ifdef _SC_2_UPE |
| 5817 | {"SC_2_UPE", _SC_2_UPE}, |
| 5818 | #endif |
| 5819 | #ifdef _SC_2_VERSION |
| 5820 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 5821 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5822 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 5823 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 5824 | #endif |
| 5825 | #ifdef _SC_ACL |
| 5826 | {"SC_ACL", _SC_ACL}, |
| 5827 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5828 | #ifdef _SC_AIO_LISTIO_MAX |
| 5829 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 5830 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5831 | #ifdef _SC_AIO_MAX |
| 5832 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 5833 | #endif |
| 5834 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 5835 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 5836 | #endif |
| 5837 | #ifdef _SC_ARG_MAX |
| 5838 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 5839 | #endif |
| 5840 | #ifdef _SC_ASYNCHRONOUS_IO |
| 5841 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 5842 | #endif |
| 5843 | #ifdef _SC_ATEXIT_MAX |
| 5844 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 5845 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5846 | #ifdef _SC_AUDIT |
| 5847 | {"SC_AUDIT", _SC_AUDIT}, |
| 5848 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5849 | #ifdef _SC_AVPHYS_PAGES |
| 5850 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 5851 | #endif |
| 5852 | #ifdef _SC_BC_BASE_MAX |
| 5853 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 5854 | #endif |
| 5855 | #ifdef _SC_BC_DIM_MAX |
| 5856 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 5857 | #endif |
| 5858 | #ifdef _SC_BC_SCALE_MAX |
| 5859 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 5860 | #endif |
| 5861 | #ifdef _SC_BC_STRING_MAX |
| 5862 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 5863 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5864 | #ifdef _SC_CAP |
| 5865 | {"SC_CAP", _SC_CAP}, |
| 5866 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5867 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 5868 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 5869 | #endif |
| 5870 | #ifdef _SC_CHAR_BIT |
| 5871 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 5872 | #endif |
| 5873 | #ifdef _SC_CHAR_MAX |
| 5874 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 5875 | #endif |
| 5876 | #ifdef _SC_CHAR_MIN |
| 5877 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 5878 | #endif |
| 5879 | #ifdef _SC_CHILD_MAX |
| 5880 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 5881 | #endif |
| 5882 | #ifdef _SC_CLK_TCK |
| 5883 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 5884 | #endif |
| 5885 | #ifdef _SC_COHER_BLKSZ |
| 5886 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 5887 | #endif |
| 5888 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 5889 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 5890 | #endif |
| 5891 | #ifdef _SC_DCACHE_ASSOC |
| 5892 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 5893 | #endif |
| 5894 | #ifdef _SC_DCACHE_BLKSZ |
| 5895 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 5896 | #endif |
| 5897 | #ifdef _SC_DCACHE_LINESZ |
| 5898 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 5899 | #endif |
| 5900 | #ifdef _SC_DCACHE_SZ |
| 5901 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 5902 | #endif |
| 5903 | #ifdef _SC_DCACHE_TBLKSZ |
| 5904 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 5905 | #endif |
| 5906 | #ifdef _SC_DELAYTIMER_MAX |
| 5907 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 5908 | #endif |
| 5909 | #ifdef _SC_EQUIV_CLASS_MAX |
| 5910 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 5911 | #endif |
| 5912 | #ifdef _SC_EXPR_NEST_MAX |
| 5913 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 5914 | #endif |
| 5915 | #ifdef _SC_FSYNC |
| 5916 | {"SC_FSYNC", _SC_FSYNC}, |
| 5917 | #endif |
| 5918 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5919 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5920 | #endif |
| 5921 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5922 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5923 | #endif |
| 5924 | #ifdef _SC_ICACHE_ASSOC |
| 5925 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5926 | #endif |
| 5927 | #ifdef _SC_ICACHE_BLKSZ |
| 5928 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5929 | #endif |
| 5930 | #ifdef _SC_ICACHE_LINESZ |
| 5931 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5932 | #endif |
| 5933 | #ifdef _SC_ICACHE_SZ |
| 5934 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5935 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5936 | #ifdef _SC_INF |
| 5937 | {"SC_INF", _SC_INF}, |
| 5938 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5939 | #ifdef _SC_INT_MAX |
| 5940 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5941 | #endif |
| 5942 | #ifdef _SC_INT_MIN |
| 5943 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5944 | #endif |
| 5945 | #ifdef _SC_IOV_MAX |
| 5946 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5947 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5948 | #ifdef _SC_IP_SECOPTS |
| 5949 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5950 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5951 | #ifdef _SC_JOB_CONTROL |
| 5952 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5953 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5954 | #ifdef _SC_KERN_POINTERS |
| 5955 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5956 | #endif |
| 5957 | #ifdef _SC_KERN_SIM |
| 5958 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5959 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5960 | #ifdef _SC_LINE_MAX |
| 5961 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5962 | #endif |
| 5963 | #ifdef _SC_LOGIN_NAME_MAX |
| 5964 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5965 | #endif |
| 5966 | #ifdef _SC_LOGNAME_MAX |
| 5967 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5968 | #endif |
| 5969 | #ifdef _SC_LONG_BIT |
| 5970 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5971 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5972 | #ifdef _SC_MAC |
| 5973 | {"SC_MAC", _SC_MAC}, |
| 5974 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5975 | #ifdef _SC_MAPPED_FILES |
| 5976 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5977 | #endif |
| 5978 | #ifdef _SC_MAXPID |
| 5979 | {"SC_MAXPID", _SC_MAXPID}, |
| 5980 | #endif |
| 5981 | #ifdef _SC_MB_LEN_MAX |
| 5982 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5983 | #endif |
| 5984 | #ifdef _SC_MEMLOCK |
| 5985 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 5986 | #endif |
| 5987 | #ifdef _SC_MEMLOCK_RANGE |
| 5988 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 5989 | #endif |
| 5990 | #ifdef _SC_MEMORY_PROTECTION |
| 5991 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 5992 | #endif |
| 5993 | #ifdef _SC_MESSAGE_PASSING |
| 5994 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 5995 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5996 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 5997 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 5998 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5999 | #ifdef _SC_MQ_OPEN_MAX |
| 6000 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6001 | #endif |
| 6002 | #ifdef _SC_MQ_PRIO_MAX |
| 6003 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6004 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6005 | #ifdef _SC_NACLS_MAX |
| 6006 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6007 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6008 | #ifdef _SC_NGROUPS_MAX |
| 6009 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6010 | #endif |
| 6011 | #ifdef _SC_NL_ARGMAX |
| 6012 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6013 | #endif |
| 6014 | #ifdef _SC_NL_LANGMAX |
| 6015 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6016 | #endif |
| 6017 | #ifdef _SC_NL_MSGMAX |
| 6018 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6019 | #endif |
| 6020 | #ifdef _SC_NL_NMAX |
| 6021 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6022 | #endif |
| 6023 | #ifdef _SC_NL_SETMAX |
| 6024 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6025 | #endif |
| 6026 | #ifdef _SC_NL_TEXTMAX |
| 6027 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6028 | #endif |
| 6029 | #ifdef _SC_NPROCESSORS_CONF |
| 6030 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6031 | #endif |
| 6032 | #ifdef _SC_NPROCESSORS_ONLN |
| 6033 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6034 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6035 | #ifdef _SC_NPROC_CONF |
| 6036 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6037 | #endif |
| 6038 | #ifdef _SC_NPROC_ONLN |
| 6039 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6040 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6041 | #ifdef _SC_NZERO |
| 6042 | {"SC_NZERO", _SC_NZERO}, |
| 6043 | #endif |
| 6044 | #ifdef _SC_OPEN_MAX |
| 6045 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6046 | #endif |
| 6047 | #ifdef _SC_PAGESIZE |
| 6048 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6049 | #endif |
| 6050 | #ifdef _SC_PAGE_SIZE |
| 6051 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6052 | #endif |
| 6053 | #ifdef _SC_PASS_MAX |
| 6054 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6055 | #endif |
| 6056 | #ifdef _SC_PHYS_PAGES |
| 6057 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6058 | #endif |
| 6059 | #ifdef _SC_PII |
| 6060 | {"SC_PII", _SC_PII}, |
| 6061 | #endif |
| 6062 | #ifdef _SC_PII_INTERNET |
| 6063 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6064 | #endif |
| 6065 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6066 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6067 | #endif |
| 6068 | #ifdef _SC_PII_INTERNET_STREAM |
| 6069 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6070 | #endif |
| 6071 | #ifdef _SC_PII_OSI |
| 6072 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6073 | #endif |
| 6074 | #ifdef _SC_PII_OSI_CLTS |
| 6075 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6076 | #endif |
| 6077 | #ifdef _SC_PII_OSI_COTS |
| 6078 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6079 | #endif |
| 6080 | #ifdef _SC_PII_OSI_M |
| 6081 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6082 | #endif |
| 6083 | #ifdef _SC_PII_SOCKET |
| 6084 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6085 | #endif |
| 6086 | #ifdef _SC_PII_XTI |
| 6087 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6088 | #endif |
| 6089 | #ifdef _SC_POLL |
| 6090 | {"SC_POLL", _SC_POLL}, |
| 6091 | #endif |
| 6092 | #ifdef _SC_PRIORITIZED_IO |
| 6093 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6094 | #endif |
| 6095 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6096 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6097 | #endif |
| 6098 | #ifdef _SC_REALTIME_SIGNALS |
| 6099 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6100 | #endif |
| 6101 | #ifdef _SC_RE_DUP_MAX |
| 6102 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6103 | #endif |
| 6104 | #ifdef _SC_RTSIG_MAX |
| 6105 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6106 | #endif |
| 6107 | #ifdef _SC_SAVED_IDS |
| 6108 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6109 | #endif |
| 6110 | #ifdef _SC_SCHAR_MAX |
| 6111 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6112 | #endif |
| 6113 | #ifdef _SC_SCHAR_MIN |
| 6114 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6115 | #endif |
| 6116 | #ifdef _SC_SELECT |
| 6117 | {"SC_SELECT", _SC_SELECT}, |
| 6118 | #endif |
| 6119 | #ifdef _SC_SEMAPHORES |
| 6120 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6121 | #endif |
| 6122 | #ifdef _SC_SEM_NSEMS_MAX |
| 6123 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6124 | #endif |
| 6125 | #ifdef _SC_SEM_VALUE_MAX |
| 6126 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6127 | #endif |
| 6128 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6129 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6130 | #endif |
| 6131 | #ifdef _SC_SHRT_MAX |
| 6132 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6133 | #endif |
| 6134 | #ifdef _SC_SHRT_MIN |
| 6135 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6136 | #endif |
| 6137 | #ifdef _SC_SIGQUEUE_MAX |
| 6138 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6139 | #endif |
| 6140 | #ifdef _SC_SIGRT_MAX |
| 6141 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6142 | #endif |
| 6143 | #ifdef _SC_SIGRT_MIN |
| 6144 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6145 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6146 | #ifdef _SC_SOFTPOWER |
| 6147 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6148 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6149 | #ifdef _SC_SPLIT_CACHE |
| 6150 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6151 | #endif |
| 6152 | #ifdef _SC_SSIZE_MAX |
| 6153 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6154 | #endif |
| 6155 | #ifdef _SC_STACK_PROT |
| 6156 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6157 | #endif |
| 6158 | #ifdef _SC_STREAM_MAX |
| 6159 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6160 | #endif |
| 6161 | #ifdef _SC_SYNCHRONIZED_IO |
| 6162 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6163 | #endif |
| 6164 | #ifdef _SC_THREADS |
| 6165 | {"SC_THREADS", _SC_THREADS}, |
| 6166 | #endif |
| 6167 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6168 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6169 | #endif |
| 6170 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6171 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6172 | #endif |
| 6173 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6174 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6175 | #endif |
| 6176 | #ifdef _SC_THREAD_KEYS_MAX |
| 6177 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6178 | #endif |
| 6179 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6180 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6181 | #endif |
| 6182 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6183 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6184 | #endif |
| 6185 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6186 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6187 | #endif |
| 6188 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6189 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6190 | #endif |
| 6191 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6192 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6193 | #endif |
| 6194 | #ifdef _SC_THREAD_STACK_MIN |
| 6195 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6196 | #endif |
| 6197 | #ifdef _SC_THREAD_THREADS_MAX |
| 6198 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6199 | #endif |
| 6200 | #ifdef _SC_TIMERS |
| 6201 | {"SC_TIMERS", _SC_TIMERS}, |
| 6202 | #endif |
| 6203 | #ifdef _SC_TIMER_MAX |
| 6204 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6205 | #endif |
| 6206 | #ifdef _SC_TTY_NAME_MAX |
| 6207 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6208 | #endif |
| 6209 | #ifdef _SC_TZNAME_MAX |
| 6210 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6211 | #endif |
| 6212 | #ifdef _SC_T_IOV_MAX |
| 6213 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6214 | #endif |
| 6215 | #ifdef _SC_UCHAR_MAX |
| 6216 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6217 | #endif |
| 6218 | #ifdef _SC_UINT_MAX |
| 6219 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6220 | #endif |
| 6221 | #ifdef _SC_UIO_MAXIOV |
| 6222 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6223 | #endif |
| 6224 | #ifdef _SC_ULONG_MAX |
| 6225 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6226 | #endif |
| 6227 | #ifdef _SC_USHRT_MAX |
| 6228 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6229 | #endif |
| 6230 | #ifdef _SC_VERSION |
| 6231 | {"SC_VERSION", _SC_VERSION}, |
| 6232 | #endif |
| 6233 | #ifdef _SC_WORD_BIT |
| 6234 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6235 | #endif |
| 6236 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6237 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6238 | #endif |
| 6239 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6240 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6241 | #endif |
| 6242 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6243 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6244 | #endif |
| 6245 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6246 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6247 | #endif |
| 6248 | #ifdef _SC_XOPEN_CRYPT |
| 6249 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6250 | #endif |
| 6251 | #ifdef _SC_XOPEN_ENH_I18N |
| 6252 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6253 | #endif |
| 6254 | #ifdef _SC_XOPEN_LEGACY |
| 6255 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6256 | #endif |
| 6257 | #ifdef _SC_XOPEN_REALTIME |
| 6258 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6259 | #endif |
| 6260 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6261 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6262 | #endif |
| 6263 | #ifdef _SC_XOPEN_SHM |
| 6264 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6265 | #endif |
| 6266 | #ifdef _SC_XOPEN_UNIX |
| 6267 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6268 | #endif |
| 6269 | #ifdef _SC_XOPEN_VERSION |
| 6270 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6271 | #endif |
| 6272 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6273 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6274 | #endif |
| 6275 | #ifdef _SC_XOPEN_XPG2 |
| 6276 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6277 | #endif |
| 6278 | #ifdef _SC_XOPEN_XPG3 |
| 6279 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6280 | #endif |
| 6281 | #ifdef _SC_XOPEN_XPG4 |
| 6282 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6283 | #endif |
| 6284 | }; |
| 6285 | |
| 6286 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6287 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6288 | { |
| 6289 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6290 | sizeof(posix_constants_sysconf) |
| 6291 | / sizeof(struct constdef)); |
| 6292 | } |
| 6293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6294 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6295 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6296 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6297 | |
| 6298 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6299 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6300 | { |
| 6301 | PyObject *result = NULL; |
| 6302 | int name; |
| 6303 | |
| 6304 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6305 | int value; |
| 6306 | |
| 6307 | errno = 0; |
| 6308 | value = sysconf(name); |
| 6309 | if (value == -1 && errno != 0) |
| 6310 | posix_error(); |
| 6311 | else |
| 6312 | result = PyInt_FromLong(value); |
| 6313 | } |
| 6314 | return result; |
| 6315 | } |
| 6316 | #endif |
| 6317 | |
| 6318 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6319 | /* This code is used to ensure that the tables of configuration value names |
| 6320 | * are in sorted order as required by conv_confname(), and also to build the |
| 6321 | * the exported dictionaries that are used to publish information about the |
| 6322 | * names available on the host platform. |
| 6323 | * |
| 6324 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6325 | * when used, even for platforms we're not able to test on. It also makes |
| 6326 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6327 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6328 | |
| 6329 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6330 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6331 | { |
| 6332 | const struct constdef *c1 = |
| 6333 | (const struct constdef *) v1; |
| 6334 | const struct constdef *c2 = |
| 6335 | (const struct constdef *) v2; |
| 6336 | |
| 6337 | return strcmp(c1->name, c2->name); |
| 6338 | } |
| 6339 | |
| 6340 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6341 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6342 | char *tablename, PyObject *module) |
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 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6345 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6346 | |
| 6347 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6348 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6349 | if (d == NULL) |
| 6350 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6351 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6352 | for (i=0; i < tablesize; ++i) { |
| 6353 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6354 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6355 | Py_XDECREF(o); |
| 6356 | Py_DECREF(d); |
| 6357 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6358 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6359 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6360 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6361 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6362 | } |
| 6363 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6364 | /* Return -1 on failure, 0 on success. */ |
| 6365 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6366 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6367 | { |
| 6368 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6369 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6370 | sizeof(posix_constants_pathconf) |
| 6371 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6372 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6373 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6374 | #endif |
| 6375 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6376 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6377 | sizeof(posix_constants_confstr) |
| 6378 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6379 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6380 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6381 | #endif |
| 6382 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6383 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6384 | sizeof(posix_constants_sysconf) |
| 6385 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6386 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6387 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6388 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6389 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6390 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6391 | |
| 6392 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6393 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6394 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6395 | 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] | 6396 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6397 | |
| 6398 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6399 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6400 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6401 | abort(); |
| 6402 | /*NOTREACHED*/ |
| 6403 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6404 | return NULL; |
| 6405 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6406 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6407 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6408 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6409 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6410 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6411 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6412 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6413 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6414 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6415 | application (if any) its extension is associated.\n\ |
| 6416 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6417 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6418 | \n\ |
| 6419 | startfile returns as soon as the associated application is launched.\n\ |
| 6420 | There is no option to wait for the application to close, and no way\n\ |
| 6421 | to retrieve the application's exit status.\n\ |
| 6422 | \n\ |
| 6423 | The filepath is relative to the current directory. If you want to use\n\ |
| 6424 | 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] | 6425 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6426 | |
| 6427 | static PyObject * |
| 6428 | win32_startfile(PyObject *self, PyObject *args) |
| 6429 | { |
| 6430 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6431 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6432 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6433 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6434 | if (unicode_file_names()) { |
| 6435 | PyObject *unipath, *woperation = NULL; |
| 6436 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6437 | &unipath, &operation)) { |
| 6438 | PyErr_Clear(); |
| 6439 | goto normal; |
| 6440 | } |
| 6441 | |
| 6442 | |
| 6443 | if (operation) { |
| 6444 | woperation = PyUnicode_DecodeASCII(operation, |
| 6445 | strlen(operation), NULL); |
| 6446 | if (!woperation) { |
| 6447 | PyErr_Clear(); |
| 6448 | operation = NULL; |
| 6449 | goto normal; |
| 6450 | } |
| 6451 | } |
| 6452 | |
| 6453 | Py_BEGIN_ALLOW_THREADS |
| 6454 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6455 | PyUnicode_AS_UNICODE(unipath), |
| 6456 | NULL, NULL, SW_SHOWNORMAL); |
| 6457 | Py_END_ALLOW_THREADS |
| 6458 | |
| 6459 | Py_XDECREF(woperation); |
| 6460 | if (rc <= (HINSTANCE)32) { |
| 6461 | PyObject *errval = win32_error_unicode("startfile", |
| 6462 | PyUnicode_AS_UNICODE(unipath)); |
| 6463 | return errval; |
| 6464 | } |
| 6465 | Py_INCREF(Py_None); |
| 6466 | return Py_None; |
| 6467 | } |
| 6468 | #endif |
| 6469 | |
| 6470 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6471 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 6472 | Py_FileSystemDefaultEncoding, &filepath, |
| 6473 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6474 | return NULL; |
| 6475 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6476 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6477 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6478 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6479 | if (rc <= (HINSTANCE)32) { |
| 6480 | PyObject *errval = win32_error("startfile", filepath); |
| 6481 | PyMem_Free(filepath); |
| 6482 | return errval; |
| 6483 | } |
| 6484 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6485 | Py_INCREF(Py_None); |
| 6486 | return Py_None; |
| 6487 | } |
| 6488 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6489 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6490 | #ifdef HAVE_GETLOADAVG |
| 6491 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6492 | "getloadavg() -> (float, float, float)\n\n\ |
| 6493 | Return the number of processes in the system run queue averaged over\n\ |
| 6494 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6495 | was unobtainable"); |
| 6496 | |
| 6497 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6498 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6499 | { |
| 6500 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6501 | if (getloadavg(loadavg, 3)!=3) { |
| 6502 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6503 | return NULL; |
| 6504 | } else |
| 6505 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6506 | } |
| 6507 | #endif |
| 6508 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6509 | #ifdef MS_WINDOWS |
| 6510 | |
| 6511 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6512 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6513 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6514 | |
| 6515 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6516 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6517 | DWORD dwFlags ); |
| 6518 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6519 | BYTE *pbBuffer ); |
| 6520 | |
| 6521 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6522 | /* This handle is never explicitly released. Instead, the operating |
| 6523 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6524 | static HCRYPTPROV hCryptProv = 0; |
| 6525 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6526 | static PyObject* |
| 6527 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6528 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6529 | int howMany; |
| 6530 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6531 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6532 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6533 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6534 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6535 | if (howMany < 0) |
| 6536 | return PyErr_Format(PyExc_ValueError, |
| 6537 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6538 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6539 | if (hCryptProv == 0) { |
| 6540 | HINSTANCE hAdvAPI32 = NULL; |
| 6541 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6542 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6543 | /* Obtain handle to the DLL containing CryptoAPI |
| 6544 | This should not fail */ |
| 6545 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6546 | if(hAdvAPI32 == NULL) |
| 6547 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6548 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6549 | /* Obtain pointers to the CryptoAPI functions |
| 6550 | This will fail on some early versions of Win95 */ |
| 6551 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6552 | hAdvAPI32, |
| 6553 | "CryptAcquireContextA"); |
| 6554 | if (pCryptAcquireContext == NULL) |
| 6555 | return PyErr_Format(PyExc_NotImplementedError, |
| 6556 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6557 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6558 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6559 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6560 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6561 | return PyErr_Format(PyExc_NotImplementedError, |
| 6562 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6563 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6564 | /* Acquire context */ |
| 6565 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6566 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6567 | return win32_error("CryptAcquireContext", NULL); |
| 6568 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6569 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6570 | /* Allocate bytes */ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6571 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6572 | if (result != NULL) { |
| 6573 | /* Get random data */ |
| 6574 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6575 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6576 | Py_DECREF(result); |
| 6577 | return win32_error("CryptGenRandom", NULL); |
| 6578 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6579 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6580 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6581 | } |
| 6582 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6583 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6584 | PyDoc_STRVAR(device_encoding__doc__, |
| 6585 | "device_encoding(fd) -> str\n\n\ |
| 6586 | Return a string describing the encoding of the device\n\ |
| 6587 | if the output is a terminal; else return None."); |
| 6588 | |
| 6589 | static PyObject * |
| 6590 | device_encoding(PyObject *self, PyObject *args) |
| 6591 | { |
| 6592 | int fd; |
| 6593 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6594 | return NULL; |
| 6595 | if (!isatty(fd)) { |
| 6596 | Py_INCREF(Py_None); |
| 6597 | return Py_None; |
| 6598 | } |
| 6599 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6600 | if (fd == 0) { |
| 6601 | char buf[100]; |
| 6602 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6603 | return PyUnicode_FromString(buf); |
| 6604 | } |
| 6605 | if (fd == 1 || fd == 2) { |
| 6606 | char buf[100]; |
| 6607 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6608 | return PyUnicode_FromString(buf); |
| 6609 | } |
| 6610 | #elif defined(CODESET) |
| 6611 | { |
| 6612 | char *codeset = nl_langinfo(CODESET); |
| 6613 | if (codeset) |
| 6614 | return PyUnicode_FromString(codeset); |
| 6615 | } |
| 6616 | #endif |
| 6617 | Py_INCREF(Py_None); |
| 6618 | return Py_None; |
| 6619 | } |
| 6620 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6621 | #ifdef __VMS |
| 6622 | /* Use openssl random routine */ |
| 6623 | #include <openssl/rand.h> |
| 6624 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6625 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6626 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6627 | |
| 6628 | static PyObject* |
| 6629 | vms_urandom(PyObject *self, PyObject *args) |
| 6630 | { |
| 6631 | int howMany; |
| 6632 | PyObject* result; |
| 6633 | |
| 6634 | /* Read arguments */ |
| 6635 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6636 | return NULL; |
| 6637 | if (howMany < 0) |
| 6638 | return PyErr_Format(PyExc_ValueError, |
| 6639 | "negative argument not allowed"); |
| 6640 | |
| 6641 | /* Allocate bytes */ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6642 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6643 | if (result != NULL) { |
| 6644 | /* Get random data */ |
| 6645 | if (RAND_pseudo_bytes((unsigned char*) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6646 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6647 | howMany) < 0) { |
| 6648 | Py_DECREF(result); |
| 6649 | return PyErr_Format(PyExc_ValueError, |
| 6650 | "RAND_pseudo_bytes"); |
| 6651 | } |
| 6652 | } |
| 6653 | return result; |
| 6654 | } |
| 6655 | #endif |
| 6656 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6657 | static PyMethodDef posix_methods[] = { |
| 6658 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6659 | #ifdef HAVE_TTYNAME |
| 6660 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6661 | #endif |
| 6662 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6663 | #ifdef HAVE_CHFLAGS |
| 6664 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 6665 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6666 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6667 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6668 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6669 | #endif /* HAVE_CHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6670 | #ifdef HAVE_LCHFLAGS |
| 6671 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 6672 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6673 | #ifdef HAVE_LCHOWN |
| 6674 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6675 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6676 | #ifdef HAVE_CHROOT |
| 6677 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6678 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6679 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6680 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6681 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6682 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6683 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6684 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6685 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6686 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6687 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6688 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6689 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6690 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6691 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6692 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6693 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6694 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6695 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6696 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6697 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6698 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6699 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6700 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6701 | {"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] | 6702 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6703 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6704 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6705 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6706 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6707 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6708 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6709 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6710 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6711 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6712 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6713 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6714 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6715 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6716 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6717 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6718 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6719 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6720 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6721 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6722 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6723 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6724 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6725 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6726 | #if defined(PYOS_OS2) |
| 6727 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 6728 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 6729 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6730 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6731 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6732 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6733 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6734 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6735 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6736 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6737 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6738 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6739 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6740 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6741 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6742 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6743 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6744 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6745 | #endif /* HAVE_GETEGID */ |
| 6746 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6747 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6748 | #endif /* HAVE_GETEUID */ |
| 6749 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6750 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6751 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6752 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6753 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6754 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6755 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6756 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6757 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6758 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6759 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6760 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6761 | #endif /* HAVE_GETPPID */ |
| 6762 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6763 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6764 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6765 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6766 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6767 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6768 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6769 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6770 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6771 | #ifdef HAVE_KILLPG |
| 6772 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6773 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6774 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6775 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6776 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 6777 | #ifdef MS_WINDOWS |
| 6778 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 6779 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6780 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6781 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6782 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6783 | #ifdef HAVE_SETEUID |
| 6784 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6785 | #endif /* HAVE_SETEUID */ |
| 6786 | #ifdef HAVE_SETEGID |
| 6787 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6788 | #endif /* HAVE_SETEGID */ |
| 6789 | #ifdef HAVE_SETREUID |
| 6790 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6791 | #endif /* HAVE_SETREUID */ |
| 6792 | #ifdef HAVE_SETREGID |
| 6793 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6794 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6795 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6796 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6797 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6798 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6799 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6800 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6801 | #ifdef HAVE_GETPGID |
| 6802 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 6803 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6804 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6805 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6806 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6807 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6808 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6809 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6810 | #ifdef HAVE_WAIT3 |
| 6811 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 6812 | #endif /* HAVE_WAIT3 */ |
| 6813 | #ifdef HAVE_WAIT4 |
| 6814 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 6815 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6816 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6817 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6818 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6819 | #ifdef HAVE_GETSID |
| 6820 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 6821 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6822 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6823 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6824 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6825 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6826 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6827 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6828 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6829 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6830 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6831 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6832 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6833 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6834 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6835 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6836 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6837 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6838 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6839 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6840 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6841 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6842 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6843 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6844 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6845 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6846 | #endif |
| 6847 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6848 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6849 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6850 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6851 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 6852 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6853 | #ifdef HAVE_DEVICE_MACROS |
| 6854 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 6855 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 6856 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 6857 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6858 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6859 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6860 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6861 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6862 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6863 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6864 | #ifdef HAVE_UNSETENV |
| 6865 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6866 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6867 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6868 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6869 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6870 | #ifdef HAVE_FCHDIR |
| 6871 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 6872 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6873 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6874 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6875 | #endif |
| 6876 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6877 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6878 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6879 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6880 | #ifdef WCOREDUMP |
| 6881 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 6882 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6883 | #ifdef WIFCONTINUED |
| 6884 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 6885 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6886 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6887 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6888 | #endif /* WIFSTOPPED */ |
| 6889 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6890 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6891 | #endif /* WIFSIGNALED */ |
| 6892 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6893 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6894 | #endif /* WIFEXITED */ |
| 6895 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6896 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6897 | #endif /* WEXITSTATUS */ |
| 6898 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6899 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6900 | #endif /* WTERMSIG */ |
| 6901 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6902 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6903 | #endif /* WSTOPSIG */ |
| 6904 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6905 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6906 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6907 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6908 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6909 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6910 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6911 | #ifdef HAVE_CONFSTR |
| 6912 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 6913 | #endif |
| 6914 | #ifdef HAVE_SYSCONF |
| 6915 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 6916 | #endif |
| 6917 | #ifdef HAVE_FPATHCONF |
| 6918 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 6919 | #endif |
| 6920 | #ifdef HAVE_PATHCONF |
| 6921 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 6922 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6923 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6924 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 6925 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 6926 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6927 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6928 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6929 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6930 | #ifdef MS_WINDOWS |
| 6931 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 6932 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6933 | #ifdef __VMS |
| 6934 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 6935 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6936 | {NULL, NULL} /* Sentinel */ |
| 6937 | }; |
| 6938 | |
| 6939 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6940 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6941 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6942 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6943 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6946 | #if defined(PYOS_OS2) |
| 6947 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6948 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6949 | { |
| 6950 | APIRET rc; |
| 6951 | ULONG values[QSV_MAX+1]; |
| 6952 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 6953 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6954 | |
| 6955 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 6956 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6957 | Py_END_ALLOW_THREADS |
| 6958 | |
| 6959 | if (rc != NO_ERROR) { |
| 6960 | os2_error(rc); |
| 6961 | return -1; |
| 6962 | } |
| 6963 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6964 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 6965 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 6966 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 6967 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 6968 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 6969 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 6970 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6971 | |
| 6972 | switch (values[QSV_VERSION_MINOR]) { |
| 6973 | case 0: ver = "2.00"; break; |
| 6974 | case 10: ver = "2.10"; break; |
| 6975 | case 11: ver = "2.11"; break; |
| 6976 | case 30: ver = "3.00"; break; |
| 6977 | case 40: ver = "4.00"; break; |
| 6978 | case 50: ver = "5.00"; break; |
| 6979 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 6980 | PyOS_snprintf(tmp, sizeof(tmp), |
| 6981 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 6982 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6983 | ver = &tmp[0]; |
| 6984 | } |
| 6985 | |
| 6986 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6987 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6988 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6989 | |
| 6990 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 6991 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 6992 | tmp[1] = ':'; |
| 6993 | tmp[2] = '\0'; |
| 6994 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6995 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6996 | } |
| 6997 | #endif |
| 6998 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6999 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7000 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7001 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7002 | #ifdef F_OK |
| 7003 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7004 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7005 | #ifdef R_OK |
| 7006 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7007 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7008 | #ifdef W_OK |
| 7009 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7010 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7011 | #ifdef X_OK |
| 7012 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7013 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7014 | #ifdef NGROUPS_MAX |
| 7015 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7016 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7017 | #ifdef TMP_MAX |
| 7018 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7019 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7020 | #ifdef WCONTINUED |
| 7021 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7022 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7023 | #ifdef WNOHANG |
| 7024 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7025 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7026 | #ifdef WUNTRACED |
| 7027 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7028 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7029 | #ifdef O_RDONLY |
| 7030 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7031 | #endif |
| 7032 | #ifdef O_WRONLY |
| 7033 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7034 | #endif |
| 7035 | #ifdef O_RDWR |
| 7036 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7037 | #endif |
| 7038 | #ifdef O_NDELAY |
| 7039 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7040 | #endif |
| 7041 | #ifdef O_NONBLOCK |
| 7042 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7043 | #endif |
| 7044 | #ifdef O_APPEND |
| 7045 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7046 | #endif |
| 7047 | #ifdef O_DSYNC |
| 7048 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7049 | #endif |
| 7050 | #ifdef O_RSYNC |
| 7051 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7052 | #endif |
| 7053 | #ifdef O_SYNC |
| 7054 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7055 | #endif |
| 7056 | #ifdef O_NOCTTY |
| 7057 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7058 | #endif |
| 7059 | #ifdef O_CREAT |
| 7060 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7061 | #endif |
| 7062 | #ifdef O_EXCL |
| 7063 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7064 | #endif |
| 7065 | #ifdef O_TRUNC |
| 7066 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7067 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7068 | #ifdef O_BINARY |
| 7069 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7070 | #endif |
| 7071 | #ifdef O_TEXT |
| 7072 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7073 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7074 | #ifdef O_LARGEFILE |
| 7075 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7076 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7077 | #ifdef O_SHLOCK |
| 7078 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7079 | #endif |
| 7080 | #ifdef O_EXLOCK |
| 7081 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7082 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7083 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7084 | /* MS Windows */ |
| 7085 | #ifdef O_NOINHERIT |
| 7086 | /* Don't inherit in child processes. */ |
| 7087 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7088 | #endif |
| 7089 | #ifdef _O_SHORT_LIVED |
| 7090 | /* Optimize for short life (keep in memory). */ |
| 7091 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7092 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7093 | #endif |
| 7094 | #ifdef O_TEMPORARY |
| 7095 | /* Automatically delete when last handle is closed. */ |
| 7096 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7097 | #endif |
| 7098 | #ifdef O_RANDOM |
| 7099 | /* Optimize for random access. */ |
| 7100 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7101 | #endif |
| 7102 | #ifdef O_SEQUENTIAL |
| 7103 | /* Optimize for sequential access. */ |
| 7104 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7105 | #endif |
| 7106 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7107 | /* GNU extensions. */ |
| 7108 | #ifdef O_DIRECT |
| 7109 | /* Direct disk access. */ |
| 7110 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7111 | #endif |
| 7112 | #ifdef O_DIRECTORY |
| 7113 | /* Must be a directory. */ |
| 7114 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7115 | #endif |
| 7116 | #ifdef O_NOFOLLOW |
| 7117 | /* Do not follow links. */ |
| 7118 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7119 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7120 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7121 | /* These come from sysexits.h */ |
| 7122 | #ifdef EX_OK |
| 7123 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7124 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7125 | #ifdef EX_USAGE |
| 7126 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7127 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7128 | #ifdef EX_DATAERR |
| 7129 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7130 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7131 | #ifdef EX_NOINPUT |
| 7132 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7133 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7134 | #ifdef EX_NOUSER |
| 7135 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7136 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7137 | #ifdef EX_NOHOST |
| 7138 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7139 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7140 | #ifdef EX_UNAVAILABLE |
| 7141 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7142 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7143 | #ifdef EX_SOFTWARE |
| 7144 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7145 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7146 | #ifdef EX_OSERR |
| 7147 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7148 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7149 | #ifdef EX_OSFILE |
| 7150 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7151 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7152 | #ifdef EX_CANTCREAT |
| 7153 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7154 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7155 | #ifdef EX_IOERR |
| 7156 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7157 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7158 | #ifdef EX_TEMPFAIL |
| 7159 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7160 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7161 | #ifdef EX_PROTOCOL |
| 7162 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7163 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7164 | #ifdef EX_NOPERM |
| 7165 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7166 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7167 | #ifdef EX_CONFIG |
| 7168 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7169 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7170 | #ifdef EX_NOTFOUND |
| 7171 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7172 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7173 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7174 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7175 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7176 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7177 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7178 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7179 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7180 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7181 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7182 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7183 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7184 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7185 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7186 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7187 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7188 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7189 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7190 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7191 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7192 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7193 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7194 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7195 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7196 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7197 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7198 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7199 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7200 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7201 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7202 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7203 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7204 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7205 | #if defined(PYOS_OS2) |
| 7206 | if (insertvalues(d)) return -1; |
| 7207 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7208 | return 0; |
| 7209 | } |
| 7210 | |
| 7211 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7212 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7213 | #define INITFUNC initnt |
| 7214 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7215 | |
| 7216 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7217 | #define INITFUNC initos2 |
| 7218 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7219 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7220 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7221 | #define INITFUNC initposix |
| 7222 | #define MODNAME "posix" |
| 7223 | #endif |
| 7224 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7225 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7226 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7227 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7228 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7229 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7230 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7231 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7232 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7233 | if (m == NULL) |
| 7234 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7235 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7236 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7237 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7238 | Py_XINCREF(v); |
| 7239 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7240 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7241 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7242 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7243 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7244 | return; |
| 7245 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7246 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7247 | return; |
| 7248 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7249 | Py_INCREF(PyExc_OSError); |
| 7250 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7251 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7252 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7253 | if (posix_putenv_garbage == NULL) |
| 7254 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7255 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7256 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7257 | if (!initialized) { |
| 7258 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7259 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7260 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7261 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7262 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7263 | structseq_new = StatResultType.tp_new; |
| 7264 | StatResultType.tp_new = statresult_new; |
| 7265 | |
| 7266 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7267 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 7268 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7269 | Py_INCREF((PyObject*) &StatResultType); |
| 7270 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7271 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7272 | PyModule_AddObject(m, "statvfs_result", |
| 7273 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7274 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7275 | |
| 7276 | #ifdef __APPLE__ |
| 7277 | /* |
| 7278 | * Step 2 of weak-linking support on Mac OS X. |
| 7279 | * |
| 7280 | * The code below removes functions that are not available on the |
| 7281 | * currently active platform. |
| 7282 | * |
| 7283 | * This block allow one to use a python binary that was build on |
| 7284 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7285 | * OSX 10.4. |
| 7286 | */ |
| 7287 | #ifdef HAVE_FSTATVFS |
| 7288 | if (fstatvfs == NULL) { |
| 7289 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 7290 | return; |
| 7291 | } |
| 7292 | } |
| 7293 | #endif /* HAVE_FSTATVFS */ |
| 7294 | |
| 7295 | #ifdef HAVE_STATVFS |
| 7296 | if (statvfs == NULL) { |
| 7297 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 7298 | return; |
| 7299 | } |
| 7300 | } |
| 7301 | #endif /* HAVE_STATVFS */ |
| 7302 | |
| 7303 | # ifdef HAVE_LCHOWN |
| 7304 | if (lchown == NULL) { |
| 7305 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 7306 | return; |
| 7307 | } |
| 7308 | } |
| 7309 | #endif /* HAVE_LCHOWN */ |
| 7310 | |
| 7311 | |
| 7312 | #endif /* __APPLE__ */ |
| 7313 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7314 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7315 | |
| 7316 | #ifdef __cplusplus |
| 7317 | } |
| 7318 | #endif |