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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 16 | #include "Python.h" |
| 17 | #include "structseq.h" |
| 18 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 19 | #if defined(__VMS) |
| 20 | # include <ctype.h> /* tolower() */ |
| 21 | # include <descrip.h> /* string descriptors */ |
| 22 | # include <dvidef.h> /* DVI$_name */ |
| 23 | # include <file.h> /* -> O_RDWR */ |
| 24 | # include <jpidef.h> /* JPI$_name */ |
| 25 | # include <lib$routines.h> /* LIB$name */ |
| 26 | # include <ots$routines.h> /* OTS$name */ |
| 27 | # include <ssdef.h> /* SS$_name */ |
| 28 | # include <unixio.h> |
| 29 | # include <unixlib.h> |
| 30 | # include <stat.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 31 | # include <wait.h> /* define wait() */ |
| 32 | #endif /* defined(__VMS) */ |
| 33 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 34 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 35 | "This module provides access to operating system functionality that is\n\ |
| 36 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 37 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 38 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 40 | #ifndef Py_USING_UNICODE |
| 41 | /* This is used in signatures of functions. */ |
| 42 | #define Py_UNICODE void |
| 43 | #endif |
| 44 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 45 | #if defined(PYOS_OS2) |
| 46 | #define INCL_DOS |
| 47 | #define INCL_DOSERRORS |
| 48 | #define INCL_DOSPROCESS |
| 49 | #define INCL_NOPMAPI |
| 50 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 51 | #if defined(PYCC_GCC) |
| 52 | #include <ctype.h> |
| 53 | #include <io.h> |
| 54 | #include <stdio.h> |
| 55 | #include <process.h> |
| 56 | #include "osdefs.h" |
| 57 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | #include <sys/types.h> |
| 61 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 63 | #ifdef HAVE_SYS_WAIT_H |
| 64 | #include <sys/wait.h> /* For WNOHANG */ |
| 65 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 67 | #ifdef HAVE_SIGNAL_H |
| 68 | #include <signal.h> |
| 69 | #endif |
| 70 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | #ifdef HAVE_FCNTL_H |
| 72 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 73 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 75 | #ifdef HAVE_GRP_H |
| 76 | #include <grp.h> |
| 77 | #endif |
| 78 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 79 | #ifdef HAVE_SYSEXITS_H |
| 80 | #include <sysexits.h> |
| 81 | #endif /* HAVE_SYSEXITS_H */ |
| 82 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 83 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 84 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 85 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 86 | #include <process.h> |
| 87 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 88 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 89 | #define HAVE_GETCWD 1 |
| 90 | #define HAVE_OPENDIR 1 |
| 91 | #define HAVE_SYSTEM 1 |
| 92 | #if defined(__OS2__) |
| 93 | #define HAVE_EXECV 1 |
| 94 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 95 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 96 | #include <process.h> |
| 97 | #else |
| 98 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 99 | #define HAVE_EXECV 1 |
| 100 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 101 | #define HAVE_OPENDIR 1 |
| 102 | #define HAVE_PIPE 1 |
| 103 | #define HAVE_POPEN 1 |
| 104 | #define HAVE_SYSTEM 1 |
| 105 | #define HAVE_WAIT 1 |
| 106 | #else |
| 107 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 108 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 109 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 110 | #define HAVE_EXECV 1 |
| 111 | #define HAVE_PIPE 1 |
| 112 | #define HAVE_POPEN 1 |
| 113 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 114 | #define HAVE_CWAIT 1 |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 115 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 116 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 117 | /* 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] | 118 | #else /* all other compilers */ |
| 119 | /* Unix functions that the configure script doesn't check for */ |
| 120 | #define HAVE_EXECV 1 |
| 121 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 122 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 123 | #define HAVE_FORK1 1 |
| 124 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | #define HAVE_GETCWD 1 |
| 126 | #define HAVE_GETEGID 1 |
| 127 | #define HAVE_GETEUID 1 |
| 128 | #define HAVE_GETGID 1 |
| 129 | #define HAVE_GETPPID 1 |
| 130 | #define HAVE_GETUID 1 |
| 131 | #define HAVE_KILL 1 |
| 132 | #define HAVE_OPENDIR 1 |
| 133 | #define HAVE_PIPE 1 |
| 134 | #define HAVE_POPEN 1 |
| 135 | #define HAVE_SYSTEM 1 |
| 136 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 137 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 138 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 139 | #endif /* _MSC_VER */ |
| 140 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 141 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 142 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 144 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 146 | #if defined(sun) && !defined(__SVR4) |
| 147 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 148 | extern int rename(const char *, const char *); |
| 149 | extern int pclose(FILE *); |
| 150 | extern int fclose(FILE *); |
Thomas Wouters | 0f954a4 | 2001-02-15 08:46:56 +0000 | [diff] [blame] | 151 | extern int fsync(int); |
| 152 | extern int lstat(const char *, struct stat *); |
| 153 | extern int symlink(const char *, const char *); |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 156 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 157 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 158 | (default) */ |
| 159 | extern char *ctermid_r(char *); |
| 160 | #endif |
| 161 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 162 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 163 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 164 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 165 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 166 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 167 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 168 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 170 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 171 | #endif |
| 172 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 173 | extern int chdir(char *); |
| 174 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 175 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 176 | extern int chdir(const char *); |
| 177 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 178 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 179 | #ifdef __BORLANDC__ |
| 180 | extern int chmod(const char *, int); |
| 181 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 182 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 183 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 184 | extern int chown(const char *, uid_t, gid_t); |
| 185 | extern char *getcwd(char *, int); |
| 186 | extern char *strerror(int); |
| 187 | extern int link(const char *, const char *); |
| 188 | extern int rename(const char *, const char *); |
| 189 | extern int stat(const char *, struct stat *); |
| 190 | extern int unlink(const char *); |
| 191 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 192 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 193 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 194 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 196 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 197 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 200 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_UTIME_H |
| 203 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 204 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 206 | #ifdef HAVE_SYS_UTIME_H |
| 207 | #include <sys/utime.h> |
| 208 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 209 | #endif /* HAVE_SYS_UTIME_H */ |
| 210 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | #ifdef HAVE_SYS_TIMES_H |
| 212 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 213 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 214 | |
| 215 | #ifdef HAVE_SYS_PARAM_H |
| 216 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 217 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | |
| 219 | #ifdef HAVE_SYS_UTSNAME_H |
| 220 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 221 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 223 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 225 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 226 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 227 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 228 | #include <direct.h> |
| 229 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 230 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 232 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 233 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 234 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 236 | #endif |
| 237 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 239 | #endif |
| 240 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #endif |
| 243 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 245 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 246 | #include <direct.h> |
| 247 | #include <io.h> |
| 248 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 249 | #include "osdefs.h" |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 250 | #define WIN32_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 252 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 253 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 254 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 255 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 257 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 259 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 260 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 261 | #ifndef MAXPATHLEN |
| 262 | #define MAXPATHLEN 1024 |
| 263 | #endif /* MAXPATHLEN */ |
| 264 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 265 | #ifdef UNION_WAIT |
| 266 | /* Emulate some macros on systems that have a union instead of macros */ |
| 267 | |
| 268 | #ifndef WIFEXITED |
| 269 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 270 | #endif |
| 271 | |
| 272 | #ifndef WEXITSTATUS |
| 273 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 274 | #endif |
| 275 | |
| 276 | #ifndef WTERMSIG |
| 277 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 278 | #endif |
| 279 | |
| 280 | #endif /* UNION_WAIT */ |
| 281 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 282 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 283 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 284 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 285 | #define USE_CTERMID_R |
| 286 | #endif |
| 287 | |
| 288 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 289 | #define USE_TMPNAM_R |
| 290 | #endif |
| 291 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 292 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 293 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 294 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 295 | # define STAT _stati64 |
| 296 | # define FSTAT _fstati64 |
| 297 | # define STRUCT_STAT struct _stati64 |
| 298 | #else |
| 299 | # define STAT stat |
| 300 | # define FSTAT fstat |
| 301 | # define STRUCT_STAT struct stat |
| 302 | #endif |
| 303 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 304 | #if defined(MAJOR_IN_MKDEV) |
| 305 | #include <sys/mkdev.h> |
| 306 | #else |
| 307 | #if defined(MAJOR_IN_SYSMACROS) |
| 308 | #include <sys/sysmacros.h> |
| 309 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 310 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 311 | #include <sys/mkdev.h> |
| 312 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 313 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 314 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 316 | #ifdef WITH_NEXT_FRAMEWORK |
| 317 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 318 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 319 | */ |
| 320 | #include <crt_externs.h> |
| 321 | static char **environ; |
| 322 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 324 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 326 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 327 | /* add some values to provide a similar environment like POSIX */ |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 328 | static |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 329 | void |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 330 | vms_add_posix_env(PyObject *d) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 331 | { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 332 | PyObject *o; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 333 | char* str; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 334 | |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 335 | str = getenv("LINES"); |
| 336 | o = Py_BuildValue("s", str); |
| 337 | if (o != NULL) { |
| 338 | (void)PyDict_SetItemString(d, "LINES", o); |
| 339 | Py_DECREF(o); |
| 340 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 341 | |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 342 | str = getenv("COLUMNS"); |
| 343 | o = Py_BuildValue("s", str); |
| 344 | if (o != NULL) { |
| 345 | (void)PyDict_SetItemString(d, "COLUMNS", o); |
| 346 | Py_DECREF(o); |
| 347 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 348 | |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 349 | str = getenv("USER"); |
| 350 | o = Py_BuildValue("s", str); |
| 351 | if (o != NULL) { |
| 352 | (void)PyDict_SetItemString(d, "USERNAME", o); |
| 353 | Py_DECREF(o); |
| 354 | } |
| 355 | o = Py_BuildValue("s", str); |
| 356 | if (o != NULL) { |
| 357 | (void)PyDict_SetItemString(d, "LOGNAME", o); |
| 358 | Py_DECREF(o); |
| 359 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 360 | |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 361 | str = getenv("HOME"); |
| 362 | o = Py_BuildValue("s", str); |
| 363 | if (o != NULL) { |
| 364 | (void)PyDict_SetItemString(d, "HOME", o); |
| 365 | Py_DECREF(o); |
| 366 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 367 | |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 368 | str = getenv("PATH"); |
| 369 | o = Py_BuildValue("s", str); |
| 370 | if (o != NULL) { |
| 371 | (void)PyDict_SetItemString(d, "PATH", o); |
| 372 | Py_DECREF(o); |
| 373 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 374 | /* OS = "OpenVMS" */ |
| 375 | o = PyString_FromString ("OpenVMS"); |
| 376 | if (o != NULL) { |
| 377 | (void)PyDict_SetItemString(d, "OS", o); |
| 378 | Py_DECREF(o); |
| 379 | } |
| 380 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 381 | #endif /* __VMS */ |
| 382 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 383 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 384 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 385 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 386 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 387 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 388 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 389 | if (d == NULL) |
| 390 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 391 | #ifdef WITH_NEXT_FRAMEWORK |
| 392 | if (environ == NULL) |
| 393 | environ = *_NSGetEnviron(); |
| 394 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 395 | if (environ == NULL) |
| 396 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 397 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 398 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 399 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 400 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 401 | char *p = strchr(*e, '='); |
| 402 | if (p == NULL) |
| 403 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 404 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 405 | if (k == NULL) { |
| 406 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 407 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 408 | } |
| 409 | v = PyString_FromString(p+1); |
| 410 | if (v == NULL) { |
| 411 | PyErr_Clear(); |
| 412 | Py_DECREF(k); |
| 413 | continue; |
| 414 | } |
| 415 | if (PyDict_GetItem(d, k) == NULL) { |
| 416 | if (PyDict_SetItem(d, k, v) != 0) |
| 417 | PyErr_Clear(); |
| 418 | } |
| 419 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 420 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 421 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 422 | #if defined(__VMS) |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 423 | vms_add_posix_env(d); |
| 424 | #elif defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 425 | { |
| 426 | APIRET rc; |
| 427 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 428 | |
| 429 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 430 | 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] | 431 | PyObject *v = PyString_FromString(buffer); |
| 432 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 433 | Py_DECREF(v); |
| 434 | } |
| 435 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 436 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 437 | PyObject *v = PyString_FromString(buffer); |
| 438 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 439 | Py_DECREF(v); |
| 440 | } |
| 441 | } |
| 442 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 443 | return d; |
| 444 | } |
| 445 | |
| 446 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 447 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 448 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 449 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 450 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 451 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 452 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 453 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 454 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 455 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 456 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 457 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 460 | #ifdef Py_WIN_WIDE_FILENAMES |
| 461 | static PyObject * |
| 462 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 463 | { |
| 464 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 465 | } |
| 466 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 467 | |
| 468 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 469 | static PyObject * |
| 470 | posix_error_with_allocated_filename(char* name) |
| 471 | { |
| 472 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 473 | PyMem_Free(name); |
| 474 | return rc; |
| 475 | } |
| 476 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 477 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 478 | static PyObject * |
| 479 | win32_error(char* function, char* filename) |
| 480 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 481 | /* XXX We should pass the function name along in the future. |
| 482 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 483 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 484 | Windows error object, which is non-trivial. |
| 485 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 486 | errno = GetLastError(); |
| 487 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 488 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 489 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 490 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 491 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 492 | |
| 493 | #ifdef Py_WIN_WIDE_FILENAMES |
| 494 | static PyObject * |
| 495 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 496 | { |
| 497 | /* XXX - see win32_error for comments on 'function' */ |
| 498 | errno = GetLastError(); |
| 499 | if (filename) |
| 500 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 501 | else |
| 502 | return PyErr_SetFromWindowsErr(errno); |
| 503 | } |
| 504 | |
| 505 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 506 | { |
| 507 | /* XXX Perhaps we should make this API an alias of |
| 508 | PyObject_Unicode() instead ?! */ |
| 509 | if (PyUnicode_CheckExact(obj)) { |
| 510 | Py_INCREF(obj); |
| 511 | return obj; |
| 512 | } |
| 513 | if (PyUnicode_Check(obj)) { |
| 514 | /* For a Unicode subtype that's not a Unicode object, |
| 515 | return a true Unicode object with the same data. */ |
| 516 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 517 | PyUnicode_GET_SIZE(obj)); |
| 518 | } |
| 519 | return PyUnicode_FromEncodedObject(obj, |
| 520 | Py_FileSystemDefaultEncoding, |
| 521 | "strict"); |
| 522 | } |
| 523 | |
| 524 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 525 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 526 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 527 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 528 | #if defined(PYOS_OS2) |
| 529 | /********************************************************************** |
| 530 | * Helper Function to Trim and Format OS/2 Messages |
| 531 | **********************************************************************/ |
| 532 | static void |
| 533 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 534 | { |
| 535 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 536 | |
| 537 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 538 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 539 | |
| 540 | while (lastc > msgbuf && isspace(*lastc)) |
| 541 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 542 | } |
| 543 | |
| 544 | /* Add Optional Reason Text */ |
| 545 | if (reason) { |
| 546 | strcat(msgbuf, " : "); |
| 547 | strcat(msgbuf, reason); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /********************************************************************** |
| 552 | * Decode an OS/2 Operating System Error Code |
| 553 | * |
| 554 | * A convenience function to lookup an OS/2 error code and return a |
| 555 | * text message we can use to raise a Python exception. |
| 556 | * |
| 557 | * Notes: |
| 558 | * The messages for errors returned from the OS/2 kernel reside in |
| 559 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 560 | * |
| 561 | **********************************************************************/ |
| 562 | static char * |
| 563 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 564 | { |
| 565 | APIRET rc; |
| 566 | ULONG msglen; |
| 567 | |
| 568 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 569 | Py_BEGIN_ALLOW_THREADS |
| 570 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 571 | errorcode, "oso001.msg", &msglen); |
| 572 | Py_END_ALLOW_THREADS |
| 573 | |
| 574 | if (rc == NO_ERROR) |
| 575 | os2_formatmsg(msgbuf, msglen, reason); |
| 576 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 577 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 578 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 579 | |
| 580 | return msgbuf; |
| 581 | } |
| 582 | |
| 583 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 584 | errors are not in a global variable e.g. 'errno' nor are |
| 585 | they congruent with posix error numbers. */ |
| 586 | |
| 587 | static PyObject * os2_error(int code) |
| 588 | { |
| 589 | char text[1024]; |
| 590 | PyObject *v; |
| 591 | |
| 592 | os2_strerror(text, sizeof(text), code, ""); |
| 593 | |
| 594 | v = Py_BuildValue("(is)", code, text); |
| 595 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 596 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 597 | Py_DECREF(v); |
| 598 | } |
| 599 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 600 | } |
| 601 | |
| 602 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 603 | |
| 604 | /* POSIX generic methods */ |
| 605 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 606 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 607 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 608 | { |
| 609 | int fd; |
| 610 | int res; |
| 611 | fd = PyObject_AsFileDescriptor(fdobj); |
| 612 | if (fd < 0) |
| 613 | return NULL; |
| 614 | Py_BEGIN_ALLOW_THREADS |
| 615 | res = (*func)(fd); |
| 616 | Py_END_ALLOW_THREADS |
| 617 | if (res < 0) |
| 618 | return posix_error(); |
| 619 | Py_INCREF(Py_None); |
| 620 | return Py_None; |
| 621 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 622 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 623 | #ifdef Py_WIN_WIDE_FILENAMES |
| 624 | static int |
| 625 | unicode_file_names(void) |
| 626 | { |
| 627 | static int canusewide = -1; |
| 628 | if (canusewide == -1) { |
| 629 | /* As per doc for ::GetVersion(), this is the correct test for |
| 630 | the Windows NT family. */ |
| 631 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 632 | } |
| 633 | return canusewide; |
| 634 | } |
| 635 | #endif |
| 636 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 637 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 638 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 639 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 640 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 641 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 642 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 643 | #ifdef Py_WIN_WIDE_FILENAMES |
| 644 | if (unicode_file_names()) { |
| 645 | PyUnicodeObject *po; |
| 646 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 647 | Py_BEGIN_ALLOW_THREADS |
| 648 | /* PyUnicode_AS_UNICODE OK without thread |
| 649 | lock as it is a simple dereference. */ |
| 650 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 651 | Py_END_ALLOW_THREADS |
| 652 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 653 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 654 | Py_INCREF(Py_None); |
| 655 | return Py_None; |
| 656 | } |
| 657 | /* Drop the argument parsing error as narrow |
| 658 | strings are also valid. */ |
| 659 | PyErr_Clear(); |
| 660 | } |
| 661 | #else |
| 662 | /* Platforms that don't support Unicode filenames |
| 663 | shouldn't be passing these extra params */ |
| 664 | assert(wformat==NULL && wfunc == NULL); |
| 665 | #endif |
| 666 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 667 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 668 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 669 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 670 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 671 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 672 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 673 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 674 | return posix_error_with_allocated_filename(path1); |
| 675 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 676 | Py_INCREF(Py_None); |
| 677 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 680 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 681 | posix_2str(PyObject *args, |
| 682 | char *format, |
| 683 | int (*func)(const char *, const char *), |
| 684 | char *wformat, |
| 685 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 686 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 687 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 688 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 689 | #ifdef Py_WIN_WIDE_FILENAMES |
| 690 | if (unicode_file_names()) { |
| 691 | PyObject *po1; |
| 692 | PyObject *po2; |
| 693 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 694 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 695 | PyObject *wpath1; |
| 696 | PyObject *wpath2; |
| 697 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 698 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 699 | if (!wpath1 || !wpath2) { |
| 700 | Py_XDECREF(wpath1); |
| 701 | Py_XDECREF(wpath2); |
| 702 | return NULL; |
| 703 | } |
| 704 | Py_BEGIN_ALLOW_THREADS |
| 705 | /* PyUnicode_AS_UNICODE OK without thread |
| 706 | lock as it is a simple dereference. */ |
| 707 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 708 | PyUnicode_AS_UNICODE(wpath2)); |
| 709 | Py_END_ALLOW_THREADS |
| 710 | Py_XDECREF(wpath1); |
| 711 | Py_XDECREF(wpath2); |
| 712 | if (res != 0) |
| 713 | return posix_error(); |
| 714 | Py_INCREF(Py_None); |
| 715 | return Py_None; |
| 716 | } |
| 717 | /* Else flow through as neither is Unicode. */ |
| 718 | } |
| 719 | /* Drop the argument parsing error as narrow |
| 720 | strings are also valid. */ |
| 721 | PyErr_Clear(); |
| 722 | } |
| 723 | #else |
| 724 | /* Platforms that don't support Unicode filenames |
| 725 | shouldn't be passing these extra params */ |
| 726 | assert(wformat==NULL && wfunc == NULL); |
| 727 | #endif |
| 728 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 729 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 730 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 731 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 732 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 733 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 734 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 735 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 736 | PyMem_Free(path1); |
| 737 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 738 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 739 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 740 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 741 | Py_INCREF(Py_None); |
| 742 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 745 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 746 | "stat_result: Result from stat or lstat.\n\n\ |
| 747 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 748 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 749 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 750 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 751 | Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 752 | they are available as attributes only.\n\ |
| 753 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 754 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 755 | |
| 756 | static PyStructSequence_Field stat_result_fields[] = { |
| 757 | {"st_mode", "protection bits"}, |
| 758 | {"st_ino", "inode"}, |
| 759 | {"st_dev", "device"}, |
| 760 | {"st_nlink", "number of hard links"}, |
| 761 | {"st_uid", "user ID of owner"}, |
| 762 | {"st_gid", "group ID of owner"}, |
| 763 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 764 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 765 | {NULL, "integer time of last access"}, |
| 766 | {NULL, "integer time of last modification"}, |
| 767 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 768 | {"st_atime", "time of last access"}, |
| 769 | {"st_mtime", "time of last modification"}, |
| 770 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 771 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 772 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 773 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 774 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 775 | {"st_blocks", "number of blocks allocated"}, |
| 776 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 777 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 778 | {"st_rdev", "device type (if inode device)"}, |
| 779 | #endif |
| 780 | {0} |
| 781 | }; |
| 782 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 783 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 784 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 785 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 786 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 787 | #endif |
| 788 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 789 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 790 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 791 | #else |
| 792 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 793 | #endif |
| 794 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 795 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 796 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 797 | #else |
| 798 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 799 | #endif |
| 800 | |
| 801 | static PyStructSequence_Desc stat_result_desc = { |
| 802 | "stat_result", /* name */ |
| 803 | stat_result__doc__, /* doc */ |
| 804 | stat_result_fields, |
| 805 | 10 |
| 806 | }; |
| 807 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 808 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 809 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 810 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 811 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 812 | 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] | 813 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 814 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 815 | |
| 816 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 817 | {"f_bsize", }, |
| 818 | {"f_frsize", }, |
| 819 | {"f_blocks", }, |
| 820 | {"f_bfree", }, |
| 821 | {"f_bavail", }, |
| 822 | {"f_files", }, |
| 823 | {"f_ffree", }, |
| 824 | {"f_favail", }, |
| 825 | {"f_flag", }, |
| 826 | {"f_namemax",}, |
| 827 | {0} |
| 828 | }; |
| 829 | |
| 830 | static PyStructSequence_Desc statvfs_result_desc = { |
| 831 | "statvfs_result", /* name */ |
| 832 | statvfs_result__doc__, /* doc */ |
| 833 | statvfs_result_fields, |
| 834 | 10 |
| 835 | }; |
| 836 | |
| 837 | static PyTypeObject StatResultType; |
| 838 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 839 | static newfunc structseq_new; |
| 840 | |
| 841 | static PyObject * |
| 842 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 843 | { |
| 844 | PyStructSequence *result; |
| 845 | int i; |
| 846 | |
| 847 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 848 | if (!result) |
| 849 | return NULL; |
| 850 | /* If we have been initialized from a tuple, |
| 851 | st_?time might be set to None. Initialize it |
| 852 | from the int slots. */ |
| 853 | for (i = 7; i <= 9; i++) { |
| 854 | if (result->ob_item[i+3] == Py_None) { |
| 855 | Py_DECREF(Py_None); |
| 856 | Py_INCREF(result->ob_item[i]); |
| 857 | result->ob_item[i+3] = result->ob_item[i]; |
| 858 | } |
| 859 | } |
| 860 | return (PyObject*)result; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | |
| 865 | /* If true, st_?time is float. */ |
| 866 | static int _stat_float_times = 0; |
| 867 | |
| 868 | PyDoc_STRVAR(stat_float_times__doc__, |
| 869 | "stat_float_times([newval]) -> oldval\n\n\ |
| 870 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 871 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 872 | future calls return ints. \n\ |
| 873 | If newval is omitted, return the current setting.\n"); |
| 874 | |
| 875 | static PyObject* |
| 876 | stat_float_times(PyObject* self, PyObject *args) |
| 877 | { |
| 878 | int newval = -1; |
| 879 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 880 | return NULL; |
| 881 | if (newval == -1) |
| 882 | /* Return old value */ |
| 883 | return PyBool_FromLong(_stat_float_times); |
| 884 | _stat_float_times = newval; |
| 885 | Py_INCREF(Py_None); |
| 886 | return Py_None; |
| 887 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 888 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 889 | static void |
| 890 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 891 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 892 | PyObject *fval,*ival; |
| 893 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 894 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 895 | #else |
| 896 | ival = PyInt_FromLong((long)sec); |
| 897 | #endif |
| 898 | if (_stat_float_times) { |
| 899 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 900 | } else { |
| 901 | fval = ival; |
| 902 | Py_INCREF(fval); |
| 903 | } |
| 904 | PyStructSequence_SET_ITEM(v, index, ival); |
| 905 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 908 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 909 | (used by posix_stat() and posix_fstat()) */ |
| 910 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 911 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 912 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 913 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 914 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 915 | if (v == NULL) |
| 916 | return NULL; |
| 917 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 918 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 919 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 920 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 921 | PyLong_FromLongLong((PY_LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 922 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 923 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 924 | #endif |
| 925 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 926 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 927 | PyLong_FromLongLong((PY_LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 928 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 929 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 930 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 931 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 932 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 933 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 934 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 935 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 936 | PyLong_FromLongLong((PY_LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 937 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 938 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 939 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 940 | |
| 941 | #ifdef HAVE_STAT_TV_NSEC |
| 942 | ansec = st.st_atim.tv_nsec; |
| 943 | mnsec = st.st_mtim.tv_nsec; |
| 944 | cnsec = st.st_ctim.tv_nsec; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 945 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 946 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 947 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 948 | fill_time(v, 7, st.st_atime, ansec); |
| 949 | fill_time(v, 8, st.st_mtime, mnsec); |
| 950 | fill_time(v, 9, st.st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 951 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 952 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 953 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 954 | PyInt_FromLong((long)st.st_blksize)); |
| 955 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 956 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 957 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 958 | PyInt_FromLong((long)st.st_blocks)); |
| 959 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 960 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 961 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 962 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 963 | #endif |
| 964 | |
| 965 | if (PyErr_Occurred()) { |
| 966 | Py_DECREF(v); |
| 967 | return NULL; |
| 968 | } |
| 969 | |
| 970 | return v; |
| 971 | } |
| 972 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 973 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 974 | posix_do_stat(PyObject *self, PyObject *args, |
| 975 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 976 | #ifdef __VMS |
| 977 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 978 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 979 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 980 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 981 | char *wformat, |
| 982 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 983 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 984 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 985 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 986 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 987 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 988 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 989 | #ifdef MS_WINDOWS |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 990 | int pathlen; |
| 991 | char pathcopy[MAX_PATH]; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 992 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 993 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 994 | |
| 995 | #ifdef Py_WIN_WIDE_FILENAMES |
| 996 | /* If on wide-character-capable OS see if argument |
| 997 | is Unicode and if so use wide API. */ |
| 998 | if (unicode_file_names()) { |
| 999 | PyUnicodeObject *po; |
| 1000 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1001 | Py_UNICODE wpath[MAX_PATH+1]; |
| 1002 | pathlen = wcslen(PyUnicode_AS_UNICODE(po)); |
| 1003 | /* the library call can blow up if the file name is too long! */ |
| 1004 | if (pathlen > MAX_PATH) { |
| 1005 | errno = ENAMETOOLONG; |
| 1006 | return posix_error(); |
| 1007 | } |
| 1008 | wcscpy(wpath, PyUnicode_AS_UNICODE(po)); |
| 1009 | /* Remove trailing slash or backslash, unless it's the current |
| 1010 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 1011 | */ |
| 1012 | if (pathlen > 0 && |
| 1013 | (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) { |
| 1014 | /* It does end with a slash -- exempt the root drive cases. */ |
| 1015 | /* XXX UNC root drives should also be exempted? */ |
| 1016 | if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':')) |
| 1017 | /* leave it alone */; |
| 1018 | else { |
| 1019 | /* nuke the trailing backslash */ |
| 1020 | wpath[pathlen-1] = L'\0'; |
| 1021 | } |
| 1022 | } |
| 1023 | Py_BEGIN_ALLOW_THREADS |
| 1024 | /* PyUnicode_AS_UNICODE result OK without |
| 1025 | thread lock as it is a simple dereference. */ |
| 1026 | res = wstatfunc(wpath, &st); |
| 1027 | Py_END_ALLOW_THREADS |
| 1028 | if (res != 0) |
| 1029 | return posix_error_with_unicode_filename(wpath); |
| 1030 | return _pystat_fromstructstat(st); |
| 1031 | } |
| 1032 | /* Drop the argument parsing error as narrow strings |
| 1033 | are also valid. */ |
| 1034 | PyErr_Clear(); |
| 1035 | } |
| 1036 | #endif |
| 1037 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1038 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1039 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1040 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1041 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1042 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1043 | #ifdef MS_WINDOWS |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1044 | pathlen = strlen(path); |
| 1045 | /* the library call can blow up if the file name is too long! */ |
| 1046 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1047 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1048 | errno = ENAMETOOLONG; |
| 1049 | return posix_error(); |
| 1050 | } |
| 1051 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1052 | /* Remove trailing slash or backslash, unless it's the current |
| 1053 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 1054 | */ |
| 1055 | if (pathlen > 0 && |
| 1056 | (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) { |
| 1057 | /* It does end with a slash -- exempt the root drive cases. */ |
| 1058 | /* XXX UNC root drives should also be exempted? */ |
| 1059 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':')) |
| 1060 | /* leave it alone */; |
| 1061 | else { |
| 1062 | /* nuke the trailing backslash */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1063 | strncpy(pathcopy, path, pathlen); |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1064 | pathcopy[pathlen-1] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1065 | path = pathcopy; |
| 1066 | } |
| 1067 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1068 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1069 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1070 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1071 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1072 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1073 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1074 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1075 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1076 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1077 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | /* POSIX methods */ |
| 1082 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1083 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1084 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1085 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1086 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1087 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1088 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1089 | 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] | 1090 | |
| 1091 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1092 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1093 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1094 | char *path; |
| 1095 | int mode; |
| 1096 | int res; |
| 1097 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1098 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1099 | return NULL; |
| 1100 | Py_BEGIN_ALLOW_THREADS |
| 1101 | res = access(path, mode); |
| 1102 | Py_END_ALLOW_THREADS |
Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 1103 | return(PyBool_FromLong(res == 0)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1106 | #ifndef F_OK |
| 1107 | #define F_OK 0 |
| 1108 | #endif |
| 1109 | #ifndef R_OK |
| 1110 | #define R_OK 4 |
| 1111 | #endif |
| 1112 | #ifndef W_OK |
| 1113 | #define W_OK 2 |
| 1114 | #endif |
| 1115 | #ifndef X_OK |
| 1116 | #define X_OK 1 |
| 1117 | #endif |
| 1118 | |
| 1119 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1120 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1121 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1122 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1123 | |
| 1124 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1125 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1126 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1127 | int id; |
| 1128 | char *ret; |
| 1129 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1130 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1131 | return NULL; |
| 1132 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1133 | #if defined(__VMS) |
| 1134 | /* DECC V5.0 - only about FD= 0 @@ try getname()+$getdvi(dvi$_devnam) */ |
| 1135 | if (id == 0) { |
| 1136 | ret = ttyname(); |
| 1137 | } |
| 1138 | else { |
| 1139 | ret = NULL; |
| 1140 | } |
| 1141 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1142 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1143 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1144 | if (ret == NULL) |
| 1145 | return(posix_error()); |
| 1146 | return(PyString_FromString(ret)); |
| 1147 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1148 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1149 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1150 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1151 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1152 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1153 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1154 | |
| 1155 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1156 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1157 | { |
| 1158 | char *ret; |
| 1159 | char buffer[L_ctermid]; |
| 1160 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1161 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1162 | ret = ctermid_r(buffer); |
| 1163 | #else |
| 1164 | ret = ctermid(buffer); |
| 1165 | #endif |
| 1166 | if (ret == NULL) |
| 1167 | return(posix_error()); |
| 1168 | return(PyString_FromString(buffer)); |
| 1169 | } |
| 1170 | #endif |
| 1171 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1172 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1173 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1174 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1175 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1176 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1177 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1178 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1179 | #ifdef MS_WINDOWS |
| 1180 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1181 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1182 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1183 | #elif defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1184 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
| 1185 | NULL, NULL); |
| 1186 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1187 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1188 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1191 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1192 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1193 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1194 | 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] | 1195 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1196 | |
| 1197 | static PyObject * |
| 1198 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1199 | { |
| 1200 | return posix_fildes(fdobj, fchdir); |
| 1201 | } |
| 1202 | #endif /* HAVE_FCHDIR */ |
| 1203 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1205 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1206 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1207 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1208 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1209 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1210 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1211 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1212 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1213 | int i; |
| 1214 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1215 | if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1216 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1217 | return NULL; |
| 1218 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1219 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1220 | Py_END_ALLOW_THREADS |
| 1221 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1222 | return posix_error_with_allocated_filename(path); |
| 1223 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1224 | Py_INCREF(Py_None); |
| 1225 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1228 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1229 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1230 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1231 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1232 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1233 | |
| 1234 | static PyObject * |
| 1235 | posix_chroot(PyObject *self, PyObject *args) |
| 1236 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1237 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1238 | } |
| 1239 | #endif |
| 1240 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1241 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1242 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1243 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1244 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1245 | |
| 1246 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1247 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1248 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1249 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1250 | } |
| 1251 | #endif /* HAVE_FSYNC */ |
| 1252 | |
| 1253 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1254 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1255 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1256 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1257 | #endif |
| 1258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1259 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1260 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1261 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1262 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1263 | |
| 1264 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1265 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1266 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1267 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1268 | } |
| 1269 | #endif /* HAVE_FDATASYNC */ |
| 1270 | |
| 1271 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1272 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1273 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1274 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1275 | 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] | 1276 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1277 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1278 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1279 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1280 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1281 | int uid, gid; |
| 1282 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1283 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1284 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1285 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1286 | return NULL; |
| 1287 | Py_BEGIN_ALLOW_THREADS |
| 1288 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1289 | Py_END_ALLOW_THREADS |
| 1290 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1291 | return posix_error_with_allocated_filename(path); |
| 1292 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1293 | Py_INCREF(Py_None); |
| 1294 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1295 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1296 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1297 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1298 | #ifdef HAVE_LCHOWN |
| 1299 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1300 | "lchown(path, uid, gid)\n\n\ |
| 1301 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1302 | This function will not follow symbolic links."); |
| 1303 | |
| 1304 | static PyObject * |
| 1305 | posix_lchown(PyObject *self, PyObject *args) |
| 1306 | { |
| 1307 | char *path = NULL; |
| 1308 | int uid, gid; |
| 1309 | int res; |
| 1310 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1311 | Py_FileSystemDefaultEncoding, &path, |
| 1312 | &uid, &gid)) |
| 1313 | return NULL; |
| 1314 | Py_BEGIN_ALLOW_THREADS |
| 1315 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1316 | Py_END_ALLOW_THREADS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1317 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1318 | return posix_error_with_allocated_filename(path); |
| 1319 | PyMem_Free(path); |
| 1320 | Py_INCREF(Py_None); |
| 1321 | return Py_None; |
| 1322 | } |
| 1323 | #endif /* HAVE_LCHOWN */ |
| 1324 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1325 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1326 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1327 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1328 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1329 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1330 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1331 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1332 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1333 | { |
| 1334 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1335 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1336 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1337 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1338 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1339 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1340 | #elif defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1341 | /* 0 = force Unix-style path if in the VMS DCL environment! */ |
| 1342 | res = getcwd(buf, sizeof buf, 0); |
| 1343 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1344 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1345 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1346 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1347 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1348 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1349 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1350 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1351 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1352 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1353 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1354 | "getcwdu() -> path\n\n\ |
| 1355 | Return a unicode string representing the current working directory."); |
| 1356 | |
| 1357 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1358 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1359 | { |
| 1360 | char buf[1026]; |
| 1361 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1362 | |
| 1363 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1364 | if (unicode_file_names()) { |
| 1365 | wchar_t *wres; |
| 1366 | wchar_t wbuf[1026]; |
| 1367 | Py_BEGIN_ALLOW_THREADS |
| 1368 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1369 | Py_END_ALLOW_THREADS |
| 1370 | if (wres == NULL) |
| 1371 | return posix_error(); |
| 1372 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1373 | } |
| 1374 | #endif |
| 1375 | |
| 1376 | Py_BEGIN_ALLOW_THREADS |
| 1377 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1378 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1379 | #elif defined(__VMS) |
| 1380 | /* 0 = force Unix-style path if in the VMS DCL environment! */ |
| 1381 | res = getcwd(buf, sizeof buf, 0); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1382 | #else |
| 1383 | res = getcwd(buf, sizeof buf); |
| 1384 | #endif |
| 1385 | Py_END_ALLOW_THREADS |
| 1386 | if (res == NULL) |
| 1387 | return posix_error(); |
| 1388 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1389 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1390 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1391 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1392 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1393 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1394 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1395 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1396 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1397 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1398 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1399 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1400 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1401 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1402 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1403 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1404 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1405 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1407 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1408 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1409 | Return a list containing the names of the entries in the directory.\n\ |
| 1410 | \n\ |
| 1411 | path: path of directory to list\n\ |
| 1412 | \n\ |
| 1413 | 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] | 1414 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1415 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1416 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1417 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1418 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1419 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1420 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1421 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1422 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1423 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1424 | HANDLE hFindFile; |
| 1425 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1426 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1427 | char namebuf[MAX_PATH*2+5]; |
| 1428 | char *bufptr = namebuf; |
| 1429 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1430 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1431 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1432 | /* If on wide-character-capable OS see if argument |
| 1433 | is Unicode and if so use wide API. */ |
| 1434 | if (unicode_file_names()) { |
| 1435 | PyUnicodeObject *po; |
| 1436 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1437 | WIN32_FIND_DATAW wFileData; |
| 1438 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1439 | Py_UNICODE wch; |
| 1440 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1441 | wnamebuf[MAX_PATH] = L'\0'; |
| 1442 | len = wcslen(wnamebuf); |
| 1443 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1444 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1445 | wnamebuf[len++] = L'/'; |
| 1446 | wcscpy(wnamebuf + len, L"*.*"); |
| 1447 | if ((d = PyList_New(0)) == NULL) |
| 1448 | return NULL; |
| 1449 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1450 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1451 | errno = GetLastError(); |
| 1452 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1453 | return d; |
| 1454 | } |
| 1455 | Py_DECREF(d); |
| 1456 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1457 | } |
| 1458 | do { |
| 1459 | if (wFileData.cFileName[0] == L'.' && |
| 1460 | (wFileData.cFileName[1] == L'\0' || |
| 1461 | wFileData.cFileName[1] == L'.' && |
| 1462 | wFileData.cFileName[2] == L'\0')) |
| 1463 | continue; |
| 1464 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1465 | if (v == NULL) { |
| 1466 | Py_DECREF(d); |
| 1467 | d = NULL; |
| 1468 | break; |
| 1469 | } |
| 1470 | if (PyList_Append(d, v) != 0) { |
| 1471 | Py_DECREF(v); |
| 1472 | Py_DECREF(d); |
| 1473 | d = NULL; |
| 1474 | break; |
| 1475 | } |
| 1476 | Py_DECREF(v); |
| 1477 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); |
| 1478 | |
| 1479 | if (FindClose(hFindFile) == FALSE) { |
| 1480 | Py_DECREF(d); |
| 1481 | return win32_error_unicode("FindClose", wnamebuf); |
| 1482 | } |
| 1483 | return d; |
| 1484 | } |
| 1485 | /* Drop the argument parsing error as narrow strings |
| 1486 | are also valid. */ |
| 1487 | PyErr_Clear(); |
| 1488 | } |
| 1489 | #endif |
| 1490 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1491 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1492 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1493 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1494 | if (len > 0) { |
| 1495 | char ch = namebuf[len-1]; |
| 1496 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1497 | namebuf[len++] = '/'; |
| 1498 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1499 | strcpy(namebuf + len, "*.*"); |
| 1500 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1501 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1502 | return NULL; |
| 1503 | |
| 1504 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1505 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1506 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1507 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1508 | return d; |
| 1509 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1510 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1511 | } |
| 1512 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1513 | if (FileData.cFileName[0] == '.' && |
| 1514 | (FileData.cFileName[1] == '\0' || |
| 1515 | FileData.cFileName[1] == '.' && |
| 1516 | FileData.cFileName[2] == '\0')) |
| 1517 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1518 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1519 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1520 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1521 | d = NULL; |
| 1522 | break; |
| 1523 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1524 | if (PyList_Append(d, v) != 0) { |
| 1525 | Py_DECREF(v); |
| 1526 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1527 | d = NULL; |
| 1528 | break; |
| 1529 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1530 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1531 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1532 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1533 | if (FindClose(hFindFile) == FALSE) { |
| 1534 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1535 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1536 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1537 | |
| 1538 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1539 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1540 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1541 | |
| 1542 | #ifndef MAX_PATH |
| 1543 | #define MAX_PATH CCHMAXPATH |
| 1544 | #endif |
| 1545 | char *name, *pt; |
| 1546 | int len; |
| 1547 | PyObject *d, *v; |
| 1548 | char namebuf[MAX_PATH+5]; |
| 1549 | HDIR hdir = 1; |
| 1550 | ULONG srchcnt = 1; |
| 1551 | FILEFINDBUF3 ep; |
| 1552 | APIRET rc; |
| 1553 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1554 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1555 | return NULL; |
| 1556 | if (len >= MAX_PATH) { |
| 1557 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1558 | return NULL; |
| 1559 | } |
| 1560 | strcpy(namebuf, name); |
| 1561 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1562 | if (*pt == ALTSEP) |
| 1563 | *pt = SEP; |
| 1564 | if (namebuf[len-1] != SEP) |
| 1565 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1566 | strcpy(namebuf + len, "*.*"); |
| 1567 | |
| 1568 | if ((d = PyList_New(0)) == NULL) |
| 1569 | return NULL; |
| 1570 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1571 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1572 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1573 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1574 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1575 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1576 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1577 | |
| 1578 | if (rc != NO_ERROR) { |
| 1579 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1580 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1583 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1584 | do { |
| 1585 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1586 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1587 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1588 | |
| 1589 | strcpy(namebuf, ep.achName); |
| 1590 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1591 | /* Leave Case of Name Alone -- In Native Form */ |
| 1592 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1593 | |
| 1594 | v = PyString_FromString(namebuf); |
| 1595 | if (v == NULL) { |
| 1596 | Py_DECREF(d); |
| 1597 | d = NULL; |
| 1598 | break; |
| 1599 | } |
| 1600 | if (PyList_Append(d, v) != 0) { |
| 1601 | Py_DECREF(v); |
| 1602 | Py_DECREF(d); |
| 1603 | d = NULL; |
| 1604 | break; |
| 1605 | } |
| 1606 | Py_DECREF(v); |
| 1607 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1608 | } |
| 1609 | |
| 1610 | return d; |
| 1611 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1612 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1613 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1614 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1615 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1616 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1617 | int arg_is_unicode = 1; |
| 1618 | |
| 1619 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1620 | arg_is_unicode = 0; |
| 1621 | PyErr_Clear(); |
| 1622 | } |
| 1623 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1624 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1625 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1626 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1627 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1628 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1629 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1630 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1631 | return NULL; |
| 1632 | } |
| 1633 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1634 | if (ep->d_name[0] == '.' && |
| 1635 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1636 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1637 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1638 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1639 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1640 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1641 | d = NULL; |
| 1642 | break; |
| 1643 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1644 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1645 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1646 | PyObject *w; |
| 1647 | |
| 1648 | w = PyUnicode_FromEncodedObject(v, |
| 1649 | Py_FileSystemDefaultEncoding, |
| 1650 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1651 | if (w != NULL) { |
| 1652 | Py_DECREF(v); |
| 1653 | v = w; |
| 1654 | } |
| 1655 | else { |
| 1656 | /* fall back to the original byte string, as |
| 1657 | discussed in patch #683592 */ |
| 1658 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1659 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1660 | } |
| 1661 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1662 | if (PyList_Append(d, v) != 0) { |
| 1663 | Py_DECREF(v); |
| 1664 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1665 | d = NULL; |
| 1666 | break; |
| 1667 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1668 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1669 | } |
| 1670 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1671 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1672 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1673 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1674 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1675 | #endif /* which OS */ |
| 1676 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1677 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1678 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1679 | /* A helper function for abspath on win32 */ |
| 1680 | static PyObject * |
| 1681 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1682 | { |
| 1683 | /* assume encoded strings wont more than double no of chars */ |
| 1684 | char inbuf[MAX_PATH*2]; |
| 1685 | char *inbufp = inbuf; |
| 1686 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1687 | char outbuf[MAX_PATH*2]; |
| 1688 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1689 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1690 | if (unicode_file_names()) { |
| 1691 | PyUnicodeObject *po; |
| 1692 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1693 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1694 | Py_UNICODE *wtemp; |
| 1695 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
| 1696 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1697 | woutbuf, &wtemp)) |
| 1698 | return win32_error("GetFullPathName", ""); |
| 1699 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1700 | } |
| 1701 | /* Drop the argument parsing error as narrow strings |
| 1702 | are also valid. */ |
| 1703 | PyErr_Clear(); |
| 1704 | } |
| 1705 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1706 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1707 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1708 | &insize)) |
| 1709 | return NULL; |
| 1710 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1711 | outbuf, &temp)) |
| 1712 | return win32_error("GetFullPathName", inbuf); |
| 1713 | return PyString_FromString(outbuf); |
| 1714 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1715 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1717 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1718 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1719 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1721 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1722 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1723 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1724 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1725 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1726 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1727 | |
| 1728 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1729 | if (unicode_file_names()) { |
| 1730 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1731 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1732 | Py_BEGIN_ALLOW_THREADS |
| 1733 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1734 | it is a simple dereference. */ |
| 1735 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1736 | Py_END_ALLOW_THREADS |
| 1737 | if (res < 0) |
| 1738 | return posix_error(); |
| 1739 | Py_INCREF(Py_None); |
| 1740 | return Py_None; |
| 1741 | } |
| 1742 | /* Drop the argument parsing error as narrow strings |
| 1743 | are also valid. */ |
| 1744 | PyErr_Clear(); |
| 1745 | } |
| 1746 | #endif |
| 1747 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1748 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1749 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1750 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1751 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1752 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1753 | res = mkdir(path); |
| 1754 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1755 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1756 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1757 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1758 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1759 | return posix_error_with_allocated_filename(path); |
| 1760 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1761 | Py_INCREF(Py_None); |
| 1762 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1765 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1766 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1767 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1768 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1769 | #include <sys/resource.h> |
| 1770 | #endif |
| 1771 | #endif |
| 1772 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1773 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1774 | "nice(inc) -> new_priority\n\n\ |
| 1775 | 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] | 1776 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1777 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1778 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1779 | { |
| 1780 | int increment, value; |
| 1781 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1782 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1783 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1784 | |
| 1785 | /* There are two flavours of 'nice': one that returns the new |
| 1786 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1787 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1788 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1789 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1790 | If we are of the nice family that returns the new priority, we |
| 1791 | need to clear errno before the call, and check if errno is filled |
| 1792 | before calling posix_error() on a returnvalue of -1, because the |
| 1793 | -1 may be the actual new priority! */ |
| 1794 | |
| 1795 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1796 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1797 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1798 | if (value == 0) |
| 1799 | value = getpriority(PRIO_PROCESS, 0); |
| 1800 | #endif |
| 1801 | if (value == -1 && errno != 0) |
| 1802 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1803 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1804 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1805 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1806 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1807 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1808 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1809 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1810 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1811 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1812 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1813 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1814 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1815 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1816 | #ifdef MS_WINDOWS |
| 1817 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 1818 | #else |
| 1819 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 1820 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1823 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1824 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1825 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1826 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1827 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1828 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1829 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1830 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1831 | #ifdef MS_WINDOWS |
| 1832 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 1833 | #else |
| 1834 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 1835 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1839 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1840 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1841 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1842 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1843 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1844 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1845 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1846 | #ifdef MS_WINDOWS |
| 1847 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64); |
| 1848 | #else |
| 1849 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 1850 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1853 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1854 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1855 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1856 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1857 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1858 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1859 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1860 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1861 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1862 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1863 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1864 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1865 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1866 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1867 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1868 | Py_END_ALLOW_THREADS |
| 1869 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1870 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1871 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1872 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1874 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1875 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1876 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1877 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1878 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1879 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1880 | { |
| 1881 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1882 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1883 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1884 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1885 | if (i < 0) |
| 1886 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1887 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1891 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1892 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1893 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1894 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1895 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1896 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1897 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1898 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1899 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1900 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1901 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1902 | #ifdef MS_WINDOWS |
| 1903 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 1904 | #else |
| 1905 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 1906 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1909 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1910 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1911 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1912 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1913 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1914 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1915 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1916 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1917 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1918 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1919 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1920 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1921 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1922 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1923 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1924 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1925 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1926 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1927 | u.sysname, |
| 1928 | u.nodename, |
| 1929 | u.release, |
| 1930 | u.version, |
| 1931 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1932 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1933 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1934 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1935 | static int |
| 1936 | extract_time(PyObject *t, long* sec, long* usec) |
| 1937 | { |
| 1938 | long intval; |
| 1939 | if (PyFloat_Check(t)) { |
| 1940 | double tval = PyFloat_AsDouble(t); |
| 1941 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 1942 | if (!intobj) |
| 1943 | return -1; |
| 1944 | intval = PyInt_AsLong(intobj); |
| 1945 | Py_DECREF(intobj); |
| 1946 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 1947 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1948 | if (*usec < 0) |
| 1949 | /* If rounding gave us a negative number, |
| 1950 | truncate. */ |
| 1951 | *usec = 0; |
| 1952 | return 0; |
| 1953 | } |
| 1954 | intval = PyInt_AsLong(t); |
| 1955 | if (intval == -1 && PyErr_Occurred()) |
| 1956 | return -1; |
| 1957 | *sec = intval; |
| 1958 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 1959 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1960 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1961 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1962 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1963 | "utime(path, (atime, utime))\n\ |
| 1964 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1965 | 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] | 1966 | 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] | 1967 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1968 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1969 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1970 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1971 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1972 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1973 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1974 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1975 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1976 | #if defined(HAVE_UTIMES) |
| 1977 | struct timeval buf[2]; |
| 1978 | #define ATIME buf[0].tv_sec |
| 1979 | #define MTIME buf[1].tv_sec |
| 1980 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1981 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1982 | struct utimbuf buf; |
| 1983 | #define ATIME buf.actime |
| 1984 | #define MTIME buf.modtime |
| 1985 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1986 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1987 | time_t buf[2]; |
| 1988 | #define ATIME buf[0] |
| 1989 | #define MTIME buf[1] |
| 1990 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1991 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1992 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1993 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1994 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1995 | if (arg == Py_None) { |
| 1996 | /* optional time values not given */ |
| 1997 | Py_BEGIN_ALLOW_THREADS |
| 1998 | res = utime(path, NULL); |
| 1999 | Py_END_ALLOW_THREADS |
| 2000 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2001 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2002 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2003 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2004 | return NULL; |
| 2005 | } |
| 2006 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2007 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2008 | &atime, &ausec) == -1) |
| 2009 | return NULL; |
| 2010 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2011 | &mtime, &musec) == -1) |
| 2012 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2013 | ATIME = atime; |
| 2014 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2015 | #ifdef HAVE_UTIMES |
| 2016 | buf[0].tv_usec = ausec; |
| 2017 | buf[1].tv_usec = musec; |
| 2018 | Py_BEGIN_ALLOW_THREADS |
| 2019 | res = utimes(path, buf); |
| 2020 | Py_END_ALLOW_THREADS |
| 2021 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2022 | Py_BEGIN_ALLOW_THREADS |
| 2023 | res = utime(path, UTIME_ARG); |
| 2024 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2025 | #endif |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2026 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2027 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 2028 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2029 | Py_INCREF(Py_None); |
| 2030 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2031 | #undef UTIME_ARG |
| 2032 | #undef ATIME |
| 2033 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2036 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2037 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2038 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2039 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2040 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2041 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2042 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2043 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2044 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2045 | { |
| 2046 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2047 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2048 | return NULL; |
| 2049 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2050 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2053 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2054 | static void |
| 2055 | free_string_array(char **array, int count) |
| 2056 | { |
| 2057 | int i; |
| 2058 | for (i = 0; i < count; i++) |
| 2059 | PyMem_Free(array[i]); |
| 2060 | PyMem_DEL(array); |
| 2061 | } |
| 2062 | #endif |
| 2063 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2064 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2065 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2066 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2067 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2068 | Execute an executable path with arguments, replacing current process.\n\ |
| 2069 | \n\ |
| 2070 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2071 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2072 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2073 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2074 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2075 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2076 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2077 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2078 | char **argvlist; |
| 2079 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2080 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2081 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2082 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2083 | argv is a list or tuple of strings. */ |
| 2084 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2085 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2086 | Py_FileSystemDefaultEncoding, |
| 2087 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2088 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2089 | if (PyList_Check(argv)) { |
| 2090 | argc = PyList_Size(argv); |
| 2091 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2092 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2093 | else if (PyTuple_Check(argv)) { |
| 2094 | argc = PyTuple_Size(argv); |
| 2095 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2096 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2097 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2098 | 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] | 2099 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2100 | return NULL; |
| 2101 | } |
| 2102 | |
| 2103 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2104 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2105 | PyMem_Free(path); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2106 | return NULL; |
| 2107 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2109 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2110 | if (argvlist == NULL) { |
| 2111 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2112 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2113 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2114 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2115 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2116 | Py_FileSystemDefaultEncoding, |
| 2117 | &argvlist[i])) { |
| 2118 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2119 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2120 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2121 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2122 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2123 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2124 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2125 | } |
| 2126 | argvlist[argc] = NULL; |
| 2127 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2128 | #ifdef BAD_EXEC_PROTOTYPES |
| 2129 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2130 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2131 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2132 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2133 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2134 | /* If we get here it's definitely an error */ |
| 2135 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2136 | free_string_array(argvlist, argc); |
| 2137 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2138 | return posix_error(); |
| 2139 | } |
| 2140 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2141 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2142 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2143 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2144 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2145 | \n\ |
| 2146 | path: path of executable file\n\ |
| 2147 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2148 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2149 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2150 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2151 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2152 | { |
| 2153 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2154 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2155 | char **argvlist; |
| 2156 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2157 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2158 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2159 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2160 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2161 | |
| 2162 | /* execve has three arguments: (path, argv, env), where |
| 2163 | argv is a list or tuple of strings and env is a dictionary |
| 2164 | like posix.environ. */ |
| 2165 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2166 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2167 | Py_FileSystemDefaultEncoding, |
| 2168 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2169 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2170 | if (PyList_Check(argv)) { |
| 2171 | argc = PyList_Size(argv); |
| 2172 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2173 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2174 | else if (PyTuple_Check(argv)) { |
| 2175 | argc = PyTuple_Size(argv); |
| 2176 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2177 | } |
| 2178 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2179 | PyErr_SetString(PyExc_TypeError, |
| 2180 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2181 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2182 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2183 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2184 | PyErr_SetString(PyExc_TypeError, |
| 2185 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2186 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2189 | if (argc == 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2190 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2191 | "execve() arg 2 must not be empty"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2192 | goto fail_0; |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2195 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2196 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2197 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2198 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2199 | } |
| 2200 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2201 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2202 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2203 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2204 | &argvlist[i])) |
| 2205 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2206 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2207 | goto fail_1; |
| 2208 | } |
| 2209 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2210 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2211 | argvlist[argc] = NULL; |
| 2212 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2213 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2214 | if (i < 0) |
| 2215 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2216 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2217 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2218 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2219 | goto fail_1; |
| 2220 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2221 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2222 | keys = PyMapping_Keys(env); |
| 2223 | vals = PyMapping_Values(env); |
| 2224 | if (!keys || !vals) |
| 2225 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2226 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2227 | PyErr_SetString(PyExc_TypeError, |
| 2228 | "execve(): env.keys() or env.values() is not a list"); |
| 2229 | goto fail_2; |
| 2230 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2231 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2232 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2233 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2234 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2235 | |
| 2236 | key = PyList_GetItem(keys, pos); |
| 2237 | val = PyList_GetItem(vals, pos); |
| 2238 | if (!key || !val) |
| 2239 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2240 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2241 | if (!PyArg_Parse( |
| 2242 | key, |
| 2243 | "s;execve() arg 3 contains a non-string key", |
| 2244 | &k) || |
| 2245 | !PyArg_Parse( |
| 2246 | val, |
| 2247 | "s;execve() arg 3 contains a non-string value", |
| 2248 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2249 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2250 | goto fail_2; |
| 2251 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2252 | |
| 2253 | #if defined(PYOS_OS2) |
| 2254 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2255 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2256 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2257 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2258 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2259 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2260 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2261 | goto fail_2; |
| 2262 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2263 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2264 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2265 | #if defined(PYOS_OS2) |
| 2266 | } |
| 2267 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2268 | } |
| 2269 | envlist[envc] = 0; |
| 2270 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2271 | |
| 2272 | #ifdef BAD_EXEC_PROTOTYPES |
| 2273 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2274 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2275 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2276 | #endif /* BAD_EXEC_PROTOTYPES */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2277 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2278 | /* If we get here it's definitely an error */ |
| 2279 | |
| 2280 | (void) posix_error(); |
| 2281 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2282 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2283 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2284 | PyMem_DEL(envlist[envc]); |
| 2285 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2286 | fail_1: |
| 2287 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2288 | Py_XDECREF(vals); |
| 2289 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2290 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2291 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2292 | return NULL; |
| 2293 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2294 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2295 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2296 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2297 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2298 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2299 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2300 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2301 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2302 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2303 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2304 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2305 | |
| 2306 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2307 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2308 | { |
| 2309 | char *path; |
| 2310 | PyObject *argv; |
| 2311 | char **argvlist; |
| 2312 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2313 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2314 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2315 | |
| 2316 | /* spawnv has three arguments: (mode, path, argv), where |
| 2317 | argv is a list or tuple of strings. */ |
| 2318 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2319 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2320 | Py_FileSystemDefaultEncoding, |
| 2321 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2322 | return NULL; |
| 2323 | if (PyList_Check(argv)) { |
| 2324 | argc = PyList_Size(argv); |
| 2325 | getitem = PyList_GetItem; |
| 2326 | } |
| 2327 | else if (PyTuple_Check(argv)) { |
| 2328 | argc = PyTuple_Size(argv); |
| 2329 | getitem = PyTuple_GetItem; |
| 2330 | } |
| 2331 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2332 | PyErr_SetString(PyExc_TypeError, |
| 2333 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2334 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2335 | return NULL; |
| 2336 | } |
| 2337 | |
| 2338 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2339 | if (argvlist == NULL) { |
| 2340 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2341 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2342 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2343 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2344 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2345 | Py_FileSystemDefaultEncoding, |
| 2346 | &argvlist[i])) { |
| 2347 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2348 | PyErr_SetString( |
| 2349 | PyExc_TypeError, |
| 2350 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2351 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2352 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | argvlist[argc] = NULL; |
| 2356 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2357 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2358 | Py_BEGIN_ALLOW_THREADS |
| 2359 | spawnval = spawnv(mode, path, argvlist); |
| 2360 | Py_END_ALLOW_THREADS |
| 2361 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2362 | if (mode == _OLD_P_OVERLAY) |
| 2363 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2364 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2365 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2366 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2367 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2368 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2369 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2370 | free_string_array(argvlist, argc); |
| 2371 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2372 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2373 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2374 | return posix_error(); |
| 2375 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2376 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2377 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2378 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2379 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2380 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2384 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2385 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2386 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2387 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2388 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2389 | path: path of executable file\n\ |
| 2390 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2391 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2392 | |
| 2393 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2394 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2395 | { |
| 2396 | char *path; |
| 2397 | PyObject *argv, *env; |
| 2398 | char **argvlist; |
| 2399 | char **envlist; |
| 2400 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2401 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2402 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2403 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2404 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2405 | |
| 2406 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2407 | argv is a list or tuple of strings and env is a dictionary |
| 2408 | like posix.environ. */ |
| 2409 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2410 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2411 | Py_FileSystemDefaultEncoding, |
| 2412 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2413 | return NULL; |
| 2414 | if (PyList_Check(argv)) { |
| 2415 | argc = PyList_Size(argv); |
| 2416 | getitem = PyList_GetItem; |
| 2417 | } |
| 2418 | else if (PyTuple_Check(argv)) { |
| 2419 | argc = PyTuple_Size(argv); |
| 2420 | getitem = PyTuple_GetItem; |
| 2421 | } |
| 2422 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2423 | PyErr_SetString(PyExc_TypeError, |
| 2424 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2425 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2426 | } |
| 2427 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2428 | PyErr_SetString(PyExc_TypeError, |
| 2429 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2430 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | argvlist = PyMem_NEW(char *, argc+1); |
| 2434 | if (argvlist == NULL) { |
| 2435 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2436 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2437 | } |
| 2438 | for (i = 0; i < argc; i++) { |
| 2439 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2440 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2441 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2442 | &argvlist[i])) |
| 2443 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2444 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2445 | goto fail_1; |
| 2446 | } |
| 2447 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2448 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2449 | argvlist[argc] = NULL; |
| 2450 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2451 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2452 | if (i < 0) |
| 2453 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2454 | envlist = PyMem_NEW(char *, i + 1); |
| 2455 | if (envlist == NULL) { |
| 2456 | PyErr_NoMemory(); |
| 2457 | goto fail_1; |
| 2458 | } |
| 2459 | envc = 0; |
| 2460 | keys = PyMapping_Keys(env); |
| 2461 | vals = PyMapping_Values(env); |
| 2462 | if (!keys || !vals) |
| 2463 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2464 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2465 | PyErr_SetString(PyExc_TypeError, |
| 2466 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2467 | goto fail_2; |
| 2468 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2469 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2470 | for (pos = 0; pos < i; pos++) { |
| 2471 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2472 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2473 | |
| 2474 | key = PyList_GetItem(keys, pos); |
| 2475 | val = PyList_GetItem(vals, pos); |
| 2476 | if (!key || !val) |
| 2477 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2478 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2479 | if (!PyArg_Parse( |
| 2480 | key, |
| 2481 | "s;spawnve() arg 3 contains a non-string key", |
| 2482 | &k) || |
| 2483 | !PyArg_Parse( |
| 2484 | val, |
| 2485 | "s;spawnve() arg 3 contains a non-string value", |
| 2486 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2487 | { |
| 2488 | goto fail_2; |
| 2489 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2490 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2491 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2492 | if (p == NULL) { |
| 2493 | PyErr_NoMemory(); |
| 2494 | goto fail_2; |
| 2495 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2496 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2497 | envlist[envc++] = p; |
| 2498 | } |
| 2499 | envlist[envc] = 0; |
| 2500 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2501 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2502 | Py_BEGIN_ALLOW_THREADS |
| 2503 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2504 | Py_END_ALLOW_THREADS |
| 2505 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2506 | if (mode == _OLD_P_OVERLAY) |
| 2507 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2508 | |
| 2509 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2510 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2511 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2512 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2513 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2514 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2515 | (void) posix_error(); |
| 2516 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2517 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2518 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2519 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2520 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2521 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2522 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2523 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2524 | while (--envc >= 0) |
| 2525 | PyMem_DEL(envlist[envc]); |
| 2526 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2527 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2528 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2529 | Py_XDECREF(vals); |
| 2530 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2531 | fail_0: |
| 2532 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2533 | return res; |
| 2534 | } |
| 2535 | #endif /* HAVE_SPAWNV */ |
| 2536 | |
| 2537 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2538 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2539 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2540 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2541 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 2542 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2543 | 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] | 2544 | |
| 2545 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2546 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2547 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2548 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2549 | if (pid == -1) |
| 2550 | return posix_error(); |
| 2551 | PyOS_AfterFork(); |
| 2552 | return PyInt_FromLong((long)pid); |
| 2553 | } |
| 2554 | #endif |
| 2555 | |
| 2556 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2557 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2558 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2559 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2560 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2561 | 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] | 2562 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2563 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2564 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2565 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2566 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2567 | if (pid == -1) |
| 2568 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2569 | if (pid == 0) |
| 2570 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2571 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2572 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2573 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2574 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2575 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 2576 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 2577 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2578 | #define DEV_PTY_FILE "/dev/ptc" |
| 2579 | #define HAVE_DEV_PTMX |
| 2580 | #else |
| 2581 | #define DEV_PTY_FILE "/dev/ptmx" |
| 2582 | #endif |
| 2583 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2584 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2585 | #ifdef HAVE_PTY_H |
| 2586 | #include <pty.h> |
| 2587 | #else |
| 2588 | #ifdef HAVE_LIBUTIL_H |
| 2589 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2590 | #endif /* HAVE_LIBUTIL_H */ |
| 2591 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 2592 | #ifdef HAVE_STROPTS_H |
| 2593 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2594 | #endif |
| 2595 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2596 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2597 | #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] | 2598 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2599 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2600 | 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] | 2601 | |
| 2602 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2603 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2604 | { |
| 2605 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2606 | #ifndef HAVE_OPENPTY |
| 2607 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2608 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2609 | #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] | 2610 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2611 | #ifdef sun |
| 2612 | extern char *ptsname(); |
| 2613 | #endif |
| 2614 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2615 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2616 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2617 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 2618 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2619 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2620 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 2621 | if (slave_name == NULL) |
| 2622 | return posix_error(); |
| 2623 | |
| 2624 | slave_fd = open(slave_name, O_RDWR); |
| 2625 | if (slave_fd < 0) |
| 2626 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2627 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2628 | 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] | 2629 | if (master_fd < 0) |
| 2630 | return posix_error(); |
| 2631 | sig_saved = signal(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2632 | /* change permission of slave */ |
| 2633 | if (grantpt(master_fd) < 0) { |
| 2634 | signal(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2635 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2636 | } |
| 2637 | /* unlock slave */ |
| 2638 | if (unlockpt(master_fd) < 0) { |
| 2639 | signal(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2640 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2641 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2642 | signal(SIGCHLD, sig_saved); |
| 2643 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 2644 | if (slave_name == NULL) |
| 2645 | return posix_error(); |
| 2646 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 2647 | if (slave_fd < 0) |
| 2648 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2649 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2650 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 2651 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2652 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2653 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2654 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2655 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 2656 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2657 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2658 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2659 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2660 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2661 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2662 | |
| 2663 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2664 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2665 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2666 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 2667 | 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] | 2668 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2669 | |
| 2670 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2671 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2672 | { |
| 2673 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2674 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2675 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 2676 | if (pid == -1) |
| 2677 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2678 | if (pid == 0) |
| 2679 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2680 | return Py_BuildValue("(ii)", pid, master_fd); |
| 2681 | } |
| 2682 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2683 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2684 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2685 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2686 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2687 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2688 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2689 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2690 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2691 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2692 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2693 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2694 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2695 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2696 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2697 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2698 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2699 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2700 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2701 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2702 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2703 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2704 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2705 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2706 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2707 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2708 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2709 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2710 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2711 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2712 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2713 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2714 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2715 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2716 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2717 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2718 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2719 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2720 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2721 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2723 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2724 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2725 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2726 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2727 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2728 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2729 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2730 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2733 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2734 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2735 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2736 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2737 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2738 | |
| 2739 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2740 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2741 | { |
| 2742 | PyObject *result = NULL; |
| 2743 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2744 | #ifdef NGROUPS_MAX |
| 2745 | #define MAX_GROUPS NGROUPS_MAX |
| 2746 | #else |
| 2747 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2748 | #define MAX_GROUPS 64 |
| 2749 | #endif |
| 2750 | gid_t grouplist[MAX_GROUPS]; |
| 2751 | int n; |
| 2752 | |
| 2753 | n = getgroups(MAX_GROUPS, grouplist); |
| 2754 | if (n < 0) |
| 2755 | posix_error(); |
| 2756 | else { |
| 2757 | result = PyList_New(n); |
| 2758 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2759 | int i; |
| 2760 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2761 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2762 | if (o == NULL) { |
| 2763 | Py_DECREF(result); |
| 2764 | result = NULL; |
| 2765 | break; |
| 2766 | } |
| 2767 | PyList_SET_ITEM(result, i, o); |
| 2768 | } |
| 2769 | } |
| 2770 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2771 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2772 | return result; |
| 2773 | } |
| 2774 | #endif |
| 2775 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2776 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2777 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2778 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2779 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2780 | |
| 2781 | static PyObject * |
| 2782 | posix_getpgid(PyObject *self, PyObject *args) |
| 2783 | { |
| 2784 | int pid, pgid; |
| 2785 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 2786 | return NULL; |
| 2787 | pgid = getpgid(pid); |
| 2788 | if (pgid < 0) |
| 2789 | return posix_error(); |
| 2790 | return PyInt_FromLong((long)pgid); |
| 2791 | } |
| 2792 | #endif /* HAVE_GETPGID */ |
| 2793 | |
| 2794 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2795 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2796 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2797 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2798 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2799 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2800 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2801 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2802 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2803 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2804 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2805 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2806 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2807 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2808 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2809 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2810 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2811 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2812 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2813 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2814 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2815 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2816 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2817 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2818 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2819 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2820 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2821 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2822 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2823 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2824 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2825 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2826 | Py_INCREF(Py_None); |
| 2827 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2830 | #endif /* HAVE_SETPGRP */ |
| 2831 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2832 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2833 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2834 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2835 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2836 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2837 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2838 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2839 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2840 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2841 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2842 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2843 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2844 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2845 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2846 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2847 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2848 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2849 | |
| 2850 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2851 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2852 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2853 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2854 | char *name; |
| 2855 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2856 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2857 | errno = 0; |
| 2858 | name = getlogin(); |
| 2859 | if (name == NULL) { |
| 2860 | if (errno) |
| 2861 | posix_error(); |
| 2862 | else |
| 2863 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2864 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2865 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2866 | else |
| 2867 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2868 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2869 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2870 | return result; |
| 2871 | } |
| 2872 | #endif |
| 2873 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2874 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2875 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2876 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2877 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2878 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2879 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2880 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2881 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2882 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2883 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2884 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2885 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2886 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2887 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2888 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2889 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2890 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2891 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2892 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2893 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2894 | { |
| 2895 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2896 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2897 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2898 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2899 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2900 | APIRET rc; |
| 2901 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2902 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2903 | |
| 2904 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2905 | APIRET rc; |
| 2906 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2907 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2908 | |
| 2909 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2910 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2911 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2912 | if (kill(pid, sig) == -1) |
| 2913 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2914 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2915 | Py_INCREF(Py_None); |
| 2916 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2917 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2918 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2919 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2920 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2921 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2922 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2923 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2924 | |
| 2925 | static PyObject * |
| 2926 | posix_killpg(PyObject *self, PyObject *args) |
| 2927 | { |
| 2928 | int pgid, sig; |
| 2929 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 2930 | return NULL; |
| 2931 | if (killpg(pgid, sig) == -1) |
| 2932 | return posix_error(); |
| 2933 | Py_INCREF(Py_None); |
| 2934 | return Py_None; |
| 2935 | } |
| 2936 | #endif |
| 2937 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2938 | #ifdef HAVE_PLOCK |
| 2939 | |
| 2940 | #ifdef HAVE_SYS_LOCK_H |
| 2941 | #include <sys/lock.h> |
| 2942 | #endif |
| 2943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2944 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2945 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2946 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2947 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2948 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2949 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2950 | { |
| 2951 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2952 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2953 | return NULL; |
| 2954 | if (plock(op) == -1) |
| 2955 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2956 | Py_INCREF(Py_None); |
| 2957 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2958 | } |
| 2959 | #endif |
| 2960 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2961 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2962 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2963 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2964 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2965 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2966 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2967 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2968 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2969 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2970 | async_system(const char *command) |
| 2971 | { |
| 2972 | char *p, errormsg[256], args[1024]; |
| 2973 | RESULTCODES rcodes; |
| 2974 | APIRET rc; |
| 2975 | char *shell = getenv("COMSPEC"); |
| 2976 | if (!shell) |
| 2977 | shell = "cmd"; |
| 2978 | |
| 2979 | strcpy(args, shell); |
| 2980 | p = &args[ strlen(args)+1 ]; |
| 2981 | strcpy(p, "/c "); |
| 2982 | strcat(p, command); |
| 2983 | p += strlen(p) + 1; |
| 2984 | *p = '\0'; |
| 2985 | |
| 2986 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2987 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2988 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2989 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2990 | &rcodes, shell); |
| 2991 | return rc; |
| 2992 | } |
| 2993 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2994 | static FILE * |
| 2995 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2996 | { |
| 2997 | HFILE rhan, whan; |
| 2998 | FILE *retfd = NULL; |
| 2999 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 3000 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3001 | if (rc != NO_ERROR) { |
| 3002 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3003 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3004 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3005 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3006 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 3007 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3008 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3009 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 3010 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3011 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3012 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 3013 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3014 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3015 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3018 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 3019 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3020 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3021 | if (rc == NO_ERROR) |
| 3022 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 3023 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3024 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 3025 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3026 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3027 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 3028 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3029 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3030 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 3031 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3032 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3033 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 3034 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3035 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3036 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3039 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 3040 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3041 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3042 | if (rc == NO_ERROR) |
| 3043 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 3044 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3045 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 3046 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3047 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3048 | } else { |
| 3049 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3050 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3051 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3052 | } |
| 3053 | |
| 3054 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3055 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3056 | { |
| 3057 | char *name; |
| 3058 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3059 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3060 | FILE *fp; |
| 3061 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3062 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3063 | return NULL; |
| 3064 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3065 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3066 | Py_END_ALLOW_THREADS |
| 3067 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3068 | return os2_error(err); |
| 3069 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3070 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3071 | if (f != NULL) |
| 3072 | PyFile_SetBufSize(f, bufsize); |
| 3073 | return f; |
| 3074 | } |
| 3075 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3076 | #elif defined(PYCC_GCC) |
| 3077 | |
| 3078 | /* standard posix version of popen() support */ |
| 3079 | static PyObject * |
| 3080 | posix_popen(PyObject *self, PyObject *args) |
| 3081 | { |
| 3082 | char *name; |
| 3083 | char *mode = "r"; |
| 3084 | int bufsize = -1; |
| 3085 | FILE *fp; |
| 3086 | PyObject *f; |
| 3087 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3088 | return NULL; |
| 3089 | Py_BEGIN_ALLOW_THREADS |
| 3090 | fp = popen(name, mode); |
| 3091 | Py_END_ALLOW_THREADS |
| 3092 | if (fp == NULL) |
| 3093 | return posix_error(); |
| 3094 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3095 | if (f != NULL) |
| 3096 | PyFile_SetBufSize(f, bufsize); |
| 3097 | return f; |
| 3098 | } |
| 3099 | |
| 3100 | /* fork() under OS/2 has lots'o'warts |
| 3101 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3102 | * most of this code is a ripoff of the win32 code, but using the |
| 3103 | * capabilities of EMX's C library routines |
| 3104 | */ |
| 3105 | |
| 3106 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3107 | #define POPEN_1 1 |
| 3108 | #define POPEN_2 2 |
| 3109 | #define POPEN_3 3 |
| 3110 | #define POPEN_4 4 |
| 3111 | |
| 3112 | static PyObject *_PyPopen(char *, int, int, int); |
| 3113 | static int _PyPclose(FILE *file); |
| 3114 | |
| 3115 | /* |
| 3116 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3117 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3118 | * for more information on this dictionary's use. |
| 3119 | */ |
| 3120 | static PyObject *_PyPopenProcs = NULL; |
| 3121 | |
| 3122 | /* os2emx version of popen2() |
| 3123 | * |
| 3124 | * The result of this function is a pipe (file) connected to the |
| 3125 | * process's stdin, and a pipe connected to the process's stdout. |
| 3126 | */ |
| 3127 | |
| 3128 | static PyObject * |
| 3129 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3130 | { |
| 3131 | PyObject *f; |
| 3132 | int tm=0; |
| 3133 | |
| 3134 | char *cmdstring; |
| 3135 | char *mode = "t"; |
| 3136 | int bufsize = -1; |
| 3137 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3138 | return NULL; |
| 3139 | |
| 3140 | if (*mode == 't') |
| 3141 | tm = O_TEXT; |
| 3142 | else if (*mode != 'b') { |
| 3143 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3144 | return NULL; |
| 3145 | } else |
| 3146 | tm = O_BINARY; |
| 3147 | |
| 3148 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3149 | |
| 3150 | return f; |
| 3151 | } |
| 3152 | |
| 3153 | /* |
| 3154 | * Variation on os2emx.popen2 |
| 3155 | * |
| 3156 | * The result of this function is 3 pipes - the process's stdin, |
| 3157 | * stdout and stderr |
| 3158 | */ |
| 3159 | |
| 3160 | static PyObject * |
| 3161 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3162 | { |
| 3163 | PyObject *f; |
| 3164 | int tm = 0; |
| 3165 | |
| 3166 | char *cmdstring; |
| 3167 | char *mode = "t"; |
| 3168 | int bufsize = -1; |
| 3169 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3170 | return NULL; |
| 3171 | |
| 3172 | if (*mode == 't') |
| 3173 | tm = O_TEXT; |
| 3174 | else if (*mode != 'b') { |
| 3175 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3176 | return NULL; |
| 3177 | } else |
| 3178 | tm = O_BINARY; |
| 3179 | |
| 3180 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3181 | |
| 3182 | return f; |
| 3183 | } |
| 3184 | |
| 3185 | /* |
| 3186 | * Variation on os2emx.popen2 |
| 3187 | * |
| 3188 | * The result of this function is 2 pipes - the processes stdin, |
| 3189 | * and stdout+stderr combined as a single pipe. |
| 3190 | */ |
| 3191 | |
| 3192 | static PyObject * |
| 3193 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3194 | { |
| 3195 | PyObject *f; |
| 3196 | int tm = 0; |
| 3197 | |
| 3198 | char *cmdstring; |
| 3199 | char *mode = "t"; |
| 3200 | int bufsize = -1; |
| 3201 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3202 | return NULL; |
| 3203 | |
| 3204 | if (*mode == 't') |
| 3205 | tm = O_TEXT; |
| 3206 | else if (*mode != 'b') { |
| 3207 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3208 | return NULL; |
| 3209 | } else |
| 3210 | tm = O_BINARY; |
| 3211 | |
| 3212 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3213 | |
| 3214 | return f; |
| 3215 | } |
| 3216 | |
| 3217 | /* a couple of structures for convenient handling of multiple |
| 3218 | * file handles and pipes |
| 3219 | */ |
| 3220 | struct file_ref |
| 3221 | { |
| 3222 | int handle; |
| 3223 | int flags; |
| 3224 | }; |
| 3225 | |
| 3226 | struct pipe_ref |
| 3227 | { |
| 3228 | int rd; |
| 3229 | int wr; |
| 3230 | }; |
| 3231 | |
| 3232 | /* The following code is derived from the win32 code */ |
| 3233 | |
| 3234 | static PyObject * |
| 3235 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3236 | { |
| 3237 | struct file_ref stdio[3]; |
| 3238 | struct pipe_ref p_fd[3]; |
| 3239 | FILE *p_s[3]; |
| 3240 | int file_count, i, pipe_err, pipe_pid; |
| 3241 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3242 | PyObject *f, *p_f[3]; |
| 3243 | |
| 3244 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3245 | if (mode == O_TEXT) |
| 3246 | { |
| 3247 | rd_mode = "rt"; |
| 3248 | wr_mode = "wt"; |
| 3249 | } |
| 3250 | else |
| 3251 | { |
| 3252 | rd_mode = "rb"; |
| 3253 | wr_mode = "wb"; |
| 3254 | } |
| 3255 | |
| 3256 | /* prepare shell references */ |
| 3257 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3258 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3259 | { |
| 3260 | errno = ENOENT; |
| 3261 | return posix_error(); |
| 3262 | } |
| 3263 | |
| 3264 | sh_name = _getname(shell); |
| 3265 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3266 | opt = "/c"; |
| 3267 | else |
| 3268 | opt = "-c"; |
| 3269 | |
| 3270 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3271 | i = pipe_err = 0; |
| 3272 | while (pipe_err >= 0 && i < 3) |
| 3273 | { |
| 3274 | pipe_err = stdio[i].handle = dup(i); |
| 3275 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3276 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3277 | i++; |
| 3278 | } |
| 3279 | if (pipe_err < 0) |
| 3280 | { |
| 3281 | /* didn't get them all saved - clean up and bail out */ |
| 3282 | int saved_err = errno; |
| 3283 | while (i-- > 0) |
| 3284 | { |
| 3285 | close(stdio[i].handle); |
| 3286 | } |
| 3287 | errno = saved_err; |
| 3288 | return posix_error(); |
| 3289 | } |
| 3290 | |
| 3291 | /* create pipe ends */ |
| 3292 | file_count = 2; |
| 3293 | if (n == POPEN_3) |
| 3294 | file_count = 3; |
| 3295 | i = pipe_err = 0; |
| 3296 | while ((pipe_err == 0) && (i < file_count)) |
| 3297 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3298 | if (pipe_err < 0) |
| 3299 | { |
| 3300 | /* didn't get them all made - clean up and bail out */ |
| 3301 | while (i-- > 0) |
| 3302 | { |
| 3303 | close(p_fd[i].wr); |
| 3304 | close(p_fd[i].rd); |
| 3305 | } |
| 3306 | errno = EPIPE; |
| 3307 | return posix_error(); |
| 3308 | } |
| 3309 | |
| 3310 | /* change the actual standard IO streams over temporarily, |
| 3311 | * making the retained pipe ends non-inheritable |
| 3312 | */ |
| 3313 | pipe_err = 0; |
| 3314 | |
| 3315 | /* - stdin */ |
| 3316 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3317 | { |
| 3318 | close(p_fd[0].rd); |
| 3319 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3320 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3321 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3322 | { |
| 3323 | close(p_fd[0].wr); |
| 3324 | pipe_err = -1; |
| 3325 | } |
| 3326 | } |
| 3327 | else |
| 3328 | { |
| 3329 | pipe_err = -1; |
| 3330 | } |
| 3331 | |
| 3332 | /* - stdout */ |
| 3333 | if (pipe_err == 0) |
| 3334 | { |
| 3335 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3336 | { |
| 3337 | close(p_fd[1].wr); |
| 3338 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3339 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3340 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3341 | { |
| 3342 | close(p_fd[1].rd); |
| 3343 | pipe_err = -1; |
| 3344 | } |
| 3345 | } |
| 3346 | else |
| 3347 | { |
| 3348 | pipe_err = -1; |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | /* - stderr, as required */ |
| 3353 | if (pipe_err == 0) |
| 3354 | switch (n) |
| 3355 | { |
| 3356 | case POPEN_3: |
| 3357 | { |
| 3358 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3359 | { |
| 3360 | close(p_fd[2].wr); |
| 3361 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3362 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3363 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3364 | { |
| 3365 | close(p_fd[2].rd); |
| 3366 | pipe_err = -1; |
| 3367 | } |
| 3368 | } |
| 3369 | else |
| 3370 | { |
| 3371 | pipe_err = -1; |
| 3372 | } |
| 3373 | break; |
| 3374 | } |
| 3375 | |
| 3376 | case POPEN_4: |
| 3377 | { |
| 3378 | if (dup2(1, 2) != 2) |
| 3379 | { |
| 3380 | pipe_err = -1; |
| 3381 | } |
| 3382 | break; |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | /* spawn the child process */ |
| 3387 | if (pipe_err == 0) |
| 3388 | { |
| 3389 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3390 | if (pipe_pid == -1) |
| 3391 | { |
| 3392 | pipe_err = -1; |
| 3393 | } |
| 3394 | else |
| 3395 | { |
| 3396 | /* save the PID into the FILE structure |
| 3397 | * NOTE: this implementation doesn't actually |
| 3398 | * take advantage of this, but do it for |
| 3399 | * completeness - AIM Apr01 |
| 3400 | */ |
| 3401 | for (i = 0; i < file_count; i++) |
| 3402 | p_s[i]->_pid = pipe_pid; |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | /* reset standard IO to normal */ |
| 3407 | for (i = 0; i < 3; i++) |
| 3408 | { |
| 3409 | dup2(stdio[i].handle, i); |
| 3410 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3411 | close(stdio[i].handle); |
| 3412 | } |
| 3413 | |
| 3414 | /* if any remnant problems, clean up and bail out */ |
| 3415 | if (pipe_err < 0) |
| 3416 | { |
| 3417 | for (i = 0; i < 3; i++) |
| 3418 | { |
| 3419 | close(p_fd[i].rd); |
| 3420 | close(p_fd[i].wr); |
| 3421 | } |
| 3422 | errno = EPIPE; |
| 3423 | return posix_error_with_filename(cmdstring); |
| 3424 | } |
| 3425 | |
| 3426 | /* build tuple of file objects to return */ |
| 3427 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3428 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3429 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3430 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3431 | if (n == POPEN_3) |
| 3432 | { |
| 3433 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3434 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3435 | f = Py_BuildValue("OOO", p_f[0], p_f[1], p_f[2]); |
| 3436 | } |
| 3437 | else |
| 3438 | f = Py_BuildValue("OO", p_f[0], p_f[1]); |
| 3439 | |
| 3440 | /* |
| 3441 | * Insert the files we've created into the process dictionary |
| 3442 | * all referencing the list with the process handle and the |
| 3443 | * initial number of files (see description below in _PyPclose). |
| 3444 | * Since if _PyPclose later tried to wait on a process when all |
| 3445 | * handles weren't closed, it could create a deadlock with the |
| 3446 | * child, we spend some energy here to try to ensure that we |
| 3447 | * either insert all file handles into the dictionary or none |
| 3448 | * at all. It's a little clumsy with the various popen modes |
| 3449 | * and variable number of files involved. |
| 3450 | */ |
| 3451 | if (!_PyPopenProcs) |
| 3452 | { |
| 3453 | _PyPopenProcs = PyDict_New(); |
| 3454 | } |
| 3455 | |
| 3456 | if (_PyPopenProcs) |
| 3457 | { |
| 3458 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3459 | int ins_rc[3]; |
| 3460 | |
| 3461 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3462 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3463 | |
| 3464 | procObj = PyList_New(2); |
| 3465 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3466 | intObj = PyInt_FromLong((long) file_count); |
| 3467 | |
| 3468 | if (procObj && pidObj && intObj) |
| 3469 | { |
| 3470 | PyList_SetItem(procObj, 0, pidObj); |
| 3471 | PyList_SetItem(procObj, 1, intObj); |
| 3472 | |
| 3473 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3474 | if (fileObj[0]) |
| 3475 | { |
| 3476 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3477 | fileObj[0], |
| 3478 | procObj); |
| 3479 | } |
| 3480 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3481 | if (fileObj[1]) |
| 3482 | { |
| 3483 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3484 | fileObj[1], |
| 3485 | procObj); |
| 3486 | } |
| 3487 | if (file_count >= 3) |
| 3488 | { |
| 3489 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3490 | if (fileObj[2]) |
| 3491 | { |
| 3492 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3493 | fileObj[2], |
| 3494 | procObj); |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3499 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3500 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3501 | { |
| 3502 | /* Something failed - remove any dictionary |
| 3503 | * entries that did make it. |
| 3504 | */ |
| 3505 | if (!ins_rc[0] && fileObj[0]) |
| 3506 | { |
| 3507 | PyDict_DelItem(_PyPopenProcs, |
| 3508 | fileObj[0]); |
| 3509 | } |
| 3510 | if (!ins_rc[1] && fileObj[1]) |
| 3511 | { |
| 3512 | PyDict_DelItem(_PyPopenProcs, |
| 3513 | fileObj[1]); |
| 3514 | } |
| 3515 | if (!ins_rc[2] && fileObj[2]) |
| 3516 | { |
| 3517 | PyDict_DelItem(_PyPopenProcs, |
| 3518 | fileObj[2]); |
| 3519 | } |
| 3520 | } |
| 3521 | } |
| 3522 | |
| 3523 | /* |
| 3524 | * Clean up our localized references for the dictionary keys |
| 3525 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3526 | * that got placed in the dictionary. |
| 3527 | */ |
| 3528 | Py_XDECREF(procObj); |
| 3529 | Py_XDECREF(fileObj[0]); |
| 3530 | Py_XDECREF(fileObj[1]); |
| 3531 | Py_XDECREF(fileObj[2]); |
| 3532 | } |
| 3533 | |
| 3534 | /* Child is launched. */ |
| 3535 | return f; |
| 3536 | } |
| 3537 | |
| 3538 | /* |
| 3539 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3540 | * exit code for the child process and return as a result of the close. |
| 3541 | * |
| 3542 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3543 | * input file pointer to information about the process that was |
| 3544 | * originally created by the popen* call that created the file pointer. |
| 3545 | * The dictionary uses the file pointer as a key (with one entry |
| 3546 | * inserted for each file returned by the original popen* call) and a |
| 3547 | * single list object as the value for all files from a single call. |
| 3548 | * The list object contains the Win32 process handle at [0], and a file |
| 3549 | * count at [1], which is initialized to the total number of file |
| 3550 | * handles using that list. |
| 3551 | * |
| 3552 | * This function closes whichever handle it is passed, and decrements |
| 3553 | * the file count in the dictionary for the process handle pointed to |
| 3554 | * by this file. On the last close (when the file count reaches zero), |
| 3555 | * this function will wait for the child process and then return its |
| 3556 | * exit code as the result of the close() operation. This permits the |
| 3557 | * files to be closed in any order - it is always the close() of the |
| 3558 | * final handle that will return the exit code. |
| 3559 | */ |
| 3560 | |
| 3561 | /* RED_FLAG 31-Aug-2000 Tim |
| 3562 | * This is always called (today!) between a pair of |
| 3563 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 3564 | * macros. So the thread running this has no valid thread state, as |
| 3565 | * far as Python is concerned. However, this calls some Python API |
| 3566 | * functions that cannot be called safely without a valid thread |
| 3567 | * state, in particular PyDict_GetItem. |
| 3568 | * As a temporary hack (although it may last for years ...), we |
| 3569 | * *rely* on not having a valid thread state in this function, in |
| 3570 | * order to create our own "from scratch". |
| 3571 | * This will deadlock if _PyPclose is ever called by a thread |
| 3572 | * holding the global lock. |
| 3573 | * (The OS/2 EMX thread support appears to cover the case where the |
| 3574 | * lock is already held - AIM Apr01) |
| 3575 | */ |
| 3576 | |
| 3577 | static int _PyPclose(FILE *file) |
| 3578 | { |
| 3579 | int result; |
| 3580 | int exit_code; |
| 3581 | int pipe_pid; |
| 3582 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 3583 | int file_count; |
| 3584 | #ifdef WITH_THREAD |
| 3585 | PyInterpreterState* pInterpreterState; |
| 3586 | PyThreadState* pThreadState; |
| 3587 | #endif |
| 3588 | |
| 3589 | /* Close the file handle first, to ensure it can't block the |
| 3590 | * child from exiting if it's the last handle. |
| 3591 | */ |
| 3592 | result = fclose(file); |
| 3593 | |
| 3594 | #ifdef WITH_THREAD |
| 3595 | /* Bootstrap a valid thread state into existence. */ |
| 3596 | pInterpreterState = PyInterpreterState_New(); |
| 3597 | if (!pInterpreterState) { |
| 3598 | /* Well, we're hosed now! We don't have a thread |
| 3599 | * state, so can't call a nice error routine, or raise |
| 3600 | * an exception. Just die. |
| 3601 | */ |
| 3602 | Py_FatalError("unable to allocate interpreter state " |
| 3603 | "when closing popen object."); |
| 3604 | return -1; /* unreachable */ |
| 3605 | } |
| 3606 | pThreadState = PyThreadState_New(pInterpreterState); |
| 3607 | if (!pThreadState) { |
| 3608 | Py_FatalError("unable to allocate thread state " |
| 3609 | "when closing popen object."); |
| 3610 | return -1; /* unreachable */ |
| 3611 | } |
| 3612 | /* Grab the global lock. Note that this will deadlock if the |
| 3613 | * current thread already has the lock! (see RED_FLAG comments |
| 3614 | * before this function) |
| 3615 | */ |
| 3616 | PyEval_RestoreThread(pThreadState); |
| 3617 | #endif |
| 3618 | |
| 3619 | if (_PyPopenProcs) |
| 3620 | { |
| 3621 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3622 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3623 | fileObj)) != NULL && |
| 3624 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 3625 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 3626 | { |
| 3627 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 3628 | file_count = (int) PyInt_AsLong(intObj); |
| 3629 | |
| 3630 | if (file_count > 1) |
| 3631 | { |
| 3632 | /* Still other files referencing process */ |
| 3633 | file_count--; |
| 3634 | PyList_SetItem(procObj,1, |
| 3635 | PyInt_FromLong((long) file_count)); |
| 3636 | } |
| 3637 | else |
| 3638 | { |
| 3639 | /* Last file for this process */ |
| 3640 | if (result != EOF && |
| 3641 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 3642 | { |
| 3643 | /* extract exit status */ |
| 3644 | if (WIFEXITED(exit_code)) |
| 3645 | { |
| 3646 | result = WEXITSTATUS(exit_code); |
| 3647 | } |
| 3648 | else |
| 3649 | { |
| 3650 | errno = EPIPE; |
| 3651 | result = -1; |
| 3652 | } |
| 3653 | } |
| 3654 | else |
| 3655 | { |
| 3656 | /* Indicate failure - this will cause the file object |
| 3657 | * to raise an I/O error and translate the last |
| 3658 | * error code from errno. We do have a problem with |
| 3659 | * last errors that overlap the normal errno table, |
| 3660 | * but that's a consistent problem with the file object. |
| 3661 | */ |
| 3662 | result = -1; |
| 3663 | } |
| 3664 | } |
| 3665 | |
| 3666 | /* Remove this file pointer from dictionary */ |
| 3667 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3668 | |
| 3669 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 3670 | { |
| 3671 | Py_DECREF(_PyPopenProcs); |
| 3672 | _PyPopenProcs = NULL; |
| 3673 | } |
| 3674 | |
| 3675 | } /* if object retrieval ok */ |
| 3676 | |
| 3677 | Py_XDECREF(fileObj); |
| 3678 | } /* if _PyPopenProcs */ |
| 3679 | |
| 3680 | #ifdef WITH_THREAD |
| 3681 | /* Tear down the thread & interpreter states. |
| 3682 | * Note that interpreter state clear & delete functions automatically |
| 3683 | * call the thread clear & delete functions, and indeed insist on |
| 3684 | * doing that themselves. The lock must be held during the clear, but |
| 3685 | * need not be held during the delete. |
| 3686 | */ |
| 3687 | PyInterpreterState_Clear(pInterpreterState); |
| 3688 | PyEval_ReleaseThread(pThreadState); |
| 3689 | PyInterpreterState_Delete(pInterpreterState); |
| 3690 | #endif |
| 3691 | |
| 3692 | return result; |
| 3693 | } |
| 3694 | |
| 3695 | #endif /* PYCC_??? */ |
| 3696 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3697 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3698 | |
| 3699 | /* |
| 3700 | * Portable 'popen' replacement for Win32. |
| 3701 | * |
| 3702 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3703 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3704 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3705 | */ |
| 3706 | |
| 3707 | #include <malloc.h> |
| 3708 | #include <io.h> |
| 3709 | #include <fcntl.h> |
| 3710 | |
| 3711 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 3712 | #define POPEN_1 1 |
| 3713 | #define POPEN_2 2 |
| 3714 | #define POPEN_3 3 |
| 3715 | #define POPEN_4 4 |
| 3716 | |
| 3717 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3718 | static int _PyPclose(FILE *file); |
| 3719 | |
| 3720 | /* |
| 3721 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3722 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3723 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3724 | */ |
| 3725 | static PyObject *_PyPopenProcs = NULL; |
| 3726 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3727 | |
| 3728 | /* popen that works from a GUI. |
| 3729 | * |
| 3730 | * The result of this function is a pipe (file) connected to the |
| 3731 | * processes stdin or stdout, depending on the requested mode. |
| 3732 | */ |
| 3733 | |
| 3734 | static PyObject * |
| 3735 | posix_popen(PyObject *self, PyObject *args) |
| 3736 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3737 | PyObject *f, *s; |
| 3738 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3739 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3740 | char *cmdstring; |
| 3741 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3742 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3743 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3744 | return NULL; |
| 3745 | |
| 3746 | s = PyTuple_New(0); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3747 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3748 | if (*mode == 'r') |
| 3749 | tm = _O_RDONLY; |
| 3750 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3751 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3752 | return NULL; |
| 3753 | } else |
| 3754 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3755 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3756 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3757 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3758 | return NULL; |
| 3759 | } |
| 3760 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3761 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3762 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3763 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3764 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3765 | else |
| 3766 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 3767 | |
| 3768 | return f; |
| 3769 | } |
| 3770 | |
| 3771 | /* Variation on win32pipe.popen |
| 3772 | * |
| 3773 | * The result of this function is a pipe (file) connected to the |
| 3774 | * process's stdin, and a pipe connected to the process's stdout. |
| 3775 | */ |
| 3776 | |
| 3777 | static PyObject * |
| 3778 | win32_popen2(PyObject *self, PyObject *args) |
| 3779 | { |
| 3780 | PyObject *f; |
| 3781 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3782 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3783 | char *cmdstring; |
| 3784 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3785 | int bufsize = -1; |
| 3786 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3787 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3788 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3789 | if (*mode == 't') |
| 3790 | tm = _O_TEXT; |
| 3791 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3792 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3793 | return NULL; |
| 3794 | } else |
| 3795 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3796 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3797 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3798 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3799 | return NULL; |
| 3800 | } |
| 3801 | |
| 3802 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3803 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3804 | return f; |
| 3805 | } |
| 3806 | |
| 3807 | /* |
| 3808 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3809 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3810 | * The result of this function is 3 pipes - the process's stdin, |
| 3811 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3812 | */ |
| 3813 | |
| 3814 | static PyObject * |
| 3815 | win32_popen3(PyObject *self, PyObject *args) |
| 3816 | { |
| 3817 | PyObject *f; |
| 3818 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3819 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3820 | char *cmdstring; |
| 3821 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3822 | int bufsize = -1; |
| 3823 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3824 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3825 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3826 | if (*mode == 't') |
| 3827 | tm = _O_TEXT; |
| 3828 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3829 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3830 | return NULL; |
| 3831 | } else |
| 3832 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3833 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3834 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3835 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3836 | return NULL; |
| 3837 | } |
| 3838 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3839 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3840 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3841 | return f; |
| 3842 | } |
| 3843 | |
| 3844 | /* |
| 3845 | * Variation on win32pipe.popen |
| 3846 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3847 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3848 | * and stdout+stderr combined as a single pipe. |
| 3849 | */ |
| 3850 | |
| 3851 | static PyObject * |
| 3852 | win32_popen4(PyObject *self, PyObject *args) |
| 3853 | { |
| 3854 | PyObject *f; |
| 3855 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3856 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3857 | char *cmdstring; |
| 3858 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3859 | int bufsize = -1; |
| 3860 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3861 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3862 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3863 | if (*mode == 't') |
| 3864 | tm = _O_TEXT; |
| 3865 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3866 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3867 | return NULL; |
| 3868 | } else |
| 3869 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3870 | |
| 3871 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3872 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3873 | return NULL; |
| 3874 | } |
| 3875 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3876 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3877 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3878 | return f; |
| 3879 | } |
| 3880 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3881 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3882 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3883 | HANDLE hStdin, |
| 3884 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3885 | HANDLE hStderr, |
| 3886 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3887 | { |
| 3888 | PROCESS_INFORMATION piProcInfo; |
| 3889 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3890 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3891 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3892 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3893 | int i; |
| 3894 | int x; |
| 3895 | |
| 3896 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3897 | char *comshell; |
| 3898 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3899 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3900 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 3901 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3902 | |
| 3903 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 3904 | * then use the w9xpopen hack. |
| 3905 | */ |
| 3906 | comshell = s1 + x; |
| 3907 | while (comshell >= s1 && *comshell != '\\') |
| 3908 | --comshell; |
| 3909 | ++comshell; |
| 3910 | |
| 3911 | if (GetVersion() < 0x80000000 && |
| 3912 | _stricmp(comshell, "command.com") != 0) { |
| 3913 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3914 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3915 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3916 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3917 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3918 | } |
| 3919 | else { |
| 3920 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3921 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 3922 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3923 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3924 | char modulepath[_MAX_PATH]; |
| 3925 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3926 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 3927 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3928 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3929 | x = i+1; |
| 3930 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3931 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3932 | strncat(modulepath, |
| 3933 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3934 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3935 | -strlen(modulepath)); |
| 3936 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3937 | /* Eeek - file-not-found - possibly an embedding |
| 3938 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3939 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3940 | strncpy(modulepath, |
| 3941 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3942 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 3943 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 3944 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3945 | strncat(modulepath, |
| 3946 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3947 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3948 | -strlen(modulepath)); |
| 3949 | /* No where else to look - raise an easily identifiable |
| 3950 | error, rather than leaving Windows to report |
| 3951 | "file not found" - as the user is probably blissfully |
| 3952 | unaware this shim EXE is used, and it will confuse them. |
| 3953 | (well, it confused me for a while ;-) |
| 3954 | */ |
| 3955 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3956 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3957 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3958 | "for popen to work with your shell " |
| 3959 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3960 | szConsoleSpawn); |
| 3961 | return FALSE; |
| 3962 | } |
| 3963 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3964 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3965 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3966 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3967 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3968 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3969 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3970 | /* To maintain correct argument passing semantics, |
| 3971 | we pass the command-line as it stands, and allow |
| 3972 | quoting to be applied. w9xpopen.exe will then |
| 3973 | use its argv vector, and re-quote the necessary |
| 3974 | args for the ultimate child process. |
| 3975 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3976 | PyOS_snprintf( |
| 3977 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3978 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3979 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3980 | s1, |
| 3981 | s3, |
| 3982 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3983 | /* Not passing CREATE_NEW_CONSOLE has been known to |
| 3984 | cause random failures on win9x. Specifically a |
| 3985 | dialog: |
| 3986 | "Your program accessed mem currently in use at xxx" |
| 3987 | and a hopeful warning about the stability of your |
| 3988 | system. |
| 3989 | Cost is Ctrl+C wont kill children, but anyone |
| 3990 | who cares can have a go! |
| 3991 | */ |
| 3992 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3993 | } |
| 3994 | } |
| 3995 | |
| 3996 | /* Could be an else here to try cmd.exe / command.com in the path |
| 3997 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3998 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3999 | PyErr_SetString(PyExc_RuntimeError, |
| 4000 | "Cannot locate a COMSPEC environment variable to " |
| 4001 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4002 | return FALSE; |
| 4003 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4004 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4005 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4006 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4007 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4008 | siStartInfo.hStdInput = hStdin; |
| 4009 | siStartInfo.hStdOutput = hStdout; |
| 4010 | siStartInfo.hStdError = hStderr; |
| 4011 | siStartInfo.wShowWindow = SW_HIDE; |
| 4012 | |
| 4013 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4014 | s2, |
| 4015 | NULL, |
| 4016 | NULL, |
| 4017 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4018 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4019 | NULL, |
| 4020 | NULL, |
| 4021 | &siStartInfo, |
| 4022 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4023 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4024 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4025 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4026 | /* Return process handle */ |
| 4027 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4028 | return TRUE; |
| 4029 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4030 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4031 | return FALSE; |
| 4032 | } |
| 4033 | |
| 4034 | /* The following code is based off of KB: Q190351 */ |
| 4035 | |
| 4036 | static PyObject * |
| 4037 | _PyPopen(char *cmdstring, int mode, int n) |
| 4038 | { |
| 4039 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4040 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4041 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4042 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4043 | SECURITY_ATTRIBUTES saAttr; |
| 4044 | BOOL fSuccess; |
| 4045 | int fd1, fd2, fd3; |
| 4046 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4047 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4048 | PyObject *f; |
| 4049 | |
| 4050 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4051 | saAttr.bInheritHandle = TRUE; |
| 4052 | saAttr.lpSecurityDescriptor = NULL; |
| 4053 | |
| 4054 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4055 | return win32_error("CreatePipe", NULL); |
| 4056 | |
| 4057 | /* Create new output read handle and the input write handle. Set |
| 4058 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 4059 | * the these handles; resulting in non-closeable handles to the pipes |
| 4060 | * being created. */ |
| 4061 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4062 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4063 | FALSE, |
| 4064 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4065 | if (!fSuccess) |
| 4066 | return win32_error("DuplicateHandle", NULL); |
| 4067 | |
| 4068 | /* Close the inheritable version of ChildStdin |
| 4069 | that we're using. */ |
| 4070 | CloseHandle(hChildStdinWr); |
| 4071 | |
| 4072 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4073 | return win32_error("CreatePipe", NULL); |
| 4074 | |
| 4075 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4076 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4077 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4078 | if (!fSuccess) |
| 4079 | return win32_error("DuplicateHandle", NULL); |
| 4080 | |
| 4081 | /* Close the inheritable version of ChildStdout |
| 4082 | that we're using. */ |
| 4083 | CloseHandle(hChildStdoutRd); |
| 4084 | |
| 4085 | if (n != POPEN_4) { |
| 4086 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4087 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4088 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4089 | hChildStderrRd, |
| 4090 | GetCurrentProcess(), |
| 4091 | &hChildStderrRdDup, 0, |
| 4092 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4093 | if (!fSuccess) |
| 4094 | return win32_error("DuplicateHandle", NULL); |
| 4095 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4096 | CloseHandle(hChildStderrRd); |
| 4097 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4098 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4099 | switch (n) { |
| 4100 | case POPEN_1: |
| 4101 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4102 | case _O_WRONLY | _O_TEXT: |
| 4103 | /* Case for writing to child Stdin in text mode. */ |
| 4104 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4105 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4106 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4107 | PyFile_SetBufSize(f, 0); |
| 4108 | /* We don't care about these pipes anymore, so close them. */ |
| 4109 | CloseHandle(hChildStdoutRdDup); |
| 4110 | CloseHandle(hChildStderrRdDup); |
| 4111 | break; |
| 4112 | |
| 4113 | case _O_RDONLY | _O_TEXT: |
| 4114 | /* Case for reading from child Stdout in text mode. */ |
| 4115 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4116 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4117 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4118 | PyFile_SetBufSize(f, 0); |
| 4119 | /* We don't care about these pipes anymore, so close them. */ |
| 4120 | CloseHandle(hChildStdinWrDup); |
| 4121 | CloseHandle(hChildStderrRdDup); |
| 4122 | break; |
| 4123 | |
| 4124 | case _O_RDONLY | _O_BINARY: |
| 4125 | /* Case for readinig from child Stdout in binary mode. */ |
| 4126 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4127 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4128 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4129 | PyFile_SetBufSize(f, 0); |
| 4130 | /* We don't care about these pipes anymore, so close them. */ |
| 4131 | CloseHandle(hChildStdinWrDup); |
| 4132 | CloseHandle(hChildStderrRdDup); |
| 4133 | break; |
| 4134 | |
| 4135 | case _O_WRONLY | _O_BINARY: |
| 4136 | /* Case for writing to child Stdin in binary mode. */ |
| 4137 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4138 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4139 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4140 | PyFile_SetBufSize(f, 0); |
| 4141 | /* We don't care about these pipes anymore, so close them. */ |
| 4142 | CloseHandle(hChildStdoutRdDup); |
| 4143 | CloseHandle(hChildStderrRdDup); |
| 4144 | break; |
| 4145 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4146 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4147 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4148 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4149 | case POPEN_2: |
| 4150 | case POPEN_4: |
| 4151 | { |
| 4152 | char *m1, *m2; |
| 4153 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4154 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4155 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4156 | m1 = "r"; |
| 4157 | m2 = "w"; |
| 4158 | } else { |
| 4159 | m1 = "rb"; |
| 4160 | m2 = "wb"; |
| 4161 | } |
| 4162 | |
| 4163 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4164 | f1 = _fdopen(fd1, m2); |
| 4165 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4166 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4167 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4168 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4169 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4170 | PyFile_SetBufSize(p2, 0); |
| 4171 | |
| 4172 | if (n != 4) |
| 4173 | CloseHandle(hChildStderrRdDup); |
| 4174 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4175 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4176 | Py_XDECREF(p1); |
| 4177 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4178 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4179 | break; |
| 4180 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4181 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4182 | case POPEN_3: |
| 4183 | { |
| 4184 | char *m1, *m2; |
| 4185 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4186 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4187 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4188 | m1 = "r"; |
| 4189 | m2 = "w"; |
| 4190 | } else { |
| 4191 | m1 = "rb"; |
| 4192 | m2 = "wb"; |
| 4193 | } |
| 4194 | |
| 4195 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4196 | f1 = _fdopen(fd1, m2); |
| 4197 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4198 | f2 = _fdopen(fd2, m1); |
| 4199 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 4200 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4201 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4202 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4203 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4204 | PyFile_SetBufSize(p1, 0); |
| 4205 | PyFile_SetBufSize(p2, 0); |
| 4206 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4207 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4208 | Py_XDECREF(p1); |
| 4209 | Py_XDECREF(p2); |
| 4210 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4211 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4212 | break; |
| 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | if (n == POPEN_4) { |
| 4217 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4218 | hChildStdinRd, |
| 4219 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4220 | hChildStdoutWr, |
| 4221 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4222 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4223 | } |
| 4224 | else { |
| 4225 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4226 | hChildStdinRd, |
| 4227 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4228 | hChildStderrWr, |
| 4229 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4230 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4233 | /* |
| 4234 | * Insert the files we've created into the process dictionary |
| 4235 | * all referencing the list with the process handle and the |
| 4236 | * initial number of files (see description below in _PyPclose). |
| 4237 | * Since if _PyPclose later tried to wait on a process when all |
| 4238 | * handles weren't closed, it could create a deadlock with the |
| 4239 | * child, we spend some energy here to try to ensure that we |
| 4240 | * either insert all file handles into the dictionary or none |
| 4241 | * at all. It's a little clumsy with the various popen modes |
| 4242 | * and variable number of files involved. |
| 4243 | */ |
| 4244 | if (!_PyPopenProcs) { |
| 4245 | _PyPopenProcs = PyDict_New(); |
| 4246 | } |
| 4247 | |
| 4248 | if (_PyPopenProcs) { |
| 4249 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4250 | int ins_rc[3]; |
| 4251 | |
| 4252 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4253 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4254 | |
| 4255 | procObj = PyList_New(2); |
| 4256 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4257 | intObj = PyInt_FromLong(file_count); |
| 4258 | |
| 4259 | if (procObj && hProcessObj && intObj) { |
| 4260 | PyList_SetItem(procObj,0,hProcessObj); |
| 4261 | PyList_SetItem(procObj,1,intObj); |
| 4262 | |
| 4263 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4264 | if (fileObj[0]) { |
| 4265 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4266 | fileObj[0], |
| 4267 | procObj); |
| 4268 | } |
| 4269 | if (file_count >= 2) { |
| 4270 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4271 | if (fileObj[1]) { |
| 4272 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4273 | fileObj[1], |
| 4274 | procObj); |
| 4275 | } |
| 4276 | } |
| 4277 | if (file_count >= 3) { |
| 4278 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4279 | if (fileObj[2]) { |
| 4280 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4281 | fileObj[2], |
| 4282 | procObj); |
| 4283 | } |
| 4284 | } |
| 4285 | |
| 4286 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4287 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4288 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4289 | /* Something failed - remove any dictionary |
| 4290 | * entries that did make it. |
| 4291 | */ |
| 4292 | if (!ins_rc[0] && fileObj[0]) { |
| 4293 | PyDict_DelItem(_PyPopenProcs, |
| 4294 | fileObj[0]); |
| 4295 | } |
| 4296 | if (!ins_rc[1] && fileObj[1]) { |
| 4297 | PyDict_DelItem(_PyPopenProcs, |
| 4298 | fileObj[1]); |
| 4299 | } |
| 4300 | if (!ins_rc[2] && fileObj[2]) { |
| 4301 | PyDict_DelItem(_PyPopenProcs, |
| 4302 | fileObj[2]); |
| 4303 | } |
| 4304 | } |
| 4305 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4306 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4307 | /* |
| 4308 | * Clean up our localized references for the dictionary keys |
| 4309 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4310 | * that got placed in the dictionary. |
| 4311 | */ |
| 4312 | Py_XDECREF(procObj); |
| 4313 | Py_XDECREF(fileObj[0]); |
| 4314 | Py_XDECREF(fileObj[1]); |
| 4315 | Py_XDECREF(fileObj[2]); |
| 4316 | } |
| 4317 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4318 | /* Child is launched. Close the parents copy of those pipe |
| 4319 | * handles that only the child should have open. You need to |
| 4320 | * make sure that no handles to the write end of the output pipe |
| 4321 | * are maintained in this process or else the pipe will not close |
| 4322 | * when the child process exits and the ReadFile will hang. */ |
| 4323 | |
| 4324 | if (!CloseHandle(hChildStdinRd)) |
| 4325 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4326 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4327 | if (!CloseHandle(hChildStdoutWr)) |
| 4328 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4329 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4330 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4331 | return win32_error("CloseHandle", NULL); |
| 4332 | |
| 4333 | return f; |
| 4334 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4335 | |
| 4336 | /* |
| 4337 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4338 | * exit code for the child process and return as a result of the close. |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4339 | * |
| 4340 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4341 | * input file pointer to information about the process that was |
| 4342 | * originally created by the popen* call that created the file pointer. |
| 4343 | * The dictionary uses the file pointer as a key (with one entry |
| 4344 | * inserted for each file returned by the original popen* call) and a |
| 4345 | * single list object as the value for all files from a single call. |
| 4346 | * The list object contains the Win32 process handle at [0], and a file |
| 4347 | * count at [1], which is initialized to the total number of file |
| 4348 | * handles using that list. |
| 4349 | * |
| 4350 | * This function closes whichever handle it is passed, and decrements |
| 4351 | * the file count in the dictionary for the process handle pointed to |
| 4352 | * by this file. On the last close (when the file count reaches zero), |
| 4353 | * this function will wait for the child process and then return its |
| 4354 | * exit code as the result of the close() operation. This permits the |
| 4355 | * files to be closed in any order - it is always the close() of the |
| 4356 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4357 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4358 | |
| 4359 | /* RED_FLAG 31-Aug-2000 Tim |
| 4360 | * This is always called (today!) between a pair of |
| 4361 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 4362 | * macros. So the thread running this has no valid thread state, as |
| 4363 | * far as Python is concerned. However, this calls some Python API |
| 4364 | * functions that cannot be called safely without a valid thread |
| 4365 | * state, in particular PyDict_GetItem. |
| 4366 | * As a temporary hack (although it may last for years ...), we |
| 4367 | * *rely* on not having a valid thread state in this function, in |
| 4368 | * order to create our own "from scratch". |
| 4369 | * This will deadlock if _PyPclose is ever called by a thread |
| 4370 | * holding the global lock. |
| 4371 | */ |
| 4372 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4373 | static int _PyPclose(FILE *file) |
| 4374 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4375 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4376 | DWORD exit_code; |
| 4377 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4378 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4379 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4380 | #ifdef WITH_THREAD |
| 4381 | PyInterpreterState* pInterpreterState; |
| 4382 | PyThreadState* pThreadState; |
| 4383 | #endif |
| 4384 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4385 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4386 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4387 | */ |
| 4388 | result = fclose(file); |
| 4389 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4390 | #ifdef WITH_THREAD |
| 4391 | /* Bootstrap a valid thread state into existence. */ |
| 4392 | pInterpreterState = PyInterpreterState_New(); |
| 4393 | if (!pInterpreterState) { |
| 4394 | /* Well, we're hosed now! We don't have a thread |
| 4395 | * state, so can't call a nice error routine, or raise |
| 4396 | * an exception. Just die. |
| 4397 | */ |
| 4398 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4399 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4400 | return -1; /* unreachable */ |
| 4401 | } |
| 4402 | pThreadState = PyThreadState_New(pInterpreterState); |
| 4403 | if (!pThreadState) { |
| 4404 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4405 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4406 | return -1; /* unreachable */ |
| 4407 | } |
| 4408 | /* Grab the global lock. Note that this will deadlock if the |
| 4409 | * current thread already has the lock! (see RED_FLAG comments |
| 4410 | * before this function) |
| 4411 | */ |
| 4412 | PyEval_RestoreThread(pThreadState); |
| 4413 | #endif |
| 4414 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4415 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4416 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4417 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4418 | fileObj)) != NULL && |
| 4419 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4420 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4421 | |
| 4422 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4423 | file_count = PyInt_AsLong(intObj); |
| 4424 | |
| 4425 | if (file_count > 1) { |
| 4426 | /* Still other files referencing process */ |
| 4427 | file_count--; |
| 4428 | PyList_SetItem(procObj,1, |
| 4429 | PyInt_FromLong(file_count)); |
| 4430 | } else { |
| 4431 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4432 | if (result != EOF && |
| 4433 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4434 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4435 | /* Possible truncation here in 16-bit environments, but |
| 4436 | * real exit codes are just the lower byte in any event. |
| 4437 | */ |
| 4438 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4439 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4440 | /* Indicate failure - this will cause the file object |
| 4441 | * to raise an I/O error and translate the last Win32 |
| 4442 | * error code from errno. We do have a problem with |
| 4443 | * last errors that overlap the normal errno table, |
| 4444 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4445 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4446 | if (result != EOF) { |
| 4447 | /* If the error wasn't from the fclose(), then |
| 4448 | * set errno for the file object error handling. |
| 4449 | */ |
| 4450 | errno = GetLastError(); |
| 4451 | } |
| 4452 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | /* Free up the native handle at this point */ |
| 4456 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4457 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4458 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4459 | /* Remove this file pointer from dictionary */ |
| 4460 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4461 | |
| 4462 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4463 | Py_DECREF(_PyPopenProcs); |
| 4464 | _PyPopenProcs = NULL; |
| 4465 | } |
| 4466 | |
| 4467 | } /* if object retrieval ok */ |
| 4468 | |
| 4469 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4470 | } /* if _PyPopenProcs */ |
| 4471 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4472 | #ifdef WITH_THREAD |
| 4473 | /* Tear down the thread & interpreter states. |
| 4474 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4475 | * call the thread clear & delete functions, and indeed insist on |
| 4476 | * doing that themselves. The lock must be held during the clear, but |
| 4477 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4478 | */ |
| 4479 | PyInterpreterState_Clear(pInterpreterState); |
| 4480 | PyEval_ReleaseThread(pThreadState); |
| 4481 | PyInterpreterState_Delete(pInterpreterState); |
| 4482 | #endif |
| 4483 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4484 | return result; |
| 4485 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4486 | |
| 4487 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4488 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4489 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4490 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4491 | char *name; |
| 4492 | char *mode = "r"; |
| 4493 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4494 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4495 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4496 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4497 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4498 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4499 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4500 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4501 | if (fp == NULL) |
| 4502 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4503 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4504 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4505 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4506 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4507 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4508 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4509 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4510 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4511 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4512 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4513 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4514 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4515 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4516 | Set the current process's user id."); |
| 4517 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4518 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4519 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4520 | { |
| 4521 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4522 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4523 | return NULL; |
| 4524 | if (setuid(uid) < 0) |
| 4525 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4526 | Py_INCREF(Py_None); |
| 4527 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4528 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4529 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4530 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4531 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4532 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4533 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4534 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4535 | Set the current process's effective user id."); |
| 4536 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4537 | static PyObject * |
| 4538 | posix_seteuid (PyObject *self, PyObject *args) |
| 4539 | { |
| 4540 | int euid; |
| 4541 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4542 | return NULL; |
| 4543 | } else if (seteuid(euid) < 0) { |
| 4544 | return posix_error(); |
| 4545 | } else { |
| 4546 | Py_INCREF(Py_None); |
| 4547 | return Py_None; |
| 4548 | } |
| 4549 | } |
| 4550 | #endif /* HAVE_SETEUID */ |
| 4551 | |
| 4552 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4553 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4554 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4555 | Set the current process's effective group id."); |
| 4556 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4557 | static PyObject * |
| 4558 | posix_setegid (PyObject *self, PyObject *args) |
| 4559 | { |
| 4560 | int egid; |
| 4561 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4562 | return NULL; |
| 4563 | } else if (setegid(egid) < 0) { |
| 4564 | return posix_error(); |
| 4565 | } else { |
| 4566 | Py_INCREF(Py_None); |
| 4567 | return Py_None; |
| 4568 | } |
| 4569 | } |
| 4570 | #endif /* HAVE_SETEGID */ |
| 4571 | |
| 4572 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4573 | PyDoc_STRVAR(posix_setreuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4574 | "seteuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4575 | Set the current process's real and effective user ids."); |
| 4576 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4577 | static PyObject * |
| 4578 | posix_setreuid (PyObject *self, PyObject *args) |
| 4579 | { |
| 4580 | int ruid, euid; |
| 4581 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4582 | return NULL; |
| 4583 | } else if (setreuid(ruid, euid) < 0) { |
| 4584 | return posix_error(); |
| 4585 | } else { |
| 4586 | Py_INCREF(Py_None); |
| 4587 | return Py_None; |
| 4588 | } |
| 4589 | } |
| 4590 | #endif /* HAVE_SETREUID */ |
| 4591 | |
| 4592 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4593 | PyDoc_STRVAR(posix_setregid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4594 | "setegid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4595 | Set the current process's real and effective group ids."); |
| 4596 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4597 | static PyObject * |
| 4598 | posix_setregid (PyObject *self, PyObject *args) |
| 4599 | { |
| 4600 | int rgid, egid; |
| 4601 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4602 | return NULL; |
| 4603 | } else if (setregid(rgid, egid) < 0) { |
| 4604 | return posix_error(); |
| 4605 | } else { |
| 4606 | Py_INCREF(Py_None); |
| 4607 | return Py_None; |
| 4608 | } |
| 4609 | } |
| 4610 | #endif /* HAVE_SETREGID */ |
| 4611 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4612 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4613 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4614 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4615 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4616 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4617 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4618 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4619 | { |
| 4620 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4621 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4622 | return NULL; |
| 4623 | if (setgid(gid) < 0) |
| 4624 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4625 | Py_INCREF(Py_None); |
| 4626 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4627 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4628 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4629 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4630 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4631 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4632 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4633 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4634 | |
| 4635 | static PyObject * |
| 4636 | posix_setgroups(PyObject *self, PyObject *args) |
| 4637 | { |
| 4638 | PyObject *groups; |
| 4639 | int i, len; |
| 4640 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4641 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4642 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 4643 | return NULL; |
| 4644 | if (!PySequence_Check(groups)) { |
| 4645 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4646 | return NULL; |
| 4647 | } |
| 4648 | len = PySequence_Size(groups); |
| 4649 | if (len > MAX_GROUPS) { |
| 4650 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4651 | return NULL; |
| 4652 | } |
| 4653 | for(i = 0; i < len; i++) { |
| 4654 | PyObject *elem; |
| 4655 | elem = PySequence_GetItem(groups, i); |
| 4656 | if (!elem) |
| 4657 | return NULL; |
| 4658 | if (!PyInt_Check(elem)) { |
| 4659 | PyErr_SetString(PyExc_TypeError, |
| 4660 | "groups must be integers"); |
| 4661 | Py_DECREF(elem); |
| 4662 | return NULL; |
| 4663 | } |
| 4664 | /* XXX: check that value fits into gid_t. */ |
| 4665 | grouplist[i] = PyInt_AsLong(elem); |
| 4666 | Py_DECREF(elem); |
| 4667 | } |
| 4668 | |
| 4669 | if (setgroups(len, grouplist) < 0) |
| 4670 | return posix_error(); |
| 4671 | Py_INCREF(Py_None); |
| 4672 | return Py_None; |
| 4673 | } |
| 4674 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4675 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4676 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4677 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4678 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4679 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4680 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4681 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4682 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4683 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4684 | int pid, options; |
| 4685 | #ifdef UNION_WAIT |
| 4686 | union wait status; |
| 4687 | #define status_i (status.w_status) |
| 4688 | #else |
| 4689 | int status; |
| 4690 | #define status_i status |
| 4691 | #endif |
| 4692 | status_i = 0; |
| 4693 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4694 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4695 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4696 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4697 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4698 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4699 | if (pid == -1) |
| 4700 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4701 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4702 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4705 | #elif defined(HAVE_CWAIT) |
| 4706 | |
| 4707 | /* 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] | 4708 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4709 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4710 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4711 | |
| 4712 | static PyObject * |
| 4713 | posix_waitpid(PyObject *self, PyObject *args) |
| 4714 | { |
| 4715 | int pid, options; |
| 4716 | int status; |
| 4717 | |
| 4718 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4719 | return NULL; |
| 4720 | Py_BEGIN_ALLOW_THREADS |
| 4721 | pid = _cwait(&status, pid, options); |
| 4722 | Py_END_ALLOW_THREADS |
| 4723 | if (pid == -1) |
| 4724 | return posix_error(); |
| 4725 | else |
| 4726 | /* shift the status left a byte so this is more like the |
| 4727 | POSIX waitpid */ |
| 4728 | return Py_BuildValue("ii", pid, status << 8); |
| 4729 | } |
| 4730 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4731 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4732 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4733 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4734 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4735 | Wait for completion of a child process."); |
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 * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4738 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4739 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4740 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4741 | #ifdef UNION_WAIT |
| 4742 | union wait status; |
| 4743 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4744 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4745 | int status; |
| 4746 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4747 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4748 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4749 | status_i = 0; |
| 4750 | Py_BEGIN_ALLOW_THREADS |
| 4751 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4752 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4753 | if (pid == -1) |
| 4754 | return posix_error(); |
| 4755 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4756 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4757 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4758 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4759 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4760 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4762 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4763 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4764 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4765 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4766 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4767 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4768 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4769 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4770 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4771 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4772 | #ifdef MS_WINDOWS |
| 4773 | return posix_do_stat(self, args, "et:lstat", STAT, "u:lstat", _wstati64); |
| 4774 | #else |
| 4775 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4776 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4777 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4780 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4781 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4782 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4783 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4784 | 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] | 4785 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4786 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4787 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4788 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4789 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4790 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4791 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4792 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4793 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4794 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4795 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4796 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4797 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4798 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4799 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4800 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4801 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4802 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4803 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4804 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4805 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4806 | "symlink(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4807 | Create a symbolic link."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4808 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4809 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4810 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4811 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4812 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4813 | } |
| 4814 | #endif /* HAVE_SYMLINK */ |
| 4815 | |
| 4816 | |
| 4817 | #ifdef HAVE_TIMES |
| 4818 | #ifndef HZ |
| 4819 | #define HZ 60 /* Universal constant :-) */ |
| 4820 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4821 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4822 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4823 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4824 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4825 | { |
| 4826 | ULONG value = 0; |
| 4827 | |
| 4828 | Py_BEGIN_ALLOW_THREADS |
| 4829 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4830 | Py_END_ALLOW_THREADS |
| 4831 | |
| 4832 | return value; |
| 4833 | } |
| 4834 | |
| 4835 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4836 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4837 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4838 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4839 | return Py_BuildValue("ddddd", |
| 4840 | (double)0 /* t.tms_utime / HZ */, |
| 4841 | (double)0 /* t.tms_stime / HZ */, |
| 4842 | (double)0 /* t.tms_cutime / HZ */, |
| 4843 | (double)0 /* t.tms_cstime / HZ */, |
| 4844 | (double)system_uptime() / 1000); |
| 4845 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4846 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4847 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4848 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4849 | { |
| 4850 | struct tms t; |
| 4851 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4852 | errno = 0; |
| 4853 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4854 | if (c == (clock_t) -1) |
| 4855 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4856 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4857 | (double)t.tms_utime / HZ, |
| 4858 | (double)t.tms_stime / HZ, |
| 4859 | (double)t.tms_cutime / HZ, |
| 4860 | (double)t.tms_cstime / HZ, |
| 4861 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4862 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4863 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4864 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4865 | |
| 4866 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4867 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4868 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4869 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4870 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4871 | { |
| 4872 | FILETIME create, exit, kernel, user; |
| 4873 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4874 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4875 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4876 | /* The fields of a FILETIME structure are the hi and lo part |
| 4877 | of a 64-bit value expressed in 100 nanosecond units. |
| 4878 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4879 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4880 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4881 | return Py_BuildValue( |
| 4882 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4883 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4884 | kernel.dwLowDateTime*1e-7), |
| 4885 | (double)(user.dwHighDateTime*429.4967296 + |
| 4886 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4887 | (double)0, |
| 4888 | (double)0, |
| 4889 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4890 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4891 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4892 | |
| 4893 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4894 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4895 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4896 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4897 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4898 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4899 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4900 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4901 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4902 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4903 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4904 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4905 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4906 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4907 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4908 | if (setsid() < 0) |
| 4909 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4910 | Py_INCREF(Py_None); |
| 4911 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4912 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4913 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4914 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4915 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4916 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4917 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4918 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4919 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4920 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4921 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4922 | { |
| 4923 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4924 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4925 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4926 | if (setpgid(pid, pgrp) < 0) |
| 4927 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4928 | Py_INCREF(Py_None); |
| 4929 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4930 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4931 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4932 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4933 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4934 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4935 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4936 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4937 | 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] | 4938 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4939 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4940 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4941 | { |
| 4942 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4943 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4944 | return NULL; |
| 4945 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4946 | if (pgid < 0) |
| 4947 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4948 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4949 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4950 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4951 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4952 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4953 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4954 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4955 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4956 | 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] | 4957 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4958 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4959 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4960 | { |
| 4961 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4962 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4963 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4964 | if (tcsetpgrp(fd, pgid) < 0) |
| 4965 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4966 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4967 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4968 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4969 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4970 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4971 | /* Functions acting on file descriptors */ |
| 4972 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4973 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4974 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4975 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4976 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4977 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4978 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4979 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4980 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4981 | int flag; |
| 4982 | int mode = 0777; |
| 4983 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4984 | |
| 4985 | #ifdef MS_WINDOWS |
| 4986 | if (unicode_file_names()) { |
| 4987 | PyUnicodeObject *po; |
| 4988 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4989 | Py_BEGIN_ALLOW_THREADS |
| 4990 | /* PyUnicode_AS_UNICODE OK without thread |
| 4991 | lock as it is a simple dereference. */ |
| 4992 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4993 | Py_END_ALLOW_THREADS |
| 4994 | if (fd < 0) |
| 4995 | return posix_error(); |
| 4996 | return PyInt_FromLong((long)fd); |
| 4997 | } |
| 4998 | /* Drop the argument parsing error as narrow strings |
| 4999 | are also valid. */ |
| 5000 | PyErr_Clear(); |
| 5001 | } |
| 5002 | #endif |
| 5003 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5004 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5005 | Py_FileSystemDefaultEncoding, &file, |
| 5006 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5007 | return NULL; |
| 5008 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5009 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5010 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5011 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5012 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5013 | return posix_error_with_allocated_filename(file); |
| 5014 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5015 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5018 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5019 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5020 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5021 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5022 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5023 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5024 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5025 | { |
| 5026 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5027 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5028 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5029 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5030 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5031 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5032 | if (res < 0) |
| 5033 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5034 | Py_INCREF(Py_None); |
| 5035 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5038 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5039 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5040 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5041 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5042 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5043 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5044 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5045 | { |
| 5046 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5047 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5048 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5049 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5050 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5051 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5052 | if (fd < 0) |
| 5053 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5054 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5057 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5058 | PyDoc_STRVAR(posix_dup2__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5059 | "dup2(fd, fd2)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5060 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5061 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5062 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5063 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5064 | { |
| 5065 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5066 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5067 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5068 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5069 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5070 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5071 | if (res < 0) |
| 5072 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5073 | Py_INCREF(Py_None); |
| 5074 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5075 | } |
| 5076 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5077 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5078 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5079 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5080 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5081 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5082 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5083 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5084 | { |
| 5085 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5086 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5087 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5088 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5089 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5090 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5091 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5092 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5093 | return NULL; |
| 5094 | #ifdef SEEK_SET |
| 5095 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5096 | switch (how) { |
| 5097 | case 0: how = SEEK_SET; break; |
| 5098 | case 1: how = SEEK_CUR; break; |
| 5099 | case 2: how = SEEK_END; break; |
| 5100 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5101 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5102 | |
| 5103 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5104 | pos = PyInt_AsLong(posobj); |
| 5105 | #else |
| 5106 | pos = PyLong_Check(posobj) ? |
| 5107 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5108 | #endif |
| 5109 | if (PyErr_Occurred()) |
| 5110 | return NULL; |
| 5111 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5112 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5113 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5114 | res = _lseeki64(fd, pos, how); |
| 5115 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5116 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5117 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5118 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5119 | if (res < 0) |
| 5120 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5121 | |
| 5122 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5123 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5124 | #else |
| 5125 | return PyLong_FromLongLong(res); |
| 5126 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5129 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5130 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5131 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5132 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5133 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5134 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5135 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5136 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5137 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5138 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5139 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5140 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5141 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5142 | if (buffer == NULL) |
| 5143 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5144 | Py_BEGIN_ALLOW_THREADS |
| 5145 | n = read(fd, PyString_AsString(buffer), size); |
| 5146 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5147 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5148 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5149 | return posix_error(); |
| 5150 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5151 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5152 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5153 | return buffer; |
| 5154 | } |
| 5155 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5156 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5157 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5158 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5159 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5160 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5161 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5162 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5163 | { |
| 5164 | int fd, size; |
| 5165 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5166 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5167 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5168 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5169 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5170 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5171 | if (size < 0) |
| 5172 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5173 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5177 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5178 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5179 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5180 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5181 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5182 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5183 | { |
| 5184 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5185 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5186 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5187 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5188 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5189 | #ifdef __VMS |
| 5190 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5191 | fsync(fd); |
| 5192 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5193 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5194 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5195 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5196 | if (res != 0) |
| 5197 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5198 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5199 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5200 | } |
| 5201 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5203 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5204 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5205 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5206 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5207 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5208 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5209 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5210 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5211 | char *mode = "r"; |
| 5212 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5213 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5214 | PyObject *f; |
| 5215 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5216 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5217 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5218 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5219 | PyErr_Format(PyExc_ValueError, |
| 5220 | "invalid file mode '%s'", mode); |
| 5221 | return NULL; |
| 5222 | } |
| 5223 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5224 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5225 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5226 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5227 | if (fp == NULL) |
| 5228 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5229 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5230 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5231 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5232 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5235 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5236 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5237 | 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] | 5238 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5239 | |
| 5240 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5241 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5242 | { |
| 5243 | int fd; |
| 5244 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5245 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5246 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5247 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5248 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5249 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5250 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5251 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5252 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5253 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5254 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5255 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5256 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5257 | #if defined(PYOS_OS2) |
| 5258 | HFILE read, write; |
| 5259 | APIRET rc; |
| 5260 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5261 | Py_BEGIN_ALLOW_THREADS |
| 5262 | rc = DosCreatePipe( &read, &write, 4096); |
| 5263 | Py_END_ALLOW_THREADS |
| 5264 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5265 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5266 | |
| 5267 | return Py_BuildValue("(ii)", read, write); |
| 5268 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5269 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5270 | int fds[2]; |
| 5271 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5272 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 5273 | #if defined(__VMS) |
| 5274 | res = pipe(fds,0,2100); /* bigger mailbox quota than 512 */ |
| 5275 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5276 | res = pipe(fds); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 5277 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5278 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5279 | if (res != 0) |
| 5280 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5281 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5282 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5283 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5284 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5285 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5286 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5287 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5288 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5289 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5290 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5291 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5292 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5293 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5294 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5295 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5296 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5297 | #endif /* HAVE_PIPE */ |
| 5298 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5299 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5300 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5301 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5302 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5303 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5304 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5305 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5306 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5307 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5308 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5309 | int mode = 0666; |
| 5310 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5311 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5312 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5313 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5314 | res = mkfifo(filename, mode); |
| 5315 | Py_END_ALLOW_THREADS |
| 5316 | if (res < 0) |
| 5317 | return posix_error(); |
| 5318 | Py_INCREF(Py_None); |
| 5319 | return Py_None; |
| 5320 | } |
| 5321 | #endif |
| 5322 | |
| 5323 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5324 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5325 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5326 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5327 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5328 | named filename. mode specifies both the permissions to use and the\n\ |
| 5329 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5330 | 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] | 5331 | device defines the newly created device special file (probably using\n\ |
| 5332 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5333 | |
| 5334 | |
| 5335 | static PyObject * |
| 5336 | posix_mknod(PyObject *self, PyObject *args) |
| 5337 | { |
| 5338 | char *filename; |
| 5339 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5340 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5341 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5342 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5343 | return NULL; |
| 5344 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5345 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5346 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5347 | if (res < 0) |
| 5348 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5349 | Py_INCREF(Py_None); |
| 5350 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5351 | } |
| 5352 | #endif |
| 5353 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5354 | #ifdef HAVE_DEVICE_MACROS |
| 5355 | PyDoc_STRVAR(posix_major__doc__, |
| 5356 | "major(device) -> major number\n\ |
| 5357 | Extracts a device major number from a raw device number."); |
| 5358 | |
| 5359 | static PyObject * |
| 5360 | posix_major(PyObject *self, PyObject *args) |
| 5361 | { |
| 5362 | int device; |
| 5363 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5364 | return NULL; |
| 5365 | return PyInt_FromLong((long)major(device)); |
| 5366 | } |
| 5367 | |
| 5368 | PyDoc_STRVAR(posix_minor__doc__, |
| 5369 | "minor(device) -> minor number\n\ |
| 5370 | Extracts a device minor number from a raw device number."); |
| 5371 | |
| 5372 | static PyObject * |
| 5373 | posix_minor(PyObject *self, PyObject *args) |
| 5374 | { |
| 5375 | int device; |
| 5376 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5377 | return NULL; |
| 5378 | return PyInt_FromLong((long)minor(device)); |
| 5379 | } |
| 5380 | |
| 5381 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5382 | "makedev(major, minor) -> device number\n\ |
| 5383 | Composes a raw device number from the major and minor device numbers."); |
| 5384 | |
| 5385 | static PyObject * |
| 5386 | posix_makedev(PyObject *self, PyObject *args) |
| 5387 | { |
| 5388 | int major, minor; |
| 5389 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5390 | return NULL; |
| 5391 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5392 | } |
| 5393 | #endif /* device macros */ |
| 5394 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5395 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5396 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5397 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5398 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5399 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5400 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5401 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5402 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5403 | { |
| 5404 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5405 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5406 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5407 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5408 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5409 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5410 | return NULL; |
| 5411 | |
| 5412 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5413 | length = PyInt_AsLong(lenobj); |
| 5414 | #else |
| 5415 | length = PyLong_Check(lenobj) ? |
| 5416 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5417 | #endif |
| 5418 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5419 | return NULL; |
| 5420 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5421 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5422 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5423 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5424 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5425 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5426 | return NULL; |
| 5427 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5428 | Py_INCREF(Py_None); |
| 5429 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5430 | } |
| 5431 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5432 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5433 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5434 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5435 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5436 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5437 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5438 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5439 | * get re-set with another call for the same key. */ |
| 5440 | static PyObject *posix_putenv_garbage; |
| 5441 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5442 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5443 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5444 | { |
| 5445 | char *s1, *s2; |
| 5446 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5447 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5448 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5449 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5450 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5451 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5452 | |
| 5453 | #if defined(PYOS_OS2) |
| 5454 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5455 | APIRET rc; |
| 5456 | |
| 5457 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 5458 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 5459 | |
| 5460 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5461 | if (rc != NO_ERROR) |
| 5462 | return os2_error(rc); |
| 5463 | |
| 5464 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5465 | APIRET rc; |
| 5466 | |
| 5467 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 5468 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 5469 | |
| 5470 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5471 | if (rc != NO_ERROR) |
| 5472 | return os2_error(rc); |
| 5473 | } else { |
| 5474 | #endif |
| 5475 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5476 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5477 | len = strlen(s1) + strlen(s2) + 2; |
| 5478 | /* len includes space for a trailing \0; the size arg to |
| 5479 | PyString_FromStringAndSize does not count that */ |
| 5480 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5481 | if (newstr == NULL) |
| 5482 | return PyErr_NoMemory(); |
| 5483 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5484 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5485 | if (putenv(new)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5486 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5487 | posix_error(); |
| 5488 | return NULL; |
| 5489 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5490 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5491 | * this will cause previous value to be collected. This has to |
| 5492 | * happen after the real putenv() call because the old value |
| 5493 | * was still accessible until then. */ |
| 5494 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5495 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5496 | /* really not much we can do; just leak */ |
| 5497 | PyErr_Clear(); |
| 5498 | } |
| 5499 | else { |
| 5500 | Py_DECREF(newstr); |
| 5501 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5502 | |
| 5503 | #if defined(PYOS_OS2) |
| 5504 | } |
| 5505 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5506 | Py_INCREF(Py_None); |
| 5507 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5508 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5509 | #endif /* putenv */ |
| 5510 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5511 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5512 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5513 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5514 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5515 | |
| 5516 | static PyObject * |
| 5517 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5518 | { |
| 5519 | char *s1; |
| 5520 | |
| 5521 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5522 | return NULL; |
| 5523 | |
| 5524 | unsetenv(s1); |
| 5525 | |
| 5526 | /* Remove the key from posix_putenv_garbage; |
| 5527 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5528 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5529 | * old value was still accessible until then. |
| 5530 | */ |
| 5531 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5532 | PyTuple_GET_ITEM(args, 0))) { |
| 5533 | /* really not much we can do; just leak */ |
| 5534 | PyErr_Clear(); |
| 5535 | } |
| 5536 | |
| 5537 | Py_INCREF(Py_None); |
| 5538 | return Py_None; |
| 5539 | } |
| 5540 | #endif /* unsetenv */ |
| 5541 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5542 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5543 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5544 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5545 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5546 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5547 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5548 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5549 | { |
| 5550 | int code; |
| 5551 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5552 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5553 | return NULL; |
| 5554 | message = strerror(code); |
| 5555 | if (message == NULL) { |
| 5556 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5557 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5558 | return NULL; |
| 5559 | } |
| 5560 | return PyString_FromString(message); |
| 5561 | } |
| 5562 | #endif /* strerror */ |
| 5563 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5564 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5565 | #ifdef HAVE_SYS_WAIT_H |
| 5566 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5567 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5568 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5569 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5570 | 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] | 5571 | |
| 5572 | static PyObject * |
| 5573 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5574 | { |
| 5575 | #ifdef UNION_WAIT |
| 5576 | union wait status; |
| 5577 | #define status_i (status.w_status) |
| 5578 | #else |
| 5579 | int status; |
| 5580 | #define status_i status |
| 5581 | #endif |
| 5582 | status_i = 0; |
| 5583 | |
| 5584 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) |
| 5585 | { |
| 5586 | return NULL; |
| 5587 | } |
| 5588 | |
| 5589 | return PyBool_FromLong(WCOREDUMP(status)); |
| 5590 | #undef status_i |
| 5591 | } |
| 5592 | #endif /* WCOREDUMP */ |
| 5593 | |
| 5594 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5595 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5596 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5597 | 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] | 5598 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5599 | |
| 5600 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5601 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5602 | { |
| 5603 | #ifdef UNION_WAIT |
| 5604 | union wait status; |
| 5605 | #define status_i (status.w_status) |
| 5606 | #else |
| 5607 | int status; |
| 5608 | #define status_i status |
| 5609 | #endif |
| 5610 | status_i = 0; |
| 5611 | |
| 5612 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) |
| 5613 | { |
| 5614 | return NULL; |
| 5615 | } |
| 5616 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5617 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5618 | #undef status_i |
| 5619 | } |
| 5620 | #endif /* WIFCONTINUED */ |
| 5621 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5622 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5623 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5624 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5625 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5626 | |
| 5627 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5628 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5629 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5630 | #ifdef UNION_WAIT |
| 5631 | union wait status; |
| 5632 | #define status_i (status.w_status) |
| 5633 | #else |
| 5634 | int status; |
| 5635 | #define status_i status |
| 5636 | #endif |
| 5637 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5638 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5639 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5640 | { |
| 5641 | return NULL; |
| 5642 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5643 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5644 | return PyBool_FromLong(WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5645 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5646 | } |
| 5647 | #endif /* WIFSTOPPED */ |
| 5648 | |
| 5649 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5650 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5651 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5652 | 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] | 5653 | |
| 5654 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5655 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5656 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5657 | #ifdef UNION_WAIT |
| 5658 | union wait status; |
| 5659 | #define status_i (status.w_status) |
| 5660 | #else |
| 5661 | int status; |
| 5662 | #define status_i status |
| 5663 | #endif |
| 5664 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5665 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5666 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5667 | { |
| 5668 | return NULL; |
| 5669 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5670 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5671 | return PyBool_FromLong(WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5672 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5673 | } |
| 5674 | #endif /* WIFSIGNALED */ |
| 5675 | |
| 5676 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5677 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5678 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5679 | 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] | 5680 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5681 | |
| 5682 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5683 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5684 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5685 | #ifdef UNION_WAIT |
| 5686 | union wait status; |
| 5687 | #define status_i (status.w_status) |
| 5688 | #else |
| 5689 | int status; |
| 5690 | #define status_i status |
| 5691 | #endif |
| 5692 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5693 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5694 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5695 | { |
| 5696 | return NULL; |
| 5697 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5698 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5699 | return PyBool_FromLong(WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5700 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5701 | } |
| 5702 | #endif /* WIFEXITED */ |
| 5703 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5704 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5705 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5706 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5707 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5708 | |
| 5709 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5710 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5711 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5712 | #ifdef UNION_WAIT |
| 5713 | union wait status; |
| 5714 | #define status_i (status.w_status) |
| 5715 | #else |
| 5716 | int status; |
| 5717 | #define status_i status |
| 5718 | #endif |
| 5719 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5720 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5721 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5722 | { |
| 5723 | return NULL; |
| 5724 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5725 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5726 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5727 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5728 | } |
| 5729 | #endif /* WEXITSTATUS */ |
| 5730 | |
| 5731 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5732 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5733 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5734 | 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] | 5735 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5736 | |
| 5737 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5738 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5739 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5740 | #ifdef UNION_WAIT |
| 5741 | union wait status; |
| 5742 | #define status_i (status.w_status) |
| 5743 | #else |
| 5744 | int status; |
| 5745 | #define status_i status |
| 5746 | #endif |
| 5747 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5748 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5749 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5750 | { |
| 5751 | return NULL; |
| 5752 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5753 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5754 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5755 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5756 | } |
| 5757 | #endif /* WTERMSIG */ |
| 5758 | |
| 5759 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5760 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5761 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5762 | Return the signal that stopped the process that provided\n\ |
| 5763 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5764 | |
| 5765 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5766 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5767 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5768 | #ifdef UNION_WAIT |
| 5769 | union wait status; |
| 5770 | #define status_i (status.w_status) |
| 5771 | #else |
| 5772 | int status; |
| 5773 | #define status_i status |
| 5774 | #endif |
| 5775 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5776 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5777 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5778 | { |
| 5779 | return NULL; |
| 5780 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5781 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5782 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5783 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5784 | } |
| 5785 | #endif /* WSTOPSIG */ |
| 5786 | |
| 5787 | #endif /* HAVE_SYS_WAIT_H */ |
| 5788 | |
| 5789 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5790 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5791 | #ifdef _SCO_DS |
| 5792 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5793 | needed definitions in sys/statvfs.h */ |
| 5794 | #define _SVID3 |
| 5795 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5796 | #include <sys/statvfs.h> |
| 5797 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5798 | static PyObject* |
| 5799 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5800 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5801 | if (v == NULL) |
| 5802 | return NULL; |
| 5803 | |
| 5804 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5805 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5806 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5807 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5808 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5809 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5810 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5811 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5812 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5813 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5814 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5815 | #else |
| 5816 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5817 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5818 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5819 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5820 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5821 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5822 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5823 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5824 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5825 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5826 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5827 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5828 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5829 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5830 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5831 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5832 | #endif |
| 5833 | |
| 5834 | return v; |
| 5835 | } |
| 5836 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5837 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5838 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5839 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5840 | |
| 5841 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5842 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5843 | { |
| 5844 | int fd, res; |
| 5845 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5846 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5847 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5848 | return NULL; |
| 5849 | Py_BEGIN_ALLOW_THREADS |
| 5850 | res = fstatvfs(fd, &st); |
| 5851 | Py_END_ALLOW_THREADS |
| 5852 | if (res != 0) |
| 5853 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5854 | |
| 5855 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5856 | } |
| 5857 | #endif /* HAVE_FSTATVFS */ |
| 5858 | |
| 5859 | |
| 5860 | #if defined(HAVE_STATVFS) |
| 5861 | #include <sys/statvfs.h> |
| 5862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5863 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5864 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5865 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5866 | |
| 5867 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5868 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5869 | { |
| 5870 | char *path; |
| 5871 | int res; |
| 5872 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5873 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5874 | return NULL; |
| 5875 | Py_BEGIN_ALLOW_THREADS |
| 5876 | res = statvfs(path, &st); |
| 5877 | Py_END_ALLOW_THREADS |
| 5878 | if (res != 0) |
| 5879 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5880 | |
| 5881 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5882 | } |
| 5883 | #endif /* HAVE_STATVFS */ |
| 5884 | |
| 5885 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5886 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5887 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5888 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5889 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 5890 | The directory and a prefix may be specified as strings; they may be omitted\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5891 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5892 | |
| 5893 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5894 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5895 | { |
| 5896 | PyObject *result = NULL; |
| 5897 | char *dir = NULL; |
| 5898 | char *pfx = NULL; |
| 5899 | char *name; |
| 5900 | |
| 5901 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 5902 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5903 | |
| 5904 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5905 | "tempnam is a potential security risk to your program") < 0) |
| 5906 | return NULL; |
| 5907 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5908 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5909 | name = _tempnam(dir, pfx); |
| 5910 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5911 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5912 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5913 | if (name == NULL) |
| 5914 | return PyErr_NoMemory(); |
| 5915 | result = PyString_FromString(name); |
| 5916 | free(name); |
| 5917 | return result; |
| 5918 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 5919 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5920 | |
| 5921 | |
| 5922 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5923 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5924 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5925 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5926 | |
| 5927 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5928 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5929 | { |
| 5930 | FILE *fp; |
| 5931 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5932 | fp = tmpfile(); |
| 5933 | if (fp == NULL) |
| 5934 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 5935 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5936 | } |
| 5937 | #endif |
| 5938 | |
| 5939 | |
| 5940 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5941 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5942 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5943 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5944 | |
| 5945 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5946 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5947 | { |
| 5948 | char buffer[L_tmpnam]; |
| 5949 | char *name; |
| 5950 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5951 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5952 | "tmpnam is a potential security risk to your program") < 0) |
| 5953 | return NULL; |
| 5954 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5955 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5956 | name = tmpnam_r(buffer); |
| 5957 | #else |
| 5958 | name = tmpnam(buffer); |
| 5959 | #endif |
| 5960 | if (name == NULL) { |
| 5961 | PyErr_SetObject(PyExc_OSError, |
| 5962 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5963 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5964 | "unexpected NULL from tmpnam_r" |
| 5965 | #else |
| 5966 | "unexpected NULL from tmpnam" |
| 5967 | #endif |
| 5968 | )); |
| 5969 | return NULL; |
| 5970 | } |
| 5971 | return PyString_FromString(buffer); |
| 5972 | } |
| 5973 | #endif |
| 5974 | |
| 5975 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5976 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5977 | * It maps strings representing configuration variable names to |
| 5978 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5979 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5980 | * rarely-used constants. There are three separate tables that use |
| 5981 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5982 | * |
| 5983 | * This code is always included, even if none of the interfaces that |
| 5984 | * need it are included. The #if hackery needed to avoid it would be |
| 5985 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5986 | */ |
| 5987 | struct constdef { |
| 5988 | char *name; |
| 5989 | long value; |
| 5990 | }; |
| 5991 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5992 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5993 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 5994 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5995 | { |
| 5996 | if (PyInt_Check(arg)) { |
| 5997 | *valuep = PyInt_AS_LONG(arg); |
| 5998 | return 1; |
| 5999 | } |
| 6000 | if (PyString_Check(arg)) { |
| 6001 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6002 | size_t lo = 0; |
| 6003 | size_t mid; |
| 6004 | size_t hi = tablesize; |
| 6005 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6006 | char *confname = PyString_AS_STRING(arg); |
| 6007 | while (lo < hi) { |
| 6008 | mid = (lo + hi) / 2; |
| 6009 | cmp = strcmp(confname, table[mid].name); |
| 6010 | if (cmp < 0) |
| 6011 | hi = mid; |
| 6012 | else if (cmp > 0) |
| 6013 | lo = mid + 1; |
| 6014 | else { |
| 6015 | *valuep = table[mid].value; |
| 6016 | return 1; |
| 6017 | } |
| 6018 | } |
| 6019 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6020 | } |
| 6021 | else |
| 6022 | PyErr_SetString(PyExc_TypeError, |
| 6023 | "configuration names must be strings or integers"); |
| 6024 | return 0; |
| 6025 | } |
| 6026 | |
| 6027 | |
| 6028 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6029 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6030 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6031 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6032 | #endif |
| 6033 | #ifdef _PC_ABI_ASYNC_IO |
| 6034 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6035 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6036 | #ifdef _PC_ASYNC_IO |
| 6037 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6038 | #endif |
| 6039 | #ifdef _PC_CHOWN_RESTRICTED |
| 6040 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6041 | #endif |
| 6042 | #ifdef _PC_FILESIZEBITS |
| 6043 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6044 | #endif |
| 6045 | #ifdef _PC_LAST |
| 6046 | {"PC_LAST", _PC_LAST}, |
| 6047 | #endif |
| 6048 | #ifdef _PC_LINK_MAX |
| 6049 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6050 | #endif |
| 6051 | #ifdef _PC_MAX_CANON |
| 6052 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6053 | #endif |
| 6054 | #ifdef _PC_MAX_INPUT |
| 6055 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6056 | #endif |
| 6057 | #ifdef _PC_NAME_MAX |
| 6058 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6059 | #endif |
| 6060 | #ifdef _PC_NO_TRUNC |
| 6061 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6062 | #endif |
| 6063 | #ifdef _PC_PATH_MAX |
| 6064 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6065 | #endif |
| 6066 | #ifdef _PC_PIPE_BUF |
| 6067 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6068 | #endif |
| 6069 | #ifdef _PC_PRIO_IO |
| 6070 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6071 | #endif |
| 6072 | #ifdef _PC_SOCK_MAXBUF |
| 6073 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6074 | #endif |
| 6075 | #ifdef _PC_SYNC_IO |
| 6076 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6077 | #endif |
| 6078 | #ifdef _PC_VDISABLE |
| 6079 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6080 | #endif |
| 6081 | }; |
| 6082 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6083 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6084 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6085 | { |
| 6086 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6087 | sizeof(posix_constants_pathconf) |
| 6088 | / sizeof(struct constdef)); |
| 6089 | } |
| 6090 | #endif |
| 6091 | |
| 6092 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6093 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6094 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6095 | 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] | 6096 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6097 | |
| 6098 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6099 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6100 | { |
| 6101 | PyObject *result = NULL; |
| 6102 | int name, fd; |
| 6103 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6104 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6105 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6106 | long limit; |
| 6107 | |
| 6108 | errno = 0; |
| 6109 | limit = fpathconf(fd, name); |
| 6110 | if (limit == -1 && errno != 0) |
| 6111 | posix_error(); |
| 6112 | else |
| 6113 | result = PyInt_FromLong(limit); |
| 6114 | } |
| 6115 | return result; |
| 6116 | } |
| 6117 | #endif |
| 6118 | |
| 6119 | |
| 6120 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6121 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6122 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6123 | 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] | 6124 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6125 | |
| 6126 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6127 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6128 | { |
| 6129 | PyObject *result = NULL; |
| 6130 | int name; |
| 6131 | char *path; |
| 6132 | |
| 6133 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6134 | conv_path_confname, &name)) { |
| 6135 | long limit; |
| 6136 | |
| 6137 | errno = 0; |
| 6138 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6139 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6140 | if (errno == EINVAL) |
| 6141 | /* could be a path or name problem */ |
| 6142 | posix_error(); |
| 6143 | else |
| 6144 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6145 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6146 | else |
| 6147 | result = PyInt_FromLong(limit); |
| 6148 | } |
| 6149 | return result; |
| 6150 | } |
| 6151 | #endif |
| 6152 | |
| 6153 | #ifdef HAVE_CONFSTR |
| 6154 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6155 | #ifdef _CS_ARCHITECTURE |
| 6156 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6157 | #endif |
| 6158 | #ifdef _CS_HOSTNAME |
| 6159 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6160 | #endif |
| 6161 | #ifdef _CS_HW_PROVIDER |
| 6162 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6163 | #endif |
| 6164 | #ifdef _CS_HW_SERIAL |
| 6165 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6166 | #endif |
| 6167 | #ifdef _CS_INITTAB_NAME |
| 6168 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6169 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6170 | #ifdef _CS_LFS64_CFLAGS |
| 6171 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6172 | #endif |
| 6173 | #ifdef _CS_LFS64_LDFLAGS |
| 6174 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6175 | #endif |
| 6176 | #ifdef _CS_LFS64_LIBS |
| 6177 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6178 | #endif |
| 6179 | #ifdef _CS_LFS64_LINTFLAGS |
| 6180 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6181 | #endif |
| 6182 | #ifdef _CS_LFS_CFLAGS |
| 6183 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6184 | #endif |
| 6185 | #ifdef _CS_LFS_LDFLAGS |
| 6186 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6187 | #endif |
| 6188 | #ifdef _CS_LFS_LIBS |
| 6189 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6190 | #endif |
| 6191 | #ifdef _CS_LFS_LINTFLAGS |
| 6192 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6193 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6194 | #ifdef _CS_MACHINE |
| 6195 | {"CS_MACHINE", _CS_MACHINE}, |
| 6196 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6197 | #ifdef _CS_PATH |
| 6198 | {"CS_PATH", _CS_PATH}, |
| 6199 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6200 | #ifdef _CS_RELEASE |
| 6201 | {"CS_RELEASE", _CS_RELEASE}, |
| 6202 | #endif |
| 6203 | #ifdef _CS_SRPC_DOMAIN |
| 6204 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6205 | #endif |
| 6206 | #ifdef _CS_SYSNAME |
| 6207 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6208 | #endif |
| 6209 | #ifdef _CS_VERSION |
| 6210 | {"CS_VERSION", _CS_VERSION}, |
| 6211 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6212 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6213 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6214 | #endif |
| 6215 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6216 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6217 | #endif |
| 6218 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6219 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6220 | #endif |
| 6221 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6222 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6223 | #endif |
| 6224 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6225 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6226 | #endif |
| 6227 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6228 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6229 | #endif |
| 6230 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6231 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6232 | #endif |
| 6233 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6234 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6235 | #endif |
| 6236 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6237 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6238 | #endif |
| 6239 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6240 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6241 | #endif |
| 6242 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6243 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6244 | #endif |
| 6245 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6246 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6247 | #endif |
| 6248 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6249 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6250 | #endif |
| 6251 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6252 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6253 | #endif |
| 6254 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6255 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6256 | #endif |
| 6257 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6258 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6259 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6260 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6261 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6262 | #endif |
| 6263 | #ifdef _MIPS_CS_BASE |
| 6264 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6265 | #endif |
| 6266 | #ifdef _MIPS_CS_HOSTID |
| 6267 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6268 | #endif |
| 6269 | #ifdef _MIPS_CS_HW_NAME |
| 6270 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6271 | #endif |
| 6272 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6273 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6274 | #endif |
| 6275 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6276 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6277 | #endif |
| 6278 | #ifdef _MIPS_CS_OSREL_MIN |
| 6279 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6280 | #endif |
| 6281 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6282 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6283 | #endif |
| 6284 | #ifdef _MIPS_CS_OS_NAME |
| 6285 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6286 | #endif |
| 6287 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6288 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6289 | #endif |
| 6290 | #ifdef _MIPS_CS_PROCESSORS |
| 6291 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6292 | #endif |
| 6293 | #ifdef _MIPS_CS_SERIAL |
| 6294 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6295 | #endif |
| 6296 | #ifdef _MIPS_CS_VENDOR |
| 6297 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6298 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6299 | }; |
| 6300 | |
| 6301 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6302 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6303 | { |
| 6304 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6305 | sizeof(posix_constants_confstr) |
| 6306 | / sizeof(struct constdef)); |
| 6307 | } |
| 6308 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6309 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6310 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6311 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6312 | |
| 6313 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6314 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6315 | { |
| 6316 | PyObject *result = NULL; |
| 6317 | int name; |
| 6318 | char buffer[64]; |
| 6319 | |
| 6320 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6321 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6322 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6323 | errno = 0; |
| 6324 | if (len == 0) { |
| 6325 | if (errno != 0) |
| 6326 | posix_error(); |
| 6327 | else |
| 6328 | result = PyString_FromString(""); |
| 6329 | } |
| 6330 | else { |
| 6331 | if (len >= sizeof(buffer)) { |
| 6332 | result = PyString_FromStringAndSize(NULL, len); |
| 6333 | if (result != NULL) |
| 6334 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6335 | } |
| 6336 | else |
| 6337 | result = PyString_FromString(buffer); |
| 6338 | } |
| 6339 | } |
| 6340 | return result; |
| 6341 | } |
| 6342 | #endif |
| 6343 | |
| 6344 | |
| 6345 | #ifdef HAVE_SYSCONF |
| 6346 | static struct constdef posix_constants_sysconf[] = { |
| 6347 | #ifdef _SC_2_CHAR_TERM |
| 6348 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6349 | #endif |
| 6350 | #ifdef _SC_2_C_BIND |
| 6351 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6352 | #endif |
| 6353 | #ifdef _SC_2_C_DEV |
| 6354 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6355 | #endif |
| 6356 | #ifdef _SC_2_C_VERSION |
| 6357 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6358 | #endif |
| 6359 | #ifdef _SC_2_FORT_DEV |
| 6360 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6361 | #endif |
| 6362 | #ifdef _SC_2_FORT_RUN |
| 6363 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6364 | #endif |
| 6365 | #ifdef _SC_2_LOCALEDEF |
| 6366 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6367 | #endif |
| 6368 | #ifdef _SC_2_SW_DEV |
| 6369 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6370 | #endif |
| 6371 | #ifdef _SC_2_UPE |
| 6372 | {"SC_2_UPE", _SC_2_UPE}, |
| 6373 | #endif |
| 6374 | #ifdef _SC_2_VERSION |
| 6375 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6376 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6377 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6378 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6379 | #endif |
| 6380 | #ifdef _SC_ACL |
| 6381 | {"SC_ACL", _SC_ACL}, |
| 6382 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6383 | #ifdef _SC_AIO_LISTIO_MAX |
| 6384 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6385 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6386 | #ifdef _SC_AIO_MAX |
| 6387 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6388 | #endif |
| 6389 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6390 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6391 | #endif |
| 6392 | #ifdef _SC_ARG_MAX |
| 6393 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6394 | #endif |
| 6395 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6396 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6397 | #endif |
| 6398 | #ifdef _SC_ATEXIT_MAX |
| 6399 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6400 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6401 | #ifdef _SC_AUDIT |
| 6402 | {"SC_AUDIT", _SC_AUDIT}, |
| 6403 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6404 | #ifdef _SC_AVPHYS_PAGES |
| 6405 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6406 | #endif |
| 6407 | #ifdef _SC_BC_BASE_MAX |
| 6408 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6409 | #endif |
| 6410 | #ifdef _SC_BC_DIM_MAX |
| 6411 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6412 | #endif |
| 6413 | #ifdef _SC_BC_SCALE_MAX |
| 6414 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6415 | #endif |
| 6416 | #ifdef _SC_BC_STRING_MAX |
| 6417 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6418 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6419 | #ifdef _SC_CAP |
| 6420 | {"SC_CAP", _SC_CAP}, |
| 6421 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6422 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6423 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6424 | #endif |
| 6425 | #ifdef _SC_CHAR_BIT |
| 6426 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6427 | #endif |
| 6428 | #ifdef _SC_CHAR_MAX |
| 6429 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6430 | #endif |
| 6431 | #ifdef _SC_CHAR_MIN |
| 6432 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6433 | #endif |
| 6434 | #ifdef _SC_CHILD_MAX |
| 6435 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6436 | #endif |
| 6437 | #ifdef _SC_CLK_TCK |
| 6438 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6439 | #endif |
| 6440 | #ifdef _SC_COHER_BLKSZ |
| 6441 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6442 | #endif |
| 6443 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6444 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6445 | #endif |
| 6446 | #ifdef _SC_DCACHE_ASSOC |
| 6447 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6448 | #endif |
| 6449 | #ifdef _SC_DCACHE_BLKSZ |
| 6450 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6451 | #endif |
| 6452 | #ifdef _SC_DCACHE_LINESZ |
| 6453 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6454 | #endif |
| 6455 | #ifdef _SC_DCACHE_SZ |
| 6456 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6457 | #endif |
| 6458 | #ifdef _SC_DCACHE_TBLKSZ |
| 6459 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6460 | #endif |
| 6461 | #ifdef _SC_DELAYTIMER_MAX |
| 6462 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6463 | #endif |
| 6464 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6465 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6466 | #endif |
| 6467 | #ifdef _SC_EXPR_NEST_MAX |
| 6468 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6469 | #endif |
| 6470 | #ifdef _SC_FSYNC |
| 6471 | {"SC_FSYNC", _SC_FSYNC}, |
| 6472 | #endif |
| 6473 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6474 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6475 | #endif |
| 6476 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6477 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6478 | #endif |
| 6479 | #ifdef _SC_ICACHE_ASSOC |
| 6480 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6481 | #endif |
| 6482 | #ifdef _SC_ICACHE_BLKSZ |
| 6483 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6484 | #endif |
| 6485 | #ifdef _SC_ICACHE_LINESZ |
| 6486 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6487 | #endif |
| 6488 | #ifdef _SC_ICACHE_SZ |
| 6489 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6490 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6491 | #ifdef _SC_INF |
| 6492 | {"SC_INF", _SC_INF}, |
| 6493 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6494 | #ifdef _SC_INT_MAX |
| 6495 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6496 | #endif |
| 6497 | #ifdef _SC_INT_MIN |
| 6498 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6499 | #endif |
| 6500 | #ifdef _SC_IOV_MAX |
| 6501 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6502 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6503 | #ifdef _SC_IP_SECOPTS |
| 6504 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6505 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6506 | #ifdef _SC_JOB_CONTROL |
| 6507 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6508 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6509 | #ifdef _SC_KERN_POINTERS |
| 6510 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6511 | #endif |
| 6512 | #ifdef _SC_KERN_SIM |
| 6513 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6514 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6515 | #ifdef _SC_LINE_MAX |
| 6516 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6517 | #endif |
| 6518 | #ifdef _SC_LOGIN_NAME_MAX |
| 6519 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6520 | #endif |
| 6521 | #ifdef _SC_LOGNAME_MAX |
| 6522 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6523 | #endif |
| 6524 | #ifdef _SC_LONG_BIT |
| 6525 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6526 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6527 | #ifdef _SC_MAC |
| 6528 | {"SC_MAC", _SC_MAC}, |
| 6529 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6530 | #ifdef _SC_MAPPED_FILES |
| 6531 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6532 | #endif |
| 6533 | #ifdef _SC_MAXPID |
| 6534 | {"SC_MAXPID", _SC_MAXPID}, |
| 6535 | #endif |
| 6536 | #ifdef _SC_MB_LEN_MAX |
| 6537 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6538 | #endif |
| 6539 | #ifdef _SC_MEMLOCK |
| 6540 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6541 | #endif |
| 6542 | #ifdef _SC_MEMLOCK_RANGE |
| 6543 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6544 | #endif |
| 6545 | #ifdef _SC_MEMORY_PROTECTION |
| 6546 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6547 | #endif |
| 6548 | #ifdef _SC_MESSAGE_PASSING |
| 6549 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6550 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6551 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6552 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6553 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6554 | #ifdef _SC_MQ_OPEN_MAX |
| 6555 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6556 | #endif |
| 6557 | #ifdef _SC_MQ_PRIO_MAX |
| 6558 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6559 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6560 | #ifdef _SC_NACLS_MAX |
| 6561 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6562 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6563 | #ifdef _SC_NGROUPS_MAX |
| 6564 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6565 | #endif |
| 6566 | #ifdef _SC_NL_ARGMAX |
| 6567 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6568 | #endif |
| 6569 | #ifdef _SC_NL_LANGMAX |
| 6570 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6571 | #endif |
| 6572 | #ifdef _SC_NL_MSGMAX |
| 6573 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6574 | #endif |
| 6575 | #ifdef _SC_NL_NMAX |
| 6576 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6577 | #endif |
| 6578 | #ifdef _SC_NL_SETMAX |
| 6579 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6580 | #endif |
| 6581 | #ifdef _SC_NL_TEXTMAX |
| 6582 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6583 | #endif |
| 6584 | #ifdef _SC_NPROCESSORS_CONF |
| 6585 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6586 | #endif |
| 6587 | #ifdef _SC_NPROCESSORS_ONLN |
| 6588 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6589 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6590 | #ifdef _SC_NPROC_CONF |
| 6591 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6592 | #endif |
| 6593 | #ifdef _SC_NPROC_ONLN |
| 6594 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6595 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6596 | #ifdef _SC_NZERO |
| 6597 | {"SC_NZERO", _SC_NZERO}, |
| 6598 | #endif |
| 6599 | #ifdef _SC_OPEN_MAX |
| 6600 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6601 | #endif |
| 6602 | #ifdef _SC_PAGESIZE |
| 6603 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6604 | #endif |
| 6605 | #ifdef _SC_PAGE_SIZE |
| 6606 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6607 | #endif |
| 6608 | #ifdef _SC_PASS_MAX |
| 6609 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6610 | #endif |
| 6611 | #ifdef _SC_PHYS_PAGES |
| 6612 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6613 | #endif |
| 6614 | #ifdef _SC_PII |
| 6615 | {"SC_PII", _SC_PII}, |
| 6616 | #endif |
| 6617 | #ifdef _SC_PII_INTERNET |
| 6618 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6619 | #endif |
| 6620 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6621 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6622 | #endif |
| 6623 | #ifdef _SC_PII_INTERNET_STREAM |
| 6624 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6625 | #endif |
| 6626 | #ifdef _SC_PII_OSI |
| 6627 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6628 | #endif |
| 6629 | #ifdef _SC_PII_OSI_CLTS |
| 6630 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6631 | #endif |
| 6632 | #ifdef _SC_PII_OSI_COTS |
| 6633 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6634 | #endif |
| 6635 | #ifdef _SC_PII_OSI_M |
| 6636 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6637 | #endif |
| 6638 | #ifdef _SC_PII_SOCKET |
| 6639 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6640 | #endif |
| 6641 | #ifdef _SC_PII_XTI |
| 6642 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6643 | #endif |
| 6644 | #ifdef _SC_POLL |
| 6645 | {"SC_POLL", _SC_POLL}, |
| 6646 | #endif |
| 6647 | #ifdef _SC_PRIORITIZED_IO |
| 6648 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6649 | #endif |
| 6650 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6651 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6652 | #endif |
| 6653 | #ifdef _SC_REALTIME_SIGNALS |
| 6654 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6655 | #endif |
| 6656 | #ifdef _SC_RE_DUP_MAX |
| 6657 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6658 | #endif |
| 6659 | #ifdef _SC_RTSIG_MAX |
| 6660 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6661 | #endif |
| 6662 | #ifdef _SC_SAVED_IDS |
| 6663 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6664 | #endif |
| 6665 | #ifdef _SC_SCHAR_MAX |
| 6666 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6667 | #endif |
| 6668 | #ifdef _SC_SCHAR_MIN |
| 6669 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6670 | #endif |
| 6671 | #ifdef _SC_SELECT |
| 6672 | {"SC_SELECT", _SC_SELECT}, |
| 6673 | #endif |
| 6674 | #ifdef _SC_SEMAPHORES |
| 6675 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6676 | #endif |
| 6677 | #ifdef _SC_SEM_NSEMS_MAX |
| 6678 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6679 | #endif |
| 6680 | #ifdef _SC_SEM_VALUE_MAX |
| 6681 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6682 | #endif |
| 6683 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6684 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6685 | #endif |
| 6686 | #ifdef _SC_SHRT_MAX |
| 6687 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6688 | #endif |
| 6689 | #ifdef _SC_SHRT_MIN |
| 6690 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6691 | #endif |
| 6692 | #ifdef _SC_SIGQUEUE_MAX |
| 6693 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6694 | #endif |
| 6695 | #ifdef _SC_SIGRT_MAX |
| 6696 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6697 | #endif |
| 6698 | #ifdef _SC_SIGRT_MIN |
| 6699 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6700 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6701 | #ifdef _SC_SOFTPOWER |
| 6702 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6703 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6704 | #ifdef _SC_SPLIT_CACHE |
| 6705 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6706 | #endif |
| 6707 | #ifdef _SC_SSIZE_MAX |
| 6708 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6709 | #endif |
| 6710 | #ifdef _SC_STACK_PROT |
| 6711 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6712 | #endif |
| 6713 | #ifdef _SC_STREAM_MAX |
| 6714 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6715 | #endif |
| 6716 | #ifdef _SC_SYNCHRONIZED_IO |
| 6717 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6718 | #endif |
| 6719 | #ifdef _SC_THREADS |
| 6720 | {"SC_THREADS", _SC_THREADS}, |
| 6721 | #endif |
| 6722 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6723 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6724 | #endif |
| 6725 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6726 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6727 | #endif |
| 6728 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6729 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6730 | #endif |
| 6731 | #ifdef _SC_THREAD_KEYS_MAX |
| 6732 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6733 | #endif |
| 6734 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6735 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6736 | #endif |
| 6737 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6738 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6739 | #endif |
| 6740 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6741 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6742 | #endif |
| 6743 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6744 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6745 | #endif |
| 6746 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6747 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6748 | #endif |
| 6749 | #ifdef _SC_THREAD_STACK_MIN |
| 6750 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6751 | #endif |
| 6752 | #ifdef _SC_THREAD_THREADS_MAX |
| 6753 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6754 | #endif |
| 6755 | #ifdef _SC_TIMERS |
| 6756 | {"SC_TIMERS", _SC_TIMERS}, |
| 6757 | #endif |
| 6758 | #ifdef _SC_TIMER_MAX |
| 6759 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6760 | #endif |
| 6761 | #ifdef _SC_TTY_NAME_MAX |
| 6762 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6763 | #endif |
| 6764 | #ifdef _SC_TZNAME_MAX |
| 6765 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6766 | #endif |
| 6767 | #ifdef _SC_T_IOV_MAX |
| 6768 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6769 | #endif |
| 6770 | #ifdef _SC_UCHAR_MAX |
| 6771 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6772 | #endif |
| 6773 | #ifdef _SC_UINT_MAX |
| 6774 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6775 | #endif |
| 6776 | #ifdef _SC_UIO_MAXIOV |
| 6777 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6778 | #endif |
| 6779 | #ifdef _SC_ULONG_MAX |
| 6780 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6781 | #endif |
| 6782 | #ifdef _SC_USHRT_MAX |
| 6783 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6784 | #endif |
| 6785 | #ifdef _SC_VERSION |
| 6786 | {"SC_VERSION", _SC_VERSION}, |
| 6787 | #endif |
| 6788 | #ifdef _SC_WORD_BIT |
| 6789 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6790 | #endif |
| 6791 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6792 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6793 | #endif |
| 6794 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6795 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6796 | #endif |
| 6797 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6798 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6799 | #endif |
| 6800 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6801 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6802 | #endif |
| 6803 | #ifdef _SC_XOPEN_CRYPT |
| 6804 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6805 | #endif |
| 6806 | #ifdef _SC_XOPEN_ENH_I18N |
| 6807 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6808 | #endif |
| 6809 | #ifdef _SC_XOPEN_LEGACY |
| 6810 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6811 | #endif |
| 6812 | #ifdef _SC_XOPEN_REALTIME |
| 6813 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6814 | #endif |
| 6815 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6816 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6817 | #endif |
| 6818 | #ifdef _SC_XOPEN_SHM |
| 6819 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6820 | #endif |
| 6821 | #ifdef _SC_XOPEN_UNIX |
| 6822 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6823 | #endif |
| 6824 | #ifdef _SC_XOPEN_VERSION |
| 6825 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6826 | #endif |
| 6827 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6828 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6829 | #endif |
| 6830 | #ifdef _SC_XOPEN_XPG2 |
| 6831 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6832 | #endif |
| 6833 | #ifdef _SC_XOPEN_XPG3 |
| 6834 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6835 | #endif |
| 6836 | #ifdef _SC_XOPEN_XPG4 |
| 6837 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6838 | #endif |
| 6839 | }; |
| 6840 | |
| 6841 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6842 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6843 | { |
| 6844 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6845 | sizeof(posix_constants_sysconf) |
| 6846 | / sizeof(struct constdef)); |
| 6847 | } |
| 6848 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6849 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6850 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6851 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6852 | |
| 6853 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6854 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6855 | { |
| 6856 | PyObject *result = NULL; |
| 6857 | int name; |
| 6858 | |
| 6859 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6860 | int value; |
| 6861 | |
| 6862 | errno = 0; |
| 6863 | value = sysconf(name); |
| 6864 | if (value == -1 && errno != 0) |
| 6865 | posix_error(); |
| 6866 | else |
| 6867 | result = PyInt_FromLong(value); |
| 6868 | } |
| 6869 | return result; |
| 6870 | } |
| 6871 | #endif |
| 6872 | |
| 6873 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6874 | /* This code is used to ensure that the tables of configuration value names |
| 6875 | * are in sorted order as required by conv_confname(), and also to build the |
| 6876 | * the exported dictionaries that are used to publish information about the |
| 6877 | * names available on the host platform. |
| 6878 | * |
| 6879 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6880 | * when used, even for platforms we're not able to test on. It also makes |
| 6881 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6882 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6883 | |
| 6884 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6885 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6886 | { |
| 6887 | const struct constdef *c1 = |
| 6888 | (const struct constdef *) v1; |
| 6889 | const struct constdef *c2 = |
| 6890 | (const struct constdef *) v2; |
| 6891 | |
| 6892 | return strcmp(c1->name, c2->name); |
| 6893 | } |
| 6894 | |
| 6895 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6896 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6897 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6898 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6899 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6900 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6901 | |
| 6902 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6903 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6904 | if (d == NULL) |
| 6905 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6906 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6907 | for (i=0; i < tablesize; ++i) { |
| 6908 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6909 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6910 | Py_XDECREF(o); |
| 6911 | Py_DECREF(d); |
| 6912 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6913 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6914 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6915 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6916 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6917 | } |
| 6918 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6919 | /* Return -1 on failure, 0 on success. */ |
| 6920 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6921 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6922 | { |
| 6923 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6924 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6925 | sizeof(posix_constants_pathconf) |
| 6926 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6927 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6928 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6929 | #endif |
| 6930 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6931 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6932 | sizeof(posix_constants_confstr) |
| 6933 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6934 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6935 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6936 | #endif |
| 6937 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6938 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6939 | sizeof(posix_constants_sysconf) |
| 6940 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6941 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6942 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6943 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6944 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6945 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6946 | |
| 6947 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6948 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6949 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6950 | 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] | 6951 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6952 | |
| 6953 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6954 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6955 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6956 | abort(); |
| 6957 | /*NOTREACHED*/ |
| 6958 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6959 | return NULL; |
| 6960 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6961 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6962 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6963 | PyDoc_STRVAR(win32_startfile__doc__, |
| 6964 | "startfile(filepath) - Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6965 | \n\ |
| 6966 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 6967 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 6968 | with whatever application (if any) its extension is associated.\n\ |
| 6969 | \n\ |
| 6970 | startfile returns as soon as the associated application is launched.\n\ |
| 6971 | There is no option to wait for the application to close, and no way\n\ |
| 6972 | to retrieve the application's exit status.\n\ |
| 6973 | \n\ |
| 6974 | The filepath is relative to the current directory. If you want to use\n\ |
| 6975 | 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] | 6976 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6977 | |
| 6978 | static PyObject * |
| 6979 | win32_startfile(PyObject *self, PyObject *args) |
| 6980 | { |
| 6981 | char *filepath; |
| 6982 | HINSTANCE rc; |
| 6983 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 6984 | return NULL; |
| 6985 | Py_BEGIN_ALLOW_THREADS |
| 6986 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 6987 | Py_END_ALLOW_THREADS |
| 6988 | if (rc <= (HINSTANCE)32) |
| 6989 | return win32_error("startfile", filepath); |
| 6990 | Py_INCREF(Py_None); |
| 6991 | return Py_None; |
| 6992 | } |
| 6993 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6994 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6995 | #ifdef HAVE_GETLOADAVG |
| 6996 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6997 | "getloadavg() -> (float, float, float)\n\n\ |
| 6998 | Return the number of processes in the system run queue averaged over\n\ |
| 6999 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7000 | was unobtainable"); |
| 7001 | |
| 7002 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7003 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7004 | { |
| 7005 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7006 | if (getloadavg(loadavg, 3)!=3) { |
| 7007 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7008 | return NULL; |
| 7009 | } else |
| 7010 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7011 | } |
| 7012 | #endif |
| 7013 | |
| 7014 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7015 | static PyMethodDef posix_methods[] = { |
| 7016 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7017 | #ifdef HAVE_TTYNAME |
| 7018 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7019 | #endif |
| 7020 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7021 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7022 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7023 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7024 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7025 | #ifdef HAVE_LCHOWN |
| 7026 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7027 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7028 | #ifdef HAVE_CHROOT |
| 7029 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7030 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7031 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7032 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7033 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7034 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7035 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7036 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7037 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7038 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7039 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7040 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7041 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7042 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7043 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7044 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7045 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7046 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7047 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7048 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7049 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7050 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7051 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7052 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7053 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7054 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7055 | {"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] | 7056 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7057 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7058 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7059 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7060 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7061 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7062 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7063 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7064 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7065 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7066 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7067 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7068 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7069 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7070 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7071 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7072 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7073 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7074 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7075 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7076 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7077 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7078 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7079 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7080 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7081 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7082 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7083 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7084 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7085 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7086 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7087 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7088 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7089 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7090 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7091 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7092 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7093 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7094 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7095 | #endif /* HAVE_GETEGID */ |
| 7096 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7097 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7098 | #endif /* HAVE_GETEUID */ |
| 7099 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7100 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7101 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7102 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7103 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7104 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7105 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7106 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7107 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7108 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7109 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7110 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7111 | #endif /* HAVE_GETPPID */ |
| 7112 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7113 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7114 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7115 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7116 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7117 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7118 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7119 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7120 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7121 | #ifdef HAVE_KILLPG |
| 7122 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7123 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7124 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7125 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7126 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7127 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7128 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7129 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7130 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7131 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7132 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7133 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7134 | #else |
| 7135 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7136 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7137 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7138 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7139 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7140 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7141 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7142 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7143 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7144 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7145 | #ifdef HAVE_SETEUID |
| 7146 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7147 | #endif /* HAVE_SETEUID */ |
| 7148 | #ifdef HAVE_SETEGID |
| 7149 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7150 | #endif /* HAVE_SETEGID */ |
| 7151 | #ifdef HAVE_SETREUID |
| 7152 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7153 | #endif /* HAVE_SETREUID */ |
| 7154 | #ifdef HAVE_SETREGID |
| 7155 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7156 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7157 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7158 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7159 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7160 | #ifdef HAVE_SETGROUPS |
| 7161 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7162 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7163 | #ifdef HAVE_GETPGID |
| 7164 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7165 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7166 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7167 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7168 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7169 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7170 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7171 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7172 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7173 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7174 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7175 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7176 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7177 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7178 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7179 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7180 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7181 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7182 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7183 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7184 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7185 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7186 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7187 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7188 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7189 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7190 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7191 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7192 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7193 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7194 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7195 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7196 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7197 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7198 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7199 | #endif |
| 7200 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7201 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7202 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7203 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7204 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7205 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7206 | #ifdef HAVE_DEVICE_MACROS |
| 7207 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7208 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7209 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7210 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7211 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7212 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7213 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7214 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7215 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7216 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7217 | #ifdef HAVE_UNSETENV |
| 7218 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7219 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7220 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7221 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7222 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7223 | #ifdef HAVE_FCHDIR |
| 7224 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7225 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7226 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7227 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7228 | #endif |
| 7229 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7230 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7231 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7232 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7233 | #ifdef WCOREDUMP |
| 7234 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7235 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7236 | #ifdef WIFCONTINUED |
| 7237 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7238 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7239 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7240 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7241 | #endif /* WIFSTOPPED */ |
| 7242 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7243 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7244 | #endif /* WIFSIGNALED */ |
| 7245 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7246 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7247 | #endif /* WIFEXITED */ |
| 7248 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7249 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7250 | #endif /* WEXITSTATUS */ |
| 7251 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7252 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7253 | #endif /* WTERMSIG */ |
| 7254 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7255 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7256 | #endif /* WSTOPSIG */ |
| 7257 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7258 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7259 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7260 | #endif |
| 7261 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7262 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7263 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7264 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7265 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7266 | #endif |
| 7267 | #ifdef HAVE_TEMPNAM |
| 7268 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7269 | #endif |
| 7270 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7271 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7272 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7273 | #ifdef HAVE_CONFSTR |
| 7274 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7275 | #endif |
| 7276 | #ifdef HAVE_SYSCONF |
| 7277 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7278 | #endif |
| 7279 | #ifdef HAVE_FPATHCONF |
| 7280 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7281 | #endif |
| 7282 | #ifdef HAVE_PATHCONF |
| 7283 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7284 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7285 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7286 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7287 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7288 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7289 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7290 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7291 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7292 | {NULL, NULL} /* Sentinel */ |
| 7293 | }; |
| 7294 | |
| 7295 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7296 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7297 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7298 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7299 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7300 | } |
| 7301 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7302 | #if defined(PYOS_OS2) |
| 7303 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7304 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7305 | { |
| 7306 | APIRET rc; |
| 7307 | ULONG values[QSV_MAX+1]; |
| 7308 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7309 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7310 | |
| 7311 | Py_BEGIN_ALLOW_THREADS |
| 7312 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 7313 | Py_END_ALLOW_THREADS |
| 7314 | |
| 7315 | if (rc != NO_ERROR) { |
| 7316 | os2_error(rc); |
| 7317 | return -1; |
| 7318 | } |
| 7319 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7320 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7321 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7322 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7323 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7324 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7325 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7326 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7327 | |
| 7328 | switch (values[QSV_VERSION_MINOR]) { |
| 7329 | case 0: ver = "2.00"; break; |
| 7330 | case 10: ver = "2.10"; break; |
| 7331 | case 11: ver = "2.11"; break; |
| 7332 | case 30: ver = "3.00"; break; |
| 7333 | case 40: ver = "4.00"; break; |
| 7334 | case 50: ver = "5.00"; break; |
| 7335 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7336 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7337 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7338 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7339 | ver = &tmp[0]; |
| 7340 | } |
| 7341 | |
| 7342 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7343 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7344 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7345 | |
| 7346 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7347 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7348 | tmp[1] = ':'; |
| 7349 | tmp[2] = '\0'; |
| 7350 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7351 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7352 | } |
| 7353 | #endif |
| 7354 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7355 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7356 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7357 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7358 | #ifdef F_OK |
| 7359 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7360 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7361 | #ifdef R_OK |
| 7362 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7363 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7364 | #ifdef W_OK |
| 7365 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7366 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7367 | #ifdef X_OK |
| 7368 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7369 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7370 | #ifdef NGROUPS_MAX |
| 7371 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7372 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7373 | #ifdef TMP_MAX |
| 7374 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7375 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7376 | #ifdef WCONTINUED |
| 7377 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7378 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7379 | #ifdef WNOHANG |
| 7380 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7381 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7382 | #ifdef WUNTRACED |
| 7383 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7384 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7385 | #ifdef O_RDONLY |
| 7386 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7387 | #endif |
| 7388 | #ifdef O_WRONLY |
| 7389 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7390 | #endif |
| 7391 | #ifdef O_RDWR |
| 7392 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7393 | #endif |
| 7394 | #ifdef O_NDELAY |
| 7395 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7396 | #endif |
| 7397 | #ifdef O_NONBLOCK |
| 7398 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7399 | #endif |
| 7400 | #ifdef O_APPEND |
| 7401 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7402 | #endif |
| 7403 | #ifdef O_DSYNC |
| 7404 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7405 | #endif |
| 7406 | #ifdef O_RSYNC |
| 7407 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7408 | #endif |
| 7409 | #ifdef O_SYNC |
| 7410 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7411 | #endif |
| 7412 | #ifdef O_NOCTTY |
| 7413 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7414 | #endif |
| 7415 | #ifdef O_CREAT |
| 7416 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7417 | #endif |
| 7418 | #ifdef O_EXCL |
| 7419 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7420 | #endif |
| 7421 | #ifdef O_TRUNC |
| 7422 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7423 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7424 | #ifdef O_BINARY |
| 7425 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7426 | #endif |
| 7427 | #ifdef O_TEXT |
| 7428 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7429 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7430 | #ifdef O_LARGEFILE |
| 7431 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7432 | #endif |
| 7433 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7434 | /* MS Windows */ |
| 7435 | #ifdef O_NOINHERIT |
| 7436 | /* Don't inherit in child processes. */ |
| 7437 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7438 | #endif |
| 7439 | #ifdef _O_SHORT_LIVED |
| 7440 | /* Optimize for short life (keep in memory). */ |
| 7441 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7442 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7443 | #endif |
| 7444 | #ifdef O_TEMPORARY |
| 7445 | /* Automatically delete when last handle is closed. */ |
| 7446 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7447 | #endif |
| 7448 | #ifdef O_RANDOM |
| 7449 | /* Optimize for random access. */ |
| 7450 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7451 | #endif |
| 7452 | #ifdef O_SEQUENTIAL |
| 7453 | /* Optimize for sequential access. */ |
| 7454 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7455 | #endif |
| 7456 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7457 | /* GNU extensions. */ |
| 7458 | #ifdef O_DIRECT |
| 7459 | /* Direct disk access. */ |
| 7460 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7461 | #endif |
| 7462 | #ifdef O_DIRECTORY |
| 7463 | /* Must be a directory. */ |
| 7464 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7465 | #endif |
| 7466 | #ifdef O_NOFOLLOW |
| 7467 | /* Do not follow links. */ |
| 7468 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7469 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7470 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7471 | /* These come from sysexits.h */ |
| 7472 | #ifdef EX_OK |
| 7473 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7474 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7475 | #ifdef EX_USAGE |
| 7476 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7477 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7478 | #ifdef EX_DATAERR |
| 7479 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7480 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7481 | #ifdef EX_NOINPUT |
| 7482 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7483 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7484 | #ifdef EX_NOUSER |
| 7485 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7486 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7487 | #ifdef EX_NOHOST |
| 7488 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7489 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7490 | #ifdef EX_UNAVAILABLE |
| 7491 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7492 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7493 | #ifdef EX_SOFTWARE |
| 7494 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7495 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7496 | #ifdef EX_OSERR |
| 7497 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7498 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7499 | #ifdef EX_OSFILE |
| 7500 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7501 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7502 | #ifdef EX_CANTCREAT |
| 7503 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7504 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7505 | #ifdef EX_IOERR |
| 7506 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7507 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7508 | #ifdef EX_TEMPFAIL |
| 7509 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7510 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7511 | #ifdef EX_PROTOCOL |
| 7512 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7513 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7514 | #ifdef EX_NOPERM |
| 7515 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7516 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7517 | #ifdef EX_CONFIG |
| 7518 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7519 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7520 | #ifdef EX_NOTFOUND |
| 7521 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7522 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7523 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7524 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7525 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7526 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7527 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7528 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7529 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7530 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7531 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7532 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7533 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7534 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7535 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7536 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7537 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7538 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7539 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7540 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7541 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7542 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7543 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7544 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7545 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7546 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7547 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7548 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7549 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7550 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7551 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7552 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7553 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7554 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7555 | #if defined(PYOS_OS2) |
| 7556 | if (insertvalues(d)) return -1; |
| 7557 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7558 | return 0; |
| 7559 | } |
| 7560 | |
| 7561 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7562 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7563 | #define INITFUNC initnt |
| 7564 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7565 | |
| 7566 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7567 | #define INITFUNC initos2 |
| 7568 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7569 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7570 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7571 | #define INITFUNC initposix |
| 7572 | #define MODNAME "posix" |
| 7573 | #endif |
| 7574 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7575 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7576 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7577 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7578 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7579 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7580 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7581 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7582 | posix__doc__); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7583 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7584 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7585 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7586 | Py_XINCREF(v); |
| 7587 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7588 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7589 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7590 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7591 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7592 | return; |
| 7593 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7594 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7595 | return; |
| 7596 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7597 | Py_INCREF(PyExc_OSError); |
| 7598 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7599 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7600 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7601 | if (posix_putenv_garbage == NULL) |
| 7602 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7603 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7604 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7605 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7606 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7607 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7608 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7609 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7610 | structseq_new = StatResultType.tp_new; |
| 7611 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7612 | Py_INCREF((PyObject*) &StatResultType); |
| 7613 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7614 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7615 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7616 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7617 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7618 | PyModule_AddObject(m, "statvfs_result", |
| 7619 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7620 | } |