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> |
| 31 | /* ----- */ |
| 32 | /* DECC on Alpha does redefine these already */ |
| 33 | # ifndef shell$from_vms |
| 34 | # define shell$from_vms(_p1_,_p2_,_p3_) decc$from_vms(_p1_,_p2_,_p3_) |
| 35 | # endif |
| 36 | # ifndef shell$translate_vms |
| 37 | # define shell$translate_vms(_p1_) decc$translate_vms(_p1_) |
| 38 | # endif |
| 39 | # ifndef shell$to_vms |
| 40 | # define shell$to_vms(_p1_,_p2_,_p3_,_p4_,_p5_) \ |
| 41 | decc$to_vms(_p1_,_p2_,_p3_,_p4_,_p5_) |
| 42 | # endif |
| 43 | # include <wait.h> /* define wait() */ |
| 44 | #endif /* defined(__VMS) */ |
| 45 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 46 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 47 | "This module provides access to operating system functionality that is\n\ |
| 48 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 49 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 50 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 52 | #ifndef Py_USING_UNICODE |
| 53 | /* This is used in signatures of functions. */ |
| 54 | #define Py_UNICODE void |
| 55 | #endif |
| 56 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 57 | #if defined(PYOS_OS2) |
| 58 | #define INCL_DOS |
| 59 | #define INCL_DOSERRORS |
| 60 | #define INCL_DOSPROCESS |
| 61 | #define INCL_NOPMAPI |
| 62 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 63 | #if defined(PYCC_GCC) |
| 64 | #include <ctype.h> |
| 65 | #include <io.h> |
| 66 | #include <stdio.h> |
| 67 | #include <process.h> |
| 68 | #include "osdefs.h" |
| 69 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 70 | #endif |
| 71 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 72 | #include <sys/types.h> |
| 73 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SYS_WAIT_H |
| 76 | #include <sys/wait.h> /* For WNOHANG */ |
| 77 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 79 | #ifdef HAVE_SIGNAL_H |
| 80 | #include <signal.h> |
| 81 | #endif |
| 82 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 83 | #ifdef HAVE_FCNTL_H |
| 84 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 85 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 87 | #ifdef HAVE_GRP_H |
| 88 | #include <grp.h> |
| 89 | #endif |
| 90 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 91 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 92 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 93 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 94 | #include <process.h> |
| 95 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 96 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 97 | #define HAVE_GETCWD 1 |
| 98 | #define HAVE_OPENDIR 1 |
| 99 | #define HAVE_SYSTEM 1 |
| 100 | #if defined(__OS2__) |
| 101 | #define HAVE_EXECV 1 |
| 102 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 103 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 104 | #include <process.h> |
| 105 | #else |
| 106 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 107 | #define HAVE_EXECV 1 |
| 108 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 109 | #define HAVE_OPENDIR 1 |
| 110 | #define HAVE_PIPE 1 |
| 111 | #define HAVE_POPEN 1 |
| 112 | #define HAVE_SYSTEM 1 |
| 113 | #define HAVE_WAIT 1 |
| 114 | #else |
| 115 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 116 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 117 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 118 | #define HAVE_EXECV 1 |
| 119 | #define HAVE_PIPE 1 |
| 120 | #define HAVE_POPEN 1 |
| 121 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 122 | #define HAVE_CWAIT 1 |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 123 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 124 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 125 | /* 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] | 126 | #else /* all other compilers */ |
| 127 | /* Unix functions that the configure script doesn't check for */ |
| 128 | #define HAVE_EXECV 1 |
| 129 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 130 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 131 | #define HAVE_FORK1 1 |
| 132 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 133 | #define HAVE_GETCWD 1 |
| 134 | #define HAVE_GETEGID 1 |
| 135 | #define HAVE_GETEUID 1 |
| 136 | #define HAVE_GETGID 1 |
| 137 | #define HAVE_GETPPID 1 |
| 138 | #define HAVE_GETUID 1 |
| 139 | #define HAVE_KILL 1 |
| 140 | #define HAVE_OPENDIR 1 |
| 141 | #define HAVE_PIPE 1 |
| 142 | #define HAVE_POPEN 1 |
| 143 | #define HAVE_SYSTEM 1 |
| 144 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 145 | #define HAVE_TTYNAME 1 |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 146 | #endif /* PYOS_OS2 && PYCC_GCC */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 147 | #endif /* _MSC_VER */ |
| 148 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 149 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 150 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 152 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 154 | #if defined(sun) && !defined(__SVR4) |
| 155 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 156 | extern int rename(const char *, const char *); |
| 157 | extern int pclose(FILE *); |
| 158 | extern int fclose(FILE *); |
Thomas Wouters | 0f954a4 | 2001-02-15 08:46:56 +0000 | [diff] [blame] | 159 | extern int fsync(int); |
| 160 | extern int lstat(const char *, struct stat *); |
| 161 | extern int symlink(const char *, const char *); |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 162 | #endif |
| 163 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 164 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 165 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 166 | (default) */ |
| 167 | extern char *ctermid_r(char *); |
| 168 | #endif |
| 169 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 170 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 171 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 173 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 174 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 175 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 176 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 177 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 179 | #endif |
| 180 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int chdir(char *); |
| 182 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 183 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 184 | extern int chdir(const char *); |
| 185 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 186 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 187 | #ifdef __BORLANDC__ |
| 188 | extern int chmod(const char *, int); |
| 189 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 190 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 191 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 192 | extern int chown(const char *, uid_t, gid_t); |
| 193 | extern char *getcwd(char *, int); |
| 194 | extern char *strerror(int); |
| 195 | extern int link(const char *, const char *); |
| 196 | extern int rename(const char *, const char *); |
| 197 | extern int stat(const char *, struct stat *); |
| 198 | extern int unlink(const char *); |
| 199 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 200 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 201 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 202 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 205 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 208 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 210 | #ifdef HAVE_UTIME_H |
| 211 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 212 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 214 | #ifdef HAVE_SYS_UTIME_H |
| 215 | #include <sys/utime.h> |
| 216 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 217 | #endif /* HAVE_SYS_UTIME_H */ |
| 218 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | #ifdef HAVE_SYS_TIMES_H |
| 220 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 221 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
| 223 | #ifdef HAVE_SYS_PARAM_H |
| 224 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 225 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | |
| 227 | #ifdef HAVE_SYS_UTSNAME_H |
| 228 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 229 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 231 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 234 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 235 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 236 | #include <direct.h> |
| 237 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 238 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 240 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 241 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 244 | #endif |
| 245 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 246 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 247 | #endif |
| 248 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 250 | #endif |
| 251 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 253 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | #include <direct.h> |
| 255 | #include <io.h> |
| 256 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 257 | #include "osdefs.h" |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 258 | #define WIN32_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 260 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 261 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 262 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 263 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 265 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 267 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 268 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 269 | #ifndef MAXPATHLEN |
| 270 | #define MAXPATHLEN 1024 |
| 271 | #endif /* MAXPATHLEN */ |
| 272 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 273 | #ifdef UNION_WAIT |
| 274 | /* Emulate some macros on systems that have a union instead of macros */ |
| 275 | |
| 276 | #ifndef WIFEXITED |
| 277 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 278 | #endif |
| 279 | |
| 280 | #ifndef WEXITSTATUS |
| 281 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 282 | #endif |
| 283 | |
| 284 | #ifndef WTERMSIG |
| 285 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 286 | #endif |
| 287 | |
| 288 | #endif /* UNION_WAIT */ |
| 289 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 290 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 291 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 292 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 293 | #define USE_CTERMID_R |
| 294 | #endif |
| 295 | |
| 296 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 297 | #define USE_TMPNAM_R |
| 298 | #endif |
| 299 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 300 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 301 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 302 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 303 | # define STAT _stati64 |
| 304 | # define FSTAT _fstati64 |
| 305 | # define STRUCT_STAT struct _stati64 |
| 306 | #else |
| 307 | # define STAT stat |
| 308 | # define FSTAT fstat |
| 309 | # define STRUCT_STAT struct stat |
| 310 | #endif |
| 311 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 312 | #if defined(MAJOR_IN_MKDEV) |
| 313 | #include <sys/mkdev.h> |
| 314 | #else |
| 315 | #if defined(MAJOR_IN_SYSMACROS) |
| 316 | #include <sys/sysmacros.h> |
| 317 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 318 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 319 | #include <sys/mkdev.h> |
| 320 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 321 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 322 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 324 | #ifdef WITH_NEXT_FRAMEWORK |
| 325 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 326 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 327 | */ |
| 328 | #include <crt_externs.h> |
| 329 | static char **environ; |
| 330 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 332 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 334 | #if defined(__VMS) |
| 335 | static char psxmod_gt_psxpath[1026]; |
| 336 | |
| 337 | static int |
| 338 | psxmod_from_vms_action (char *spec) |
| 339 | { |
| 340 | (void)strcpy(psxmod_gt_psxpath, spec); |
| 341 | return 1; |
| 342 | } |
| 343 | |
| 344 | /* Return a dictionary corresponding to the VMS 'environment table' */ |
| 345 | static char* at_home = "HOME"; |
| 346 | static char* at_path = "PATH"; |
| 347 | |
| 348 | static char psxmod_t_command [] = "SYS$COMMAND"; |
| 349 | /* add some values to provide a similar environment like POSIX */ |
| 350 | void |
| 351 | psmmod_add_posix_env(PyObject *d) |
| 352 | { |
| 353 | /* -------------------- */ |
| 354 | struct dsc$descriptor_s r_device_name; |
| 355 | long l_devbufsiz; |
| 356 | long l_tt_page; |
| 357 | long l_item_code; |
| 358 | long l_status; |
| 359 | PyObject *o; |
| 360 | struct dsc$descriptor_s r_resultant_string; |
| 361 | char t_resultant_string[13]; /* enough space for username (12)+ '\0' */ |
| 362 | char *at_resultant_string; |
| 363 | short int w_resultant_length; |
| 364 | |
| 365 | /* set up string descriptor */ |
| 366 | r_device_name.dsc$w_length = strlen(psxmod_t_command); |
| 367 | r_device_name.dsc$b_dtype = DSC$K_DTYPE_T; |
| 368 | r_device_name.dsc$b_class = DSC$K_CLASS_S; |
| 369 | r_device_name.dsc$a_pointer = &psxmod_t_command[0]; |
| 370 | |
| 371 | /* -------------------- */ |
| 372 | /* COLUMNS = $getdvi("SYS$COMMAND","DEVBUFSIZ") */ |
| 373 | l_item_code = DVI$_DEVBUFSIZ; |
| 374 | l_status = lib$getdvi(&l_item_code, |
| 375 | 0, /* [channel] */ |
| 376 | &r_device_name, |
| 377 | &l_devbufsiz, /* integer-value */ |
| 378 | 0, /* resultant_string */ |
| 379 | 0); /* resultant_length */ |
| 380 | if (l_status == SS$_NORMAL) { |
| 381 | /* create a string object here to comply with POSIX */ |
| 382 | |
| 383 | /* set up string descriptor */ |
| 384 | r_resultant_string.dsc$w_length = |
| 385 | (sizeof(t_resultant_string) - 1); /* ommit '\0' at end */ |
| 386 | r_resultant_string.dsc$b_dtype = DSC$K_DTYPE_T; |
| 387 | r_resultant_string.dsc$b_class = DSC$K_CLASS_S; |
| 388 | r_resultant_string.dsc$a_pointer = &t_resultant_string[0]; |
| 389 | |
| 390 | /* Convert Signed Integer to Decimal Text */ |
| 391 | l_status = ots$cvt_l_ti(&l_devbufsiz, &r_resultant_string, 1, |
| 392 | 4, 0); |
| 393 | if (l_status == SS$_NORMAL) { |
| 394 | /* terminate string for 'C'-style */ |
| 395 | t_resultant_string[sizeof(t_resultant_string)-1] = '\0'; |
| 396 | /* string appears as: ' value' -- skip ' ' */ |
| 397 | at_resultant_string = &t_resultant_string[0]; |
| 398 | while ((*at_resultant_string == ' ' ) && |
| 399 | (*at_resultant_string != '\0')) { |
| 400 | at_resultant_string++; /* skip prefix spaces */ |
| 401 | } |
| 402 | |
| 403 | o = Py_BuildValue("s", at_resultant_string); |
| 404 | if (o != NULL) { |
| 405 | (void) PyDict_SetItemString(d, "COLUMNS", o); |
| 406 | Py_DECREF(o); |
| 407 | } |
| 408 | } /* (l_status = ots$cvt_l_ti() == SS$_NORMAL) */ |
| 409 | } /* (l_status = lib$getdvi(DVI$_DEVBUFSIZ) == SS$_NORMAL) */ |
| 410 | /* LINES = $getdvi("SYS$COMMAND","TT_PAGE") */ |
| 411 | l_item_code = DVI$_TT_PAGE; |
| 412 | l_status = lib$getdvi(&l_item_code, |
| 413 | 0, /* [channel] */ |
| 414 | &r_device_name, |
| 415 | &l_tt_page, /* integer-value */ |
| 416 | 0, /* resultant_string */ |
| 417 | 0); /* resultant_length */ |
| 418 | if (l_status == SS$_NORMAL) { |
| 419 | /* create a string object here to comply with POSIX */ |
| 420 | |
| 421 | /* set up string descriptor */ |
| 422 | r_resultant_string.dsc$w_length = |
| 423 | (sizeof(t_resultant_string) - 1); /* ommit '\0' at end */ |
| 424 | r_resultant_string.dsc$b_dtype = DSC$K_DTYPE_T; |
| 425 | r_resultant_string.dsc$b_class = DSC$K_CLASS_S; |
| 426 | r_resultant_string.dsc$a_pointer = &t_resultant_string[0]; |
| 427 | |
| 428 | /* Convert Signed Integer to Decimal Text */ |
| 429 | l_status = ots$cvt_l_ti(&l_tt_page, &r_resultant_string, |
| 430 | 1, 4, 0); |
| 431 | if (l_status == SS$_NORMAL) { |
| 432 | /* terminate string for 'C'-style */ |
| 433 | t_resultant_string[sizeof(t_resultant_string)-1] = '\0'; |
| 434 | /* string appears as: ' value' -- skip ' ' */ |
| 435 | at_resultant_string = &t_resultant_string[0]; |
| 436 | while ((*at_resultant_string == ' ' ) && |
| 437 | (*at_resultant_string != '\0')) { |
| 438 | at_resultant_string++; /* skip prefix spaces */ |
| 439 | } |
| 440 | |
| 441 | o = Py_BuildValue("s", at_resultant_string); |
| 442 | if (o != NULL) { |
| 443 | (void)PyDict_SetItemString(d, "LINES", o); |
| 444 | Py_DECREF(o); |
| 445 | } |
| 446 | } /* (l_status = ots$cvt_l_ti() == SS$_NORMAL) */ |
| 447 | } /* (l_status = lib$getdvi(DVI$_TT_PAGE) == SS$_NORMAL) */ |
| 448 | /* else -- ignore error */ |
| 449 | |
| 450 | /* LOGNAME = $getjpi(0,"USERNAME") */ |
| 451 | l_item_code = JPI$_USERNAME; |
| 452 | |
| 453 | /* set up string descriptor */ |
| 454 | r_resultant_string.dsc$w_length = |
| 455 | (sizeof(t_resultant_string) - 1); /* ommit '\0' at end */ |
| 456 | r_resultant_string.dsc$b_dtype = DSC$K_DTYPE_T; |
| 457 | r_resultant_string.dsc$b_class = DSC$K_CLASS_S; |
| 458 | r_resultant_string.dsc$a_pointer = &t_resultant_string[0]; |
| 459 | |
| 460 | l_status = lib$getjpi(&l_item_code, 0, 0, 0, |
| 461 | &r_resultant_string, &w_resultant_length); |
| 462 | if (l_status == SS$_NORMAL){ |
| 463 | t_resultant_string[w_resultant_length] = '\0'; |
| 464 | |
| 465 | /* remove any trailing spaces by replacing 1st one with '\0' */ |
| 466 | at_resultant_string = &t_resultant_string[0]; |
| 467 | while ((*at_resultant_string != ' ' ) && |
| 468 | (*at_resultant_string != '\0')) { |
| 469 | /* lowercase for compatibility with POSIX */ |
| 470 | *at_resultant_string = tolower(*at_resultant_string); |
| 471 | at_resultant_string++; /* skip non-blank */ |
| 472 | } |
| 473 | *at_resultant_string = '\0'; /* terminate */ |
| 474 | |
| 475 | o = Py_BuildValue("s", &t_resultant_string[0]); |
| 476 | if (o != NULL) { |
| 477 | (void) PyDict_SetItemString(d, "LOGNAME", o); |
| 478 | (void) PyDict_SetItemString(d, "USERNAME", o); |
| 479 | Py_DECREF(o); |
| 480 | } |
| 481 | } /* (l_status == SS$_NORMAL) */ |
| 482 | |
| 483 | /* OS = "OpenVMS" */ |
| 484 | o = PyString_FromString ("OpenVMS"); |
| 485 | if (o != NULL) { |
| 486 | (void)PyDict_SetItemString(d, "OS", o); |
| 487 | Py_DECREF(o); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /* @@ posix env: |
| 492 | COLUMNS=80 $ write sys$output f$getdvi("SYS$COMMAND","DEVBUFSIZ") |
| 493 | LINES=47 $ write sys$output f$getdvi("SYS$COMMAND","TT_PAGE") |
| 494 | LOGNAME=zessin $ write sys$output f$edit(f$getjpi(0,"username"), - |
| 495 | "collapse,lowercase") |
| 496 | OS=OpenVMS |
| 497 | PS1=HERE $ |
| 498 | |
| 499 | TZ=CET-1:00CET DST,M3.5.0/2:00,M10.5.0/3:00 |
| 500 | $ write sys$output f$trnlnm("POSIX$DEFAULT_TZ") |
| 501 | "CET-1:00CET DST-2:00,M3.5.0/2:00,M10.5.0/3:00" |
| 502 | $ write sys$output f$trnlnm("UCX$TZ") |
| 503 | "MET-1MET_DST-2,M3.5.0/2,M10.5.0/3" |
| 504 | PAGER=more |
| 505 | TERM=vt300_series |
| 506 | SHELL=/bin/sh |
| 507 | HOME=/dka100/user/zessin |
| 508 | _=/bin/env |
| 509 | |
| 510 | >>> for v in os.environ.items(): |
| 511 | ... print v |
| 512 | ... |
| 513 | ('HOME', '/user_here/zessin') |
| 514 | ('COLUMNS', '80') |
| 515 | ('LINES', '24') |
| 516 | ('PATH', '/python_disk/python/python-1_5_2/vms') |
| 517 | ('OS', 'OpenVMS') |
| 518 | ('USER', 'ZESSIN') |
| 519 | ('LOGNAME', 'zessin') |
| 520 | ('TERM', 'vt300-80') |
| 521 | ('USERNAME', 'zessin') |
| 522 | >>> |
| 523 | */ |
| 524 | #endif /* __VMS */ |
| 525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 526 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 527 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 528 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 529 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 530 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 531 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | if (d == NULL) |
| 533 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 534 | #ifdef WITH_NEXT_FRAMEWORK |
| 535 | if (environ == NULL) |
| 536 | environ = *_NSGetEnviron(); |
| 537 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 538 | if (environ == NULL) |
| 539 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 540 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 541 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 542 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 543 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 544 | char *p = strchr(*e, '='); |
| 545 | if (p == NULL) |
| 546 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 547 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 548 | if (k == NULL) { |
| 549 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 550 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 551 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 552 | #if defined(__VMS) |
| 553 | if ((strncmp(at_home, *e, sizeof(at_home)) == 0) || |
| 554 | (strncmp(at_path, *e, sizeof(at_path)) == 0)) { |
| 555 | (void)shell$from_vms(p+1, psxmod_from_vms_action, 0); |
| 556 | /* 0 = no wildcard expansion */ |
| 557 | v = PyString_FromString(psxmod_gt_psxpath); |
| 558 | } |
| 559 | else { |
| 560 | v = PyString_FromString(p+1); |
| 561 | } |
| 562 | #else |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 563 | v = PyString_FromString(p+1); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 564 | #endif |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 565 | if (v == NULL) { |
| 566 | PyErr_Clear(); |
| 567 | Py_DECREF(k); |
| 568 | continue; |
| 569 | } |
| 570 | if (PyDict_GetItem(d, k) == NULL) { |
| 571 | if (PyDict_SetItem(d, k, v) != 0) |
| 572 | PyErr_Clear(); |
| 573 | } |
| 574 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 575 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 576 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 577 | #if defined(__VMS) |
| 578 | psmmod_add_posix_env(d); |
| 579 | #endif /* defined(__VMS) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 580 | #if defined(PYOS_OS2) |
| 581 | { |
| 582 | APIRET rc; |
| 583 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 584 | |
| 585 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 586 | 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] | 587 | PyObject *v = PyString_FromString(buffer); |
| 588 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 589 | Py_DECREF(v); |
| 590 | } |
| 591 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 592 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 593 | PyObject *v = PyString_FromString(buffer); |
| 594 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 595 | Py_DECREF(v); |
| 596 | } |
| 597 | } |
| 598 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 599 | return d; |
| 600 | } |
| 601 | |
| 602 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 603 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 604 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 605 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 606 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 607 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 608 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 609 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 610 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 611 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 612 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 613 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 616 | #ifdef Py_WIN_WIDE_FILENAMES |
| 617 | static PyObject * |
| 618 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 619 | { |
| 620 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 621 | } |
| 622 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 623 | |
| 624 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 625 | static PyObject * |
| 626 | posix_error_with_allocated_filename(char* name) |
| 627 | { |
| 628 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 629 | PyMem_Free(name); |
| 630 | return rc; |
| 631 | } |
| 632 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 633 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 634 | static PyObject * |
| 635 | win32_error(char* function, char* filename) |
| 636 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 637 | /* XXX We should pass the function name along in the future. |
| 638 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 639 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 640 | Windows error object, which is non-trivial. |
| 641 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 642 | errno = GetLastError(); |
| 643 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 644 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 645 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 646 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 647 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 648 | |
| 649 | #ifdef Py_WIN_WIDE_FILENAMES |
| 650 | static PyObject * |
| 651 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 652 | { |
| 653 | /* XXX - see win32_error for comments on 'function' */ |
| 654 | errno = GetLastError(); |
| 655 | if (filename) |
| 656 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 657 | else |
| 658 | return PyErr_SetFromWindowsErr(errno); |
| 659 | } |
| 660 | |
| 661 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 662 | { |
| 663 | /* XXX Perhaps we should make this API an alias of |
| 664 | PyObject_Unicode() instead ?! */ |
| 665 | if (PyUnicode_CheckExact(obj)) { |
| 666 | Py_INCREF(obj); |
| 667 | return obj; |
| 668 | } |
| 669 | if (PyUnicode_Check(obj)) { |
| 670 | /* For a Unicode subtype that's not a Unicode object, |
| 671 | return a true Unicode object with the same data. */ |
| 672 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 673 | PyUnicode_GET_SIZE(obj)); |
| 674 | } |
| 675 | return PyUnicode_FromEncodedObject(obj, |
| 676 | Py_FileSystemDefaultEncoding, |
| 677 | "strict"); |
| 678 | } |
| 679 | |
| 680 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 681 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 682 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 683 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 684 | #if defined(PYOS_OS2) |
| 685 | /********************************************************************** |
| 686 | * Helper Function to Trim and Format OS/2 Messages |
| 687 | **********************************************************************/ |
| 688 | static void |
| 689 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 690 | { |
| 691 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 692 | |
| 693 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 694 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 695 | |
| 696 | while (lastc > msgbuf && isspace(*lastc)) |
| 697 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 698 | } |
| 699 | |
| 700 | /* Add Optional Reason Text */ |
| 701 | if (reason) { |
| 702 | strcat(msgbuf, " : "); |
| 703 | strcat(msgbuf, reason); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | /********************************************************************** |
| 708 | * Decode an OS/2 Operating System Error Code |
| 709 | * |
| 710 | * A convenience function to lookup an OS/2 error code and return a |
| 711 | * text message we can use to raise a Python exception. |
| 712 | * |
| 713 | * Notes: |
| 714 | * The messages for errors returned from the OS/2 kernel reside in |
| 715 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 716 | * |
| 717 | **********************************************************************/ |
| 718 | static char * |
| 719 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 720 | { |
| 721 | APIRET rc; |
| 722 | ULONG msglen; |
| 723 | |
| 724 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 725 | Py_BEGIN_ALLOW_THREADS |
| 726 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 727 | errorcode, "oso001.msg", &msglen); |
| 728 | Py_END_ALLOW_THREADS |
| 729 | |
| 730 | if (rc == NO_ERROR) |
| 731 | os2_formatmsg(msgbuf, msglen, reason); |
| 732 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 733 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 734 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 735 | |
| 736 | return msgbuf; |
| 737 | } |
| 738 | |
| 739 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 740 | errors are not in a global variable e.g. 'errno' nor are |
| 741 | they congruent with posix error numbers. */ |
| 742 | |
| 743 | static PyObject * os2_error(int code) |
| 744 | { |
| 745 | char text[1024]; |
| 746 | PyObject *v; |
| 747 | |
| 748 | os2_strerror(text, sizeof(text), code, ""); |
| 749 | |
| 750 | v = Py_BuildValue("(is)", code, text); |
| 751 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 752 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 753 | Py_DECREF(v); |
| 754 | } |
| 755 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 756 | } |
| 757 | |
| 758 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 759 | |
| 760 | /* POSIX generic methods */ |
| 761 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 762 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 763 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 764 | { |
| 765 | int fd; |
| 766 | int res; |
| 767 | fd = PyObject_AsFileDescriptor(fdobj); |
| 768 | if (fd < 0) |
| 769 | return NULL; |
| 770 | Py_BEGIN_ALLOW_THREADS |
| 771 | res = (*func)(fd); |
| 772 | Py_END_ALLOW_THREADS |
| 773 | if (res < 0) |
| 774 | return posix_error(); |
| 775 | Py_INCREF(Py_None); |
| 776 | return Py_None; |
| 777 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 778 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 779 | #ifdef Py_WIN_WIDE_FILENAMES |
| 780 | static int |
| 781 | unicode_file_names(void) |
| 782 | { |
| 783 | static int canusewide = -1; |
| 784 | if (canusewide == -1) { |
| 785 | /* As per doc for ::GetVersion(), this is the correct test for |
| 786 | the Windows NT family. */ |
| 787 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 788 | } |
| 789 | return canusewide; |
| 790 | } |
| 791 | #endif |
| 792 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 793 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 794 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 795 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 796 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 797 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 798 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 799 | #ifdef Py_WIN_WIDE_FILENAMES |
| 800 | if (unicode_file_names()) { |
| 801 | PyUnicodeObject *po; |
| 802 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 803 | Py_BEGIN_ALLOW_THREADS |
| 804 | /* PyUnicode_AS_UNICODE OK without thread |
| 805 | lock as it is a simple dereference. */ |
| 806 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 807 | Py_END_ALLOW_THREADS |
| 808 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 809 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 810 | Py_INCREF(Py_None); |
| 811 | return Py_None; |
| 812 | } |
| 813 | /* Drop the argument parsing error as narrow |
| 814 | strings are also valid. */ |
| 815 | PyErr_Clear(); |
| 816 | } |
| 817 | #else |
| 818 | /* Platforms that don't support Unicode filenames |
| 819 | shouldn't be passing these extra params */ |
| 820 | assert(wformat==NULL && wfunc == NULL); |
| 821 | #endif |
| 822 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 823 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 824 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 825 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 826 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 827 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 828 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 829 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 830 | return posix_error_with_allocated_filename(path1); |
| 831 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 832 | Py_INCREF(Py_None); |
| 833 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 836 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 837 | posix_2str(PyObject *args, |
| 838 | char *format, |
| 839 | int (*func)(const char *, const char *), |
| 840 | char *wformat, |
| 841 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 842 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 843 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 844 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 845 | #ifdef Py_WIN_WIDE_FILENAMES |
| 846 | if (unicode_file_names()) { |
| 847 | PyObject *po1; |
| 848 | PyObject *po2; |
| 849 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 850 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 851 | PyObject *wpath1; |
| 852 | PyObject *wpath2; |
| 853 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 854 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 855 | if (!wpath1 || !wpath2) { |
| 856 | Py_XDECREF(wpath1); |
| 857 | Py_XDECREF(wpath2); |
| 858 | return NULL; |
| 859 | } |
| 860 | Py_BEGIN_ALLOW_THREADS |
| 861 | /* PyUnicode_AS_UNICODE OK without thread |
| 862 | lock as it is a simple dereference. */ |
| 863 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 864 | PyUnicode_AS_UNICODE(wpath2)); |
| 865 | Py_END_ALLOW_THREADS |
| 866 | Py_XDECREF(wpath1); |
| 867 | Py_XDECREF(wpath2); |
| 868 | if (res != 0) |
| 869 | return posix_error(); |
| 870 | Py_INCREF(Py_None); |
| 871 | return Py_None; |
| 872 | } |
| 873 | /* Else flow through as neither is Unicode. */ |
| 874 | } |
| 875 | /* Drop the argument parsing error as narrow |
| 876 | strings are also valid. */ |
| 877 | PyErr_Clear(); |
| 878 | } |
| 879 | #else |
| 880 | /* Platforms that don't support Unicode filenames |
| 881 | shouldn't be passing these extra params */ |
| 882 | assert(wformat==NULL && wfunc == NULL); |
| 883 | #endif |
| 884 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 885 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 886 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 887 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 888 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 889 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 890 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 891 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 892 | PyMem_Free(path1); |
| 893 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 894 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 895 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 896 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 897 | Py_INCREF(Py_None); |
| 898 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 901 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 902 | "stat_result: Result from stat or lstat.\n\n\ |
| 903 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 904 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 905 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 906 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 907 | 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] | 908 | they are available as attributes only.\n\ |
| 909 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 910 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 911 | |
| 912 | static PyStructSequence_Field stat_result_fields[] = { |
| 913 | {"st_mode", "protection bits"}, |
| 914 | {"st_ino", "inode"}, |
| 915 | {"st_dev", "device"}, |
| 916 | {"st_nlink", "number of hard links"}, |
| 917 | {"st_uid", "user ID of owner"}, |
| 918 | {"st_gid", "group ID of owner"}, |
| 919 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 920 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 921 | {NULL, "integer time of last access"}, |
| 922 | {NULL, "integer time of last modification"}, |
| 923 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 924 | {"st_atime", "time of last access"}, |
| 925 | {"st_mtime", "time of last modification"}, |
| 926 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 927 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 928 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 929 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 930 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 931 | {"st_blocks", "number of blocks allocated"}, |
| 932 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 933 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 934 | {"st_rdev", "device type (if inode device)"}, |
| 935 | #endif |
| 936 | {0} |
| 937 | }; |
| 938 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 939 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 940 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 941 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 942 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 943 | #endif |
| 944 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 945 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 946 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 947 | #else |
| 948 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 949 | #endif |
| 950 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 951 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 952 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 953 | #else |
| 954 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 955 | #endif |
| 956 | |
| 957 | static PyStructSequence_Desc stat_result_desc = { |
| 958 | "stat_result", /* name */ |
| 959 | stat_result__doc__, /* doc */ |
| 960 | stat_result_fields, |
| 961 | 10 |
| 962 | }; |
| 963 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 964 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 965 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 966 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 967 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 968 | 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] | 969 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 970 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 971 | |
| 972 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 973 | {"f_bsize", }, |
| 974 | {"f_frsize", }, |
| 975 | {"f_blocks", }, |
| 976 | {"f_bfree", }, |
| 977 | {"f_bavail", }, |
| 978 | {"f_files", }, |
| 979 | {"f_ffree", }, |
| 980 | {"f_favail", }, |
| 981 | {"f_flag", }, |
| 982 | {"f_namemax",}, |
| 983 | {0} |
| 984 | }; |
| 985 | |
| 986 | static PyStructSequence_Desc statvfs_result_desc = { |
| 987 | "statvfs_result", /* name */ |
| 988 | statvfs_result__doc__, /* doc */ |
| 989 | statvfs_result_fields, |
| 990 | 10 |
| 991 | }; |
| 992 | |
| 993 | static PyTypeObject StatResultType; |
| 994 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 995 | static newfunc structseq_new; |
| 996 | |
| 997 | static PyObject * |
| 998 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 999 | { |
| 1000 | PyStructSequence *result; |
| 1001 | int i; |
| 1002 | |
| 1003 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1004 | if (!result) |
| 1005 | return NULL; |
| 1006 | /* If we have been initialized from a tuple, |
| 1007 | st_?time might be set to None. Initialize it |
| 1008 | from the int slots. */ |
| 1009 | for (i = 7; i <= 9; i++) { |
| 1010 | if (result->ob_item[i+3] == Py_None) { |
| 1011 | Py_DECREF(Py_None); |
| 1012 | Py_INCREF(result->ob_item[i]); |
| 1013 | result->ob_item[i+3] = result->ob_item[i]; |
| 1014 | } |
| 1015 | } |
| 1016 | return (PyObject*)result; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | /* If true, st_?time is float. */ |
| 1022 | static int _stat_float_times = 0; |
| 1023 | |
| 1024 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1025 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1026 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1027 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1028 | future calls return ints. \n\ |
| 1029 | If newval is omitted, return the current setting.\n"); |
| 1030 | |
| 1031 | static PyObject* |
| 1032 | stat_float_times(PyObject* self, PyObject *args) |
| 1033 | { |
| 1034 | int newval = -1; |
| 1035 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1036 | return NULL; |
| 1037 | if (newval == -1) |
| 1038 | /* Return old value */ |
| 1039 | return PyBool_FromLong(_stat_float_times); |
| 1040 | _stat_float_times = newval; |
| 1041 | Py_INCREF(Py_None); |
| 1042 | return Py_None; |
| 1043 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1044 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1045 | static void |
| 1046 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1047 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1048 | PyObject *fval,*ival; |
| 1049 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 1050 | ival = PyLong_FromLongLong((LONG_LONG)sec); |
| 1051 | #else |
| 1052 | ival = PyInt_FromLong((long)sec); |
| 1053 | #endif |
| 1054 | if (_stat_float_times) { |
| 1055 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1056 | } else { |
| 1057 | fval = ival; |
| 1058 | Py_INCREF(fval); |
| 1059 | } |
| 1060 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1061 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1064 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1065 | (used by posix_stat() and posix_fstat()) */ |
| 1066 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1067 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1068 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1069 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1070 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1071 | if (v == NULL) |
| 1072 | return NULL; |
| 1073 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1074 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1075 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1076 | PyStructSequence_SET_ITEM(v, 1, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1077 | PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1078 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1079 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1080 | #endif |
| 1081 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1082 | PyStructSequence_SET_ITEM(v, 2, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1083 | PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1084 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1085 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1086 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1087 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 1088 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 1089 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1090 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1091 | PyStructSequence_SET_ITEM(v, 6, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1092 | PyLong_FromLongLong((LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1093 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1094 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1095 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1096 | |
| 1097 | #ifdef HAVE_STAT_TV_NSEC |
| 1098 | ansec = st.st_atim.tv_nsec; |
| 1099 | mnsec = st.st_mtim.tv_nsec; |
| 1100 | cnsec = st.st_ctim.tv_nsec; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1101 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1102 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1103 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1104 | fill_time(v, 7, st.st_atime, ansec); |
| 1105 | fill_time(v, 8, st.st_mtime, mnsec); |
| 1106 | fill_time(v, 9, st.st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1107 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1108 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1109 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1110 | PyInt_FromLong((long)st.st_blksize)); |
| 1111 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1112 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1113 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1114 | PyInt_FromLong((long)st.st_blocks)); |
| 1115 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1116 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1117 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1118 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1119 | #endif |
| 1120 | |
| 1121 | if (PyErr_Occurred()) { |
| 1122 | Py_DECREF(v); |
| 1123 | return NULL; |
| 1124 | } |
| 1125 | |
| 1126 | return v; |
| 1127 | } |
| 1128 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1129 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1130 | posix_do_stat(PyObject *self, PyObject *args, |
| 1131 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1132 | #ifdef __VMS |
| 1133 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1134 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1135 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1136 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1137 | char *wformat, |
| 1138 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1139 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1140 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1141 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1142 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1143 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1144 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1145 | #ifdef MS_WINDOWS |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1146 | int pathlen; |
| 1147 | char pathcopy[MAX_PATH]; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1148 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1149 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1150 | |
| 1151 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1152 | /* If on wide-character-capable OS see if argument |
| 1153 | is Unicode and if so use wide API. */ |
| 1154 | if (unicode_file_names()) { |
| 1155 | PyUnicodeObject *po; |
| 1156 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1157 | Py_UNICODE wpath[MAX_PATH+1]; |
| 1158 | pathlen = wcslen(PyUnicode_AS_UNICODE(po)); |
| 1159 | /* the library call can blow up if the file name is too long! */ |
| 1160 | if (pathlen > MAX_PATH) { |
| 1161 | errno = ENAMETOOLONG; |
| 1162 | return posix_error(); |
| 1163 | } |
| 1164 | wcscpy(wpath, PyUnicode_AS_UNICODE(po)); |
| 1165 | /* Remove trailing slash or backslash, unless it's the current |
| 1166 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 1167 | */ |
| 1168 | if (pathlen > 0 && |
| 1169 | (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) { |
| 1170 | /* It does end with a slash -- exempt the root drive cases. */ |
| 1171 | /* XXX UNC root drives should also be exempted? */ |
| 1172 | if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':')) |
| 1173 | /* leave it alone */; |
| 1174 | else { |
| 1175 | /* nuke the trailing backslash */ |
| 1176 | wpath[pathlen-1] = L'\0'; |
| 1177 | } |
| 1178 | } |
| 1179 | Py_BEGIN_ALLOW_THREADS |
| 1180 | /* PyUnicode_AS_UNICODE result OK without |
| 1181 | thread lock as it is a simple dereference. */ |
| 1182 | res = wstatfunc(wpath, &st); |
| 1183 | Py_END_ALLOW_THREADS |
| 1184 | if (res != 0) |
| 1185 | return posix_error_with_unicode_filename(wpath); |
| 1186 | return _pystat_fromstructstat(st); |
| 1187 | } |
| 1188 | /* Drop the argument parsing error as narrow strings |
| 1189 | are also valid. */ |
| 1190 | PyErr_Clear(); |
| 1191 | } |
| 1192 | #endif |
| 1193 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1194 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1195 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1196 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1197 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1198 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1199 | #ifdef MS_WINDOWS |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1200 | pathlen = strlen(path); |
| 1201 | /* the library call can blow up if the file name is too long! */ |
| 1202 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1203 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1204 | errno = ENAMETOOLONG; |
| 1205 | return posix_error(); |
| 1206 | } |
| 1207 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1208 | /* Remove trailing slash or backslash, unless it's the current |
| 1209 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 1210 | */ |
| 1211 | if (pathlen > 0 && |
| 1212 | (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) { |
| 1213 | /* It does end with a slash -- exempt the root drive cases. */ |
| 1214 | /* XXX UNC root drives should also be exempted? */ |
| 1215 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':')) |
| 1216 | /* leave it alone */; |
| 1217 | else { |
| 1218 | /* nuke the trailing backslash */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1219 | strncpy(pathcopy, path, pathlen); |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1220 | pathcopy[pathlen-1] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1221 | path = pathcopy; |
| 1222 | } |
| 1223 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1224 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1225 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1226 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1227 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1228 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1229 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1230 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1231 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1232 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1233 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | |
| 1237 | /* POSIX methods */ |
| 1238 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1239 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1240 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1241 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1242 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1243 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1244 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1245 | 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] | 1246 | |
| 1247 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1248 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1249 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1250 | char *path; |
| 1251 | int mode; |
| 1252 | int res; |
| 1253 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1254 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1255 | return NULL; |
| 1256 | Py_BEGIN_ALLOW_THREADS |
| 1257 | res = access(path, mode); |
| 1258 | Py_END_ALLOW_THREADS |
Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 1259 | return(PyBool_FromLong(res == 0)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1262 | #ifndef F_OK |
| 1263 | #define F_OK 0 |
| 1264 | #endif |
| 1265 | #ifndef R_OK |
| 1266 | #define R_OK 4 |
| 1267 | #endif |
| 1268 | #ifndef W_OK |
| 1269 | #define W_OK 2 |
| 1270 | #endif |
| 1271 | #ifndef X_OK |
| 1272 | #define X_OK 1 |
| 1273 | #endif |
| 1274 | |
| 1275 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1276 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1277 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1278 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1279 | |
| 1280 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1281 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1282 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1283 | int id; |
| 1284 | char *ret; |
| 1285 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1286 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1287 | return NULL; |
| 1288 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1289 | #if defined(__VMS) |
| 1290 | /* DECC V5.0 - only about FD= 0 @@ try getname()+$getdvi(dvi$_devnam) */ |
| 1291 | if (id == 0) { |
| 1292 | ret = ttyname(); |
| 1293 | } |
| 1294 | else { |
| 1295 | ret = NULL; |
| 1296 | } |
| 1297 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1298 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1299 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1300 | if (ret == NULL) |
| 1301 | return(posix_error()); |
| 1302 | return(PyString_FromString(ret)); |
| 1303 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1304 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1305 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1306 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1307 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1308 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1309 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1310 | |
| 1311 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1312 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1313 | { |
| 1314 | char *ret; |
| 1315 | char buffer[L_ctermid]; |
| 1316 | |
| 1317 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 1318 | return NULL; |
| 1319 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1320 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1321 | ret = ctermid_r(buffer); |
| 1322 | #else |
| 1323 | ret = ctermid(buffer); |
| 1324 | #endif |
| 1325 | if (ret == NULL) |
| 1326 | return(posix_error()); |
| 1327 | return(PyString_FromString(buffer)); |
| 1328 | } |
| 1329 | #endif |
| 1330 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1331 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1332 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1333 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1334 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1335 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1336 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1337 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1338 | #ifdef MS_WINDOWS |
| 1339 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1340 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1341 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1342 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1343 | #ifdef __VMS |
| 1344 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
| 1345 | NULL, NULL); |
| 1346 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1347 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1348 | #endif |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1349 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1352 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1353 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1354 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1355 | 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] | 1356 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1357 | |
| 1358 | static PyObject * |
| 1359 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1360 | { |
| 1361 | return posix_fildes(fdobj, fchdir); |
| 1362 | } |
| 1363 | #endif /* HAVE_FCHDIR */ |
| 1364 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1365 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1366 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1367 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1368 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1369 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1370 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1371 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1372 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1373 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1374 | int i; |
| 1375 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1376 | if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1377 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1378 | return NULL; |
| 1379 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1380 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1381 | Py_END_ALLOW_THREADS |
| 1382 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1383 | return posix_error_with_allocated_filename(path); |
| 1384 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1385 | Py_INCREF(Py_None); |
| 1386 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1389 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1390 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1391 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1392 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1393 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1394 | |
| 1395 | static PyObject * |
| 1396 | posix_chroot(PyObject *self, PyObject *args) |
| 1397 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1398 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1399 | } |
| 1400 | #endif |
| 1401 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1402 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1403 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1404 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1405 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1406 | |
| 1407 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1408 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1409 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1410 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1411 | } |
| 1412 | #endif /* HAVE_FSYNC */ |
| 1413 | |
| 1414 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1415 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1416 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1417 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1418 | #endif |
| 1419 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1420 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1421 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1422 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1423 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1424 | |
| 1425 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1426 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1427 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1428 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1429 | } |
| 1430 | #endif /* HAVE_FDATASYNC */ |
| 1431 | |
| 1432 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1433 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1434 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1435 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1436 | 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] | 1437 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1438 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1439 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1440 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1441 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1442 | int uid, gid; |
| 1443 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1444 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1445 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1446 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1447 | return NULL; |
| 1448 | Py_BEGIN_ALLOW_THREADS |
| 1449 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1450 | Py_END_ALLOW_THREADS |
| 1451 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1452 | return posix_error_with_allocated_filename(path); |
| 1453 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1454 | Py_INCREF(Py_None); |
| 1455 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1456 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1457 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1458 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1459 | #ifdef HAVE_LCHOWN |
| 1460 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1461 | "lchown(path, uid, gid)\n\n\ |
| 1462 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1463 | This function will not follow symbolic links."); |
| 1464 | |
| 1465 | static PyObject * |
| 1466 | posix_lchown(PyObject *self, PyObject *args) |
| 1467 | { |
| 1468 | char *path = NULL; |
| 1469 | int uid, gid; |
| 1470 | int res; |
| 1471 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1472 | Py_FileSystemDefaultEncoding, &path, |
| 1473 | &uid, &gid)) |
| 1474 | return NULL; |
| 1475 | Py_BEGIN_ALLOW_THREADS |
| 1476 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1477 | Py_END_ALLOW_THREADS |
| 1478 | if (res < 0) |
| 1479 | return posix_error_with_allocated_filename(path); |
| 1480 | PyMem_Free(path); |
| 1481 | Py_INCREF(Py_None); |
| 1482 | return Py_None; |
| 1483 | } |
| 1484 | #endif /* HAVE_LCHOWN */ |
| 1485 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1486 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1487 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1488 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1489 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1490 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1491 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1492 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1493 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1494 | { |
| 1495 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1496 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1497 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1498 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1499 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1500 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1501 | res = _getcwd2(buf, sizeof buf); |
| 1502 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1503 | #if defined(__VMS) |
| 1504 | /* 0 = force Unix-style path if in the VMS DCL environment! */ |
| 1505 | res = getcwd(buf, sizeof buf, 0); |
| 1506 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1507 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1508 | #endif |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1509 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1510 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1511 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1512 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1513 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1514 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1515 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1516 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1517 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1518 | "getcwdu() -> path\n\n\ |
| 1519 | Return a unicode string representing the current working directory."); |
| 1520 | |
| 1521 | static PyObject * |
| 1522 | posix_getcwdu(PyObject *self, PyObject *args) |
| 1523 | { |
| 1524 | char buf[1026]; |
| 1525 | char *res; |
| 1526 | if (!PyArg_ParseTuple(args, ":getcwd")) |
| 1527 | return NULL; |
| 1528 | |
| 1529 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1530 | if (unicode_file_names()) { |
| 1531 | wchar_t *wres; |
| 1532 | wchar_t wbuf[1026]; |
| 1533 | Py_BEGIN_ALLOW_THREADS |
| 1534 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1535 | Py_END_ALLOW_THREADS |
| 1536 | if (wres == NULL) |
| 1537 | return posix_error(); |
| 1538 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1539 | } |
| 1540 | #endif |
| 1541 | |
| 1542 | Py_BEGIN_ALLOW_THREADS |
| 1543 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1544 | res = _getcwd2(buf, sizeof buf); |
| 1545 | #else |
| 1546 | res = getcwd(buf, sizeof buf); |
| 1547 | #endif |
| 1548 | Py_END_ALLOW_THREADS |
| 1549 | if (res == NULL) |
| 1550 | return posix_error(); |
| 1551 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1552 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1553 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1554 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1555 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1556 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1557 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1558 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1559 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1560 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1561 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1562 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1563 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1564 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1565 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1566 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1567 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1568 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1569 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1570 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1571 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1572 | Return a list containing the names of the entries in the directory.\n\ |
| 1573 | \n\ |
| 1574 | path: path of directory to list\n\ |
| 1575 | \n\ |
| 1576 | 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] | 1577 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1578 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1579 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1580 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1581 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1582 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1583 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1584 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1585 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1586 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1587 | HANDLE hFindFile; |
| 1588 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1589 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1590 | char namebuf[MAX_PATH*2+5]; |
| 1591 | char *bufptr = namebuf; |
| 1592 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1593 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1594 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1595 | /* If on wide-character-capable OS see if argument |
| 1596 | is Unicode and if so use wide API. */ |
| 1597 | if (unicode_file_names()) { |
| 1598 | PyUnicodeObject *po; |
| 1599 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1600 | WIN32_FIND_DATAW wFileData; |
| 1601 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1602 | Py_UNICODE wch; |
| 1603 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1604 | wnamebuf[MAX_PATH] = L'\0'; |
| 1605 | len = wcslen(wnamebuf); |
| 1606 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1607 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1608 | wnamebuf[len++] = L'/'; |
| 1609 | wcscpy(wnamebuf + len, L"*.*"); |
| 1610 | if ((d = PyList_New(0)) == NULL) |
| 1611 | return NULL; |
| 1612 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1613 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1614 | errno = GetLastError(); |
| 1615 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1616 | return d; |
| 1617 | } |
| 1618 | Py_DECREF(d); |
| 1619 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1620 | } |
| 1621 | do { |
| 1622 | if (wFileData.cFileName[0] == L'.' && |
| 1623 | (wFileData.cFileName[1] == L'\0' || |
| 1624 | wFileData.cFileName[1] == L'.' && |
| 1625 | wFileData.cFileName[2] == L'\0')) |
| 1626 | continue; |
| 1627 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1628 | if (v == NULL) { |
| 1629 | Py_DECREF(d); |
| 1630 | d = NULL; |
| 1631 | break; |
| 1632 | } |
| 1633 | if (PyList_Append(d, v) != 0) { |
| 1634 | Py_DECREF(v); |
| 1635 | Py_DECREF(d); |
| 1636 | d = NULL; |
| 1637 | break; |
| 1638 | } |
| 1639 | Py_DECREF(v); |
| 1640 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); |
| 1641 | |
| 1642 | if (FindClose(hFindFile) == FALSE) { |
| 1643 | Py_DECREF(d); |
| 1644 | return win32_error_unicode("FindClose", wnamebuf); |
| 1645 | } |
| 1646 | return d; |
| 1647 | } |
| 1648 | /* Drop the argument parsing error as narrow strings |
| 1649 | are also valid. */ |
| 1650 | PyErr_Clear(); |
| 1651 | } |
| 1652 | #endif |
| 1653 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1654 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1655 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1656 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1657 | if (len > 0) { |
| 1658 | char ch = namebuf[len-1]; |
| 1659 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1660 | namebuf[len++] = '/'; |
| 1661 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1662 | strcpy(namebuf + len, "*.*"); |
| 1663 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1664 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1665 | return NULL; |
| 1666 | |
| 1667 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1668 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1669 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1670 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1671 | return d; |
| 1672 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1673 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1674 | } |
| 1675 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1676 | if (FileData.cFileName[0] == '.' && |
| 1677 | (FileData.cFileName[1] == '\0' || |
| 1678 | FileData.cFileName[1] == '.' && |
| 1679 | FileData.cFileName[2] == '\0')) |
| 1680 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1681 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1682 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1683 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1684 | d = NULL; |
| 1685 | break; |
| 1686 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1687 | if (PyList_Append(d, v) != 0) { |
| 1688 | Py_DECREF(v); |
| 1689 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1690 | d = NULL; |
| 1691 | break; |
| 1692 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1693 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1694 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1695 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1696 | if (FindClose(hFindFile) == FALSE) { |
| 1697 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1698 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1699 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1700 | |
| 1701 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1702 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1703 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1704 | |
| 1705 | #ifndef MAX_PATH |
| 1706 | #define MAX_PATH CCHMAXPATH |
| 1707 | #endif |
| 1708 | char *name, *pt; |
| 1709 | int len; |
| 1710 | PyObject *d, *v; |
| 1711 | char namebuf[MAX_PATH+5]; |
| 1712 | HDIR hdir = 1; |
| 1713 | ULONG srchcnt = 1; |
| 1714 | FILEFINDBUF3 ep; |
| 1715 | APIRET rc; |
| 1716 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1717 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1718 | return NULL; |
| 1719 | if (len >= MAX_PATH) { |
| 1720 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1721 | return NULL; |
| 1722 | } |
| 1723 | strcpy(namebuf, name); |
| 1724 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1725 | if (*pt == ALTSEP) |
| 1726 | *pt = SEP; |
| 1727 | if (namebuf[len-1] != SEP) |
| 1728 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1729 | strcpy(namebuf + len, "*.*"); |
| 1730 | |
| 1731 | if ((d = PyList_New(0)) == NULL) |
| 1732 | return NULL; |
| 1733 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1734 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1735 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1736 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1737 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1738 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1739 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1740 | |
| 1741 | if (rc != NO_ERROR) { |
| 1742 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1743 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1746 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1747 | do { |
| 1748 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1749 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1750 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1751 | |
| 1752 | strcpy(namebuf, ep.achName); |
| 1753 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1754 | /* Leave Case of Name Alone -- In Native Form */ |
| 1755 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1756 | |
| 1757 | v = PyString_FromString(namebuf); |
| 1758 | if (v == NULL) { |
| 1759 | Py_DECREF(d); |
| 1760 | d = NULL; |
| 1761 | break; |
| 1762 | } |
| 1763 | if (PyList_Append(d, v) != 0) { |
| 1764 | Py_DECREF(v); |
| 1765 | Py_DECREF(d); |
| 1766 | d = NULL; |
| 1767 | break; |
| 1768 | } |
| 1769 | Py_DECREF(v); |
| 1770 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1771 | } |
| 1772 | |
| 1773 | return d; |
| 1774 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1775 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1776 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1777 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1778 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1779 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1780 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1781 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1782 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1783 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1784 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1785 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1786 | closedir(dirp); |
| 1787 | return NULL; |
| 1788 | } |
| 1789 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1790 | if (ep->d_name[0] == '.' && |
| 1791 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1792 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1793 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1794 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1795 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1796 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1797 | d = NULL; |
| 1798 | break; |
| 1799 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1800 | if (PyList_Append(d, v) != 0) { |
| 1801 | Py_DECREF(v); |
| 1802 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1803 | d = NULL; |
| 1804 | break; |
| 1805 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1806 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1807 | } |
| 1808 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1809 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1810 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1811 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1812 | #endif /* which OS */ |
| 1813 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1814 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1815 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1816 | /* A helper function for abspath on win32 */ |
| 1817 | static PyObject * |
| 1818 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1819 | { |
| 1820 | /* assume encoded strings wont more than double no of chars */ |
| 1821 | char inbuf[MAX_PATH*2]; |
| 1822 | char *inbufp = inbuf; |
| 1823 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1824 | char outbuf[MAX_PATH*2]; |
| 1825 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1826 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1827 | if (unicode_file_names()) { |
| 1828 | PyUnicodeObject *po; |
| 1829 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1830 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1831 | Py_UNICODE *wtemp; |
| 1832 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
| 1833 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1834 | woutbuf, &wtemp)) |
| 1835 | return win32_error("GetFullPathName", ""); |
| 1836 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1837 | } |
| 1838 | /* Drop the argument parsing error as narrow strings |
| 1839 | are also valid. */ |
| 1840 | PyErr_Clear(); |
| 1841 | } |
| 1842 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1843 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1844 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1845 | &insize)) |
| 1846 | return NULL; |
| 1847 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1848 | outbuf, &temp)) |
| 1849 | return win32_error("GetFullPathName", inbuf); |
| 1850 | return PyString_FromString(outbuf); |
| 1851 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1852 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1853 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1854 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1855 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1856 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1857 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1858 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1859 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1860 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1861 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1862 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1863 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1864 | |
| 1865 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1866 | if (unicode_file_names()) { |
| 1867 | PyUnicodeObject *po; |
| 1868 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po)) { |
| 1869 | Py_BEGIN_ALLOW_THREADS |
| 1870 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1871 | it is a simple dereference. */ |
| 1872 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1873 | Py_END_ALLOW_THREADS |
| 1874 | if (res < 0) |
| 1875 | return posix_error(); |
| 1876 | Py_INCREF(Py_None); |
| 1877 | return Py_None; |
| 1878 | } |
| 1879 | /* Drop the argument parsing error as narrow strings |
| 1880 | are also valid. */ |
| 1881 | PyErr_Clear(); |
| 1882 | } |
| 1883 | #endif |
| 1884 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1885 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1886 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1887 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1888 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1889 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1890 | res = mkdir(path); |
| 1891 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1892 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1893 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1894 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1895 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1896 | return posix_error_with_allocated_filename(path); |
| 1897 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1898 | Py_INCREF(Py_None); |
| 1899 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1902 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1903 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1904 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1905 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1906 | #include <sys/resource.h> |
| 1907 | #endif |
| 1908 | #endif |
| 1909 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1910 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1911 | "nice(inc) -> new_priority\n\n\ |
| 1912 | 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] | 1913 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1914 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1915 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1916 | { |
| 1917 | int increment, value; |
| 1918 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1919 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1920 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1921 | |
| 1922 | /* There are two flavours of 'nice': one that returns the new |
| 1923 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1924 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1925 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1926 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1927 | If we are of the nice family that returns the new priority, we |
| 1928 | need to clear errno before the call, and check if errno is filled |
| 1929 | before calling posix_error() on a returnvalue of -1, because the |
| 1930 | -1 may be the actual new priority! */ |
| 1931 | |
| 1932 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1933 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1934 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1935 | if (value == 0) |
| 1936 | value = getpriority(PRIO_PROCESS, 0); |
| 1937 | #endif |
| 1938 | if (value == -1 && errno != 0) |
| 1939 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1940 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1941 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1942 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1943 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1944 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1945 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1946 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1947 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1948 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1949 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1950 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1951 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1952 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1953 | #ifdef MS_WINDOWS |
| 1954 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 1955 | #else |
| 1956 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 1957 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1960 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1961 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1962 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1963 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1964 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1965 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1966 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1967 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1968 | #ifdef MS_WINDOWS |
| 1969 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 1970 | #else |
| 1971 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 1972 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1975 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1976 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1977 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1978 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1979 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1980 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1981 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1982 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1983 | #ifdef MS_WINDOWS |
| 1984 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64); |
| 1985 | #else |
| 1986 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 1987 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1990 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1991 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1992 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1993 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1994 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1995 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1996 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1997 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1998 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1999 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2000 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2001 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2002 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2003 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2004 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2005 | Py_END_ALLOW_THREADS |
| 2006 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2007 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2008 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2009 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2010 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2011 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2012 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2013 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2014 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2015 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2016 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2017 | { |
| 2018 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2019 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2020 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2021 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2022 | if (i < 0) |
| 2023 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2024 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2028 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2029 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2030 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2032 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2033 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2034 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2035 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2036 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2037 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2038 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2039 | #ifdef MS_WINDOWS |
| 2040 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 2041 | #else |
| 2042 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 2043 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2046 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2047 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2048 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2049 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2050 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2051 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2052 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2053 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2054 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2055 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2056 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2057 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 2058 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2059 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2060 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2061 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2062 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2063 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2064 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2065 | u.sysname, |
| 2066 | u.nodename, |
| 2067 | u.release, |
| 2068 | u.version, |
| 2069 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2070 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2071 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2072 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2073 | static int |
| 2074 | extract_time(PyObject *t, long* sec, long* usec) |
| 2075 | { |
| 2076 | long intval; |
| 2077 | if (PyFloat_Check(t)) { |
| 2078 | double tval = PyFloat_AsDouble(t); |
| 2079 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2080 | if (!intobj) |
| 2081 | return -1; |
| 2082 | intval = PyInt_AsLong(intobj); |
| 2083 | Py_DECREF(intobj); |
| 2084 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2085 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2086 | if (*usec < 0) |
| 2087 | /* If rounding gave us a negative number, |
| 2088 | truncate. */ |
| 2089 | *usec = 0; |
| 2090 | return 0; |
| 2091 | } |
| 2092 | intval = PyInt_AsLong(t); |
| 2093 | if (intval == -1 && PyErr_Occurred()) |
| 2094 | return -1; |
| 2095 | *sec = intval; |
| 2096 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2097 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2098 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2099 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2100 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2101 | "utime(path, (atime, utime))\n\ |
| 2102 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2103 | 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] | 2104 | 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] | 2105 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2106 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2107 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2108 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2109 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2110 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2111 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2112 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2113 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2114 | #if defined(HAVE_UTIMES) |
| 2115 | struct timeval buf[2]; |
| 2116 | #define ATIME buf[0].tv_sec |
| 2117 | #define MTIME buf[1].tv_sec |
| 2118 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2119 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2120 | struct utimbuf buf; |
| 2121 | #define ATIME buf.actime |
| 2122 | #define MTIME buf.modtime |
| 2123 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2124 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2125 | time_t buf[2]; |
| 2126 | #define ATIME buf[0] |
| 2127 | #define MTIME buf[1] |
| 2128 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2129 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2130 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2131 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2132 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2133 | if (arg == Py_None) { |
| 2134 | /* optional time values not given */ |
| 2135 | Py_BEGIN_ALLOW_THREADS |
| 2136 | res = utime(path, NULL); |
| 2137 | Py_END_ALLOW_THREADS |
| 2138 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2139 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2140 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2141 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2142 | return NULL; |
| 2143 | } |
| 2144 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2145 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2146 | &atime, &ausec) == -1) |
| 2147 | return NULL; |
| 2148 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2149 | &mtime, &musec) == -1) |
| 2150 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2151 | ATIME = atime; |
| 2152 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2153 | #ifdef HAVE_UTIMES |
| 2154 | buf[0].tv_usec = ausec; |
| 2155 | buf[1].tv_usec = musec; |
| 2156 | Py_BEGIN_ALLOW_THREADS |
| 2157 | res = utimes(path, buf); |
| 2158 | Py_END_ALLOW_THREADS |
| 2159 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2160 | Py_BEGIN_ALLOW_THREADS |
| 2161 | res = utime(path, UTIME_ARG); |
| 2162 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2163 | #endif |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2164 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2165 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 2166 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2167 | Py_INCREF(Py_None); |
| 2168 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2169 | #undef UTIME_ARG |
| 2170 | #undef ATIME |
| 2171 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2174 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2175 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2177 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2178 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2179 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2180 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2181 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2182 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2183 | { |
| 2184 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2185 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2186 | return NULL; |
| 2187 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2188 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2191 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2192 | static void |
| 2193 | free_string_array(char **array, int count) |
| 2194 | { |
| 2195 | int i; |
| 2196 | for (i = 0; i < count; i++) |
| 2197 | PyMem_Free(array[i]); |
| 2198 | PyMem_DEL(array); |
| 2199 | } |
| 2200 | #endif |
| 2201 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2202 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2203 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2204 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2205 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2206 | Execute an executable path with arguments, replacing current process.\n\ |
| 2207 | \n\ |
| 2208 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2209 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2210 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2211 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2212 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2213 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2214 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2215 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2216 | char **argvlist; |
| 2217 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2218 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2219 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2220 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2221 | argv is a list or tuple of strings. */ |
| 2222 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2223 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2224 | Py_FileSystemDefaultEncoding, |
| 2225 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2226 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2227 | if (PyList_Check(argv)) { |
| 2228 | argc = PyList_Size(argv); |
| 2229 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2230 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2231 | else if (PyTuple_Check(argv)) { |
| 2232 | argc = PyTuple_Size(argv); |
| 2233 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2234 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2235 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2236 | 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] | 2237 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2238 | return NULL; |
| 2239 | } |
| 2240 | |
| 2241 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2242 | 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] | 2243 | PyMem_Free(path); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2244 | return NULL; |
| 2245 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2246 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2247 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2248 | if (argvlist == NULL) { |
| 2249 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2250 | return NULL; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2251 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2252 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2253 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2254 | Py_FileSystemDefaultEncoding, |
| 2255 | &argvlist[i])) { |
| 2256 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2257 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2258 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2259 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2260 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2261 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2262 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2263 | } |
| 2264 | argvlist[argc] = NULL; |
| 2265 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2266 | #ifdef BAD_EXEC_PROTOTYPES |
| 2267 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2268 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2269 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2270 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2271 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2272 | /* If we get here it's definitely an error */ |
| 2273 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2274 | free_string_array(argvlist, argc); |
| 2275 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2276 | return posix_error(); |
| 2277 | } |
| 2278 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2279 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2280 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2281 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2282 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2283 | \n\ |
| 2284 | path: path of executable file\n\ |
| 2285 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2286 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2287 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2288 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2289 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2290 | { |
| 2291 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2292 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2293 | char **argvlist; |
| 2294 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2295 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2296 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2297 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2298 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2299 | |
| 2300 | /* execve has three arguments: (path, argv, env), where |
| 2301 | argv is a list or tuple of strings and env is a dictionary |
| 2302 | like posix.environ. */ |
| 2303 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2304 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2305 | Py_FileSystemDefaultEncoding, |
| 2306 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2307 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2308 | if (PyList_Check(argv)) { |
| 2309 | argc = PyList_Size(argv); |
| 2310 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2311 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2312 | else if (PyTuple_Check(argv)) { |
| 2313 | argc = PyTuple_Size(argv); |
| 2314 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2315 | } |
| 2316 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2317 | PyErr_SetString(PyExc_TypeError, |
| 2318 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2319 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2320 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2321 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2322 | PyErr_SetString(PyExc_TypeError, |
| 2323 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2324 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2327 | if (argc == 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2328 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2329 | "execve() arg 2 must not be empty"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2330 | goto fail_0; |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2333 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2334 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2335 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2336 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2337 | } |
| 2338 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2339 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2340 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2341 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2342 | &argvlist[i])) |
| 2343 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2344 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2345 | goto fail_1; |
| 2346 | } |
| 2347 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2348 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2349 | argvlist[argc] = NULL; |
| 2350 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2351 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2352 | if (i < 0) |
| 2353 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2354 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2355 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2356 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2357 | goto fail_1; |
| 2358 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2359 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2360 | keys = PyMapping_Keys(env); |
| 2361 | vals = PyMapping_Values(env); |
| 2362 | if (!keys || !vals) |
| 2363 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2364 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2365 | PyErr_SetString(PyExc_TypeError, |
| 2366 | "execve(): env.keys() or env.values() is not a list"); |
| 2367 | goto fail_2; |
| 2368 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2369 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2370 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2371 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2372 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2373 | |
| 2374 | key = PyList_GetItem(keys, pos); |
| 2375 | val = PyList_GetItem(vals, pos); |
| 2376 | if (!key || !val) |
| 2377 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2378 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2379 | if (!PyArg_Parse( |
| 2380 | key, |
| 2381 | "s;execve() arg 3 contains a non-string key", |
| 2382 | &k) || |
| 2383 | !PyArg_Parse( |
| 2384 | val, |
| 2385 | "s;execve() arg 3 contains a non-string value", |
| 2386 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2387 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2388 | goto fail_2; |
| 2389 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2390 | |
| 2391 | #if defined(PYOS_OS2) |
| 2392 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2393 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2394 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2395 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2396 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2397 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2398 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2399 | goto fail_2; |
| 2400 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2401 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2402 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2403 | #if defined(PYOS_OS2) |
| 2404 | } |
| 2405 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2406 | } |
| 2407 | envlist[envc] = 0; |
| 2408 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2409 | |
| 2410 | #ifdef BAD_EXEC_PROTOTYPES |
| 2411 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2412 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2413 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2414 | #endif /* BAD_EXEC_PROTOTYPES */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2415 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2416 | /* If we get here it's definitely an error */ |
| 2417 | |
| 2418 | (void) posix_error(); |
| 2419 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2420 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2421 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2422 | PyMem_DEL(envlist[envc]); |
| 2423 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2424 | fail_1: |
| 2425 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2426 | Py_XDECREF(vals); |
| 2427 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2428 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2429 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2430 | return NULL; |
| 2431 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2432 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2433 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2434 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2435 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2436 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2437 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2438 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2439 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2440 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2441 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2442 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2443 | |
| 2444 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2445 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2446 | { |
| 2447 | char *path; |
| 2448 | PyObject *argv; |
| 2449 | char **argvlist; |
| 2450 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2451 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2452 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2453 | |
| 2454 | /* spawnv has three arguments: (mode, path, argv), where |
| 2455 | argv is a list or tuple of strings. */ |
| 2456 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2457 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2458 | Py_FileSystemDefaultEncoding, |
| 2459 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2460 | return NULL; |
| 2461 | if (PyList_Check(argv)) { |
| 2462 | argc = PyList_Size(argv); |
| 2463 | getitem = PyList_GetItem; |
| 2464 | } |
| 2465 | else if (PyTuple_Check(argv)) { |
| 2466 | argc = PyTuple_Size(argv); |
| 2467 | getitem = PyTuple_GetItem; |
| 2468 | } |
| 2469 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2470 | PyErr_SetString(PyExc_TypeError, |
| 2471 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2472 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2473 | return NULL; |
| 2474 | } |
| 2475 | |
| 2476 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2477 | if (argvlist == NULL) { |
| 2478 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2479 | return NULL; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2480 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2481 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2482 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2483 | Py_FileSystemDefaultEncoding, |
| 2484 | &argvlist[i])) { |
| 2485 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2486 | PyErr_SetString( |
| 2487 | PyExc_TypeError, |
| 2488 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2489 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2490 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2491 | } |
| 2492 | } |
| 2493 | argvlist[argc] = NULL; |
| 2494 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2495 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2496 | Py_BEGIN_ALLOW_THREADS |
| 2497 | spawnval = spawnv(mode, path, argvlist); |
| 2498 | Py_END_ALLOW_THREADS |
| 2499 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2500 | if (mode == _OLD_P_OVERLAY) |
| 2501 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2502 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2503 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2504 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2505 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2506 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2507 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2508 | free_string_array(argvlist, argc); |
| 2509 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2510 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2511 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2512 | return posix_error(); |
| 2513 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2514 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2515 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2516 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2517 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2518 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2522 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2523 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2524 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2525 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2526 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2527 | path: path of executable file\n\ |
| 2528 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2529 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2530 | |
| 2531 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2532 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2533 | { |
| 2534 | char *path; |
| 2535 | PyObject *argv, *env; |
| 2536 | char **argvlist; |
| 2537 | char **envlist; |
| 2538 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2539 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2540 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2541 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2542 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2543 | |
| 2544 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2545 | argv is a list or tuple of strings and env is a dictionary |
| 2546 | like posix.environ. */ |
| 2547 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2548 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2549 | Py_FileSystemDefaultEncoding, |
| 2550 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2551 | return NULL; |
| 2552 | if (PyList_Check(argv)) { |
| 2553 | argc = PyList_Size(argv); |
| 2554 | getitem = PyList_GetItem; |
| 2555 | } |
| 2556 | else if (PyTuple_Check(argv)) { |
| 2557 | argc = PyTuple_Size(argv); |
| 2558 | getitem = PyTuple_GetItem; |
| 2559 | } |
| 2560 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2561 | PyErr_SetString(PyExc_TypeError, |
| 2562 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2563 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2564 | } |
| 2565 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2566 | PyErr_SetString(PyExc_TypeError, |
| 2567 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2568 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | argvlist = PyMem_NEW(char *, argc+1); |
| 2572 | if (argvlist == NULL) { |
| 2573 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2574 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2575 | } |
| 2576 | for (i = 0; i < argc; i++) { |
| 2577 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2578 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2579 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2580 | &argvlist[i])) |
| 2581 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2582 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2583 | goto fail_1; |
| 2584 | } |
| 2585 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2586 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2587 | argvlist[argc] = NULL; |
| 2588 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2589 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2590 | if (i < 0) |
| 2591 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2592 | envlist = PyMem_NEW(char *, i + 1); |
| 2593 | if (envlist == NULL) { |
| 2594 | PyErr_NoMemory(); |
| 2595 | goto fail_1; |
| 2596 | } |
| 2597 | envc = 0; |
| 2598 | keys = PyMapping_Keys(env); |
| 2599 | vals = PyMapping_Values(env); |
| 2600 | if (!keys || !vals) |
| 2601 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2602 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2603 | PyErr_SetString(PyExc_TypeError, |
| 2604 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2605 | goto fail_2; |
| 2606 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2607 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2608 | for (pos = 0; pos < i; pos++) { |
| 2609 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2610 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2611 | |
| 2612 | key = PyList_GetItem(keys, pos); |
| 2613 | val = PyList_GetItem(vals, pos); |
| 2614 | if (!key || !val) |
| 2615 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2616 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2617 | if (!PyArg_Parse( |
| 2618 | key, |
| 2619 | "s;spawnve() arg 3 contains a non-string key", |
| 2620 | &k) || |
| 2621 | !PyArg_Parse( |
| 2622 | val, |
| 2623 | "s;spawnve() arg 3 contains a non-string value", |
| 2624 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2625 | { |
| 2626 | goto fail_2; |
| 2627 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2628 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2629 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2630 | if (p == NULL) { |
| 2631 | PyErr_NoMemory(); |
| 2632 | goto fail_2; |
| 2633 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2634 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2635 | envlist[envc++] = p; |
| 2636 | } |
| 2637 | envlist[envc] = 0; |
| 2638 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2639 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2640 | Py_BEGIN_ALLOW_THREADS |
| 2641 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2642 | Py_END_ALLOW_THREADS |
| 2643 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2644 | if (mode == _OLD_P_OVERLAY) |
| 2645 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2646 | |
| 2647 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2648 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2649 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2650 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2651 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2652 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2653 | (void) posix_error(); |
| 2654 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2655 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2656 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2657 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2658 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2659 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2660 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2661 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2662 | while (--envc >= 0) |
| 2663 | PyMem_DEL(envlist[envc]); |
| 2664 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2665 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2666 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2667 | Py_XDECREF(vals); |
| 2668 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2669 | fail_0: |
| 2670 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2671 | return res; |
| 2672 | } |
| 2673 | #endif /* HAVE_SPAWNV */ |
| 2674 | |
| 2675 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2676 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2677 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2678 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2679 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 2680 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2681 | 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] | 2682 | |
| 2683 | static PyObject * |
| 2684 | posix_fork1(self, args) |
| 2685 | PyObject *self; |
| 2686 | PyObject *args; |
| 2687 | { |
| 2688 | int pid; |
| 2689 | if (!PyArg_ParseTuple(args, ":fork1")) |
| 2690 | return NULL; |
| 2691 | pid = fork1(); |
| 2692 | if (pid == -1) |
| 2693 | return posix_error(); |
| 2694 | PyOS_AfterFork(); |
| 2695 | return PyInt_FromLong((long)pid); |
| 2696 | } |
| 2697 | #endif |
| 2698 | |
| 2699 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2700 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2701 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2702 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2703 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2704 | 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] | 2705 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2706 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2707 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2708 | { |
| 2709 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2710 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 2711 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2712 | pid = fork(); |
| 2713 | if (pid == -1) |
| 2714 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2715 | if (pid == 0) |
| 2716 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2717 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2718 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2719 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2720 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2721 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 2722 | #ifdef HAVE_PTY_H |
| 2723 | #include <pty.h> |
| 2724 | #else |
| 2725 | #ifdef HAVE_LIBUTIL_H |
| 2726 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2727 | #endif /* HAVE_LIBUTIL_H */ |
| 2728 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2729 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2730 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2731 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2732 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2733 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2734 | 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] | 2735 | |
| 2736 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2737 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2738 | { |
| 2739 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2740 | #ifndef HAVE_OPENPTY |
| 2741 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2742 | #endif |
| 2743 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2744 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 2745 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2746 | |
| 2747 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2748 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 2749 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2750 | #else |
| 2751 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 2752 | if (slave_name == NULL) |
| 2753 | return posix_error(); |
| 2754 | |
| 2755 | slave_fd = open(slave_name, O_RDWR); |
| 2756 | if (slave_fd < 0) |
| 2757 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 2758 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2759 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2760 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2761 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2762 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2763 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2764 | |
| 2765 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2766 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2767 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2768 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 2769 | 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] | 2770 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2771 | |
| 2772 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2773 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2774 | { |
| 2775 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2776 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2777 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 2778 | return NULL; |
| 2779 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 2780 | if (pid == -1) |
| 2781 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2782 | if (pid == 0) |
| 2783 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2784 | return Py_BuildValue("(ii)", pid, master_fd); |
| 2785 | } |
| 2786 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2787 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2788 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2789 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2790 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2791 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2792 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2793 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2794 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2795 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2796 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2797 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2798 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2799 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2800 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2801 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2802 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2803 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2804 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2805 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2806 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2807 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2808 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2809 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2810 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2811 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2812 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2813 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2814 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2815 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2816 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2817 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2818 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2819 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2820 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2821 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2822 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2823 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2824 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2825 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2826 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2827 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2828 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2829 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2830 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2831 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2833 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2834 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2835 | Return the current 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 * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2838 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2839 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2840 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2841 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2842 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2845 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2846 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2847 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2848 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2849 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2850 | |
| 2851 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2852 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2853 | { |
| 2854 | PyObject *result = NULL; |
| 2855 | |
| 2856 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 2857 | #ifdef NGROUPS_MAX |
| 2858 | #define MAX_GROUPS NGROUPS_MAX |
| 2859 | #else |
| 2860 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2861 | #define MAX_GROUPS 64 |
| 2862 | #endif |
| 2863 | gid_t grouplist[MAX_GROUPS]; |
| 2864 | int n; |
| 2865 | |
| 2866 | n = getgroups(MAX_GROUPS, grouplist); |
| 2867 | if (n < 0) |
| 2868 | posix_error(); |
| 2869 | else { |
| 2870 | result = PyList_New(n); |
| 2871 | if (result != NULL) { |
| 2872 | PyObject *o; |
| 2873 | int i; |
| 2874 | for (i = 0; i < n; ++i) { |
| 2875 | o = PyInt_FromLong((long)grouplist[i]); |
| 2876 | if (o == NULL) { |
| 2877 | Py_DECREF(result); |
| 2878 | result = NULL; |
| 2879 | break; |
| 2880 | } |
| 2881 | PyList_SET_ITEM(result, i, o); |
| 2882 | } |
| 2883 | } |
| 2884 | } |
| 2885 | } |
| 2886 | return result; |
| 2887 | } |
| 2888 | #endif |
| 2889 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2890 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2891 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2892 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2893 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2894 | |
| 2895 | static PyObject * |
| 2896 | posix_getpgid(PyObject *self, PyObject *args) |
| 2897 | { |
| 2898 | int pid, pgid; |
| 2899 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 2900 | return NULL; |
| 2901 | pgid = getpgid(pid); |
| 2902 | if (pgid < 0) |
| 2903 | return posix_error(); |
| 2904 | return PyInt_FromLong((long)pgid); |
| 2905 | } |
| 2906 | #endif /* HAVE_GETPGID */ |
| 2907 | |
| 2908 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2909 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2910 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2911 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2912 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2913 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2914 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2915 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2916 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2917 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2918 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2919 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2920 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2921 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2922 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2923 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2924 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2925 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2926 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2927 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2928 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2929 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2930 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2931 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2934 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2935 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2936 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2937 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2938 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2939 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2940 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2941 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2942 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2943 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2944 | Py_INCREF(Py_None); |
| 2945 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2948 | #endif /* HAVE_SETPGRP */ |
| 2949 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2950 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2951 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2952 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2953 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2954 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2955 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2956 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2957 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2958 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2959 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2960 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2961 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2962 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2963 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2964 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2965 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2966 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2967 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2968 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2969 | |
| 2970 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2971 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2972 | { |
| 2973 | PyObject *result = NULL; |
| 2974 | |
| 2975 | if (PyArg_ParseTuple(args, ":getlogin")) { |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2976 | char *name; |
| 2977 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2978 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2979 | errno = 0; |
| 2980 | name = getlogin(); |
| 2981 | if (name == NULL) { |
| 2982 | if (errno) |
| 2983 | posix_error(); |
| 2984 | else |
| 2985 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2986 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2987 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2988 | else |
| 2989 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2990 | errno = old_errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2991 | } |
| 2992 | return result; |
| 2993 | } |
| 2994 | #endif |
| 2995 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2996 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2997 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2998 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2999 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3000 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3001 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3002 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3003 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3004 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3005 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3006 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3007 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3008 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3009 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3010 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3011 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3012 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3013 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3014 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3015 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3016 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3017 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3018 | { |
| 3019 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3020 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3021 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3022 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3023 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3024 | APIRET rc; |
| 3025 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3026 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3027 | |
| 3028 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3029 | APIRET rc; |
| 3030 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3031 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3032 | |
| 3033 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3034 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3035 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3036 | if (kill(pid, sig) == -1) |
| 3037 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3038 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3039 | Py_INCREF(Py_None); |
| 3040 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3041 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3042 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3043 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3044 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3045 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3046 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3047 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3048 | |
| 3049 | static PyObject * |
| 3050 | posix_killpg(PyObject *self, PyObject *args) |
| 3051 | { |
| 3052 | int pgid, sig; |
| 3053 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3054 | return NULL; |
| 3055 | if (killpg(pgid, sig) == -1) |
| 3056 | return posix_error(); |
| 3057 | Py_INCREF(Py_None); |
| 3058 | return Py_None; |
| 3059 | } |
| 3060 | #endif |
| 3061 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3062 | #ifdef HAVE_PLOCK |
| 3063 | |
| 3064 | #ifdef HAVE_SYS_LOCK_H |
| 3065 | #include <sys/lock.h> |
| 3066 | #endif |
| 3067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3068 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3069 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3070 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3071 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3072 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3073 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3074 | { |
| 3075 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3076 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3077 | return NULL; |
| 3078 | if (plock(op) == -1) |
| 3079 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3080 | Py_INCREF(Py_None); |
| 3081 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3082 | } |
| 3083 | #endif |
| 3084 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3085 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3086 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3087 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3088 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3089 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3090 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3091 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3092 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3093 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3094 | async_system(const char *command) |
| 3095 | { |
| 3096 | char *p, errormsg[256], args[1024]; |
| 3097 | RESULTCODES rcodes; |
| 3098 | APIRET rc; |
| 3099 | char *shell = getenv("COMSPEC"); |
| 3100 | if (!shell) |
| 3101 | shell = "cmd"; |
| 3102 | |
| 3103 | strcpy(args, shell); |
| 3104 | p = &args[ strlen(args)+1 ]; |
| 3105 | strcpy(p, "/c "); |
| 3106 | strcat(p, command); |
| 3107 | p += strlen(p) + 1; |
| 3108 | *p = '\0'; |
| 3109 | |
| 3110 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3111 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3112 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3113 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3114 | &rcodes, shell); |
| 3115 | return rc; |
| 3116 | } |
| 3117 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3118 | static FILE * |
| 3119 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3120 | { |
| 3121 | HFILE rhan, whan; |
| 3122 | FILE *retfd = NULL; |
| 3123 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 3124 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3125 | if (rc != NO_ERROR) { |
| 3126 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3127 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3128 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3129 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3130 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 3131 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3132 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3133 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 3134 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3135 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3136 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 3137 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3138 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3139 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3142 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 3143 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3144 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3145 | if (rc == NO_ERROR) |
| 3146 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 3147 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3148 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 3149 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3150 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3151 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 3152 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3153 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3154 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 3155 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3156 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3157 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 3158 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3159 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3160 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3163 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 3164 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3165 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3166 | if (rc == NO_ERROR) |
| 3167 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 3168 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3169 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 3170 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3171 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3172 | } else { |
| 3173 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3174 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3175 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3179 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3180 | { |
| 3181 | char *name; |
| 3182 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3183 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3184 | FILE *fp; |
| 3185 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3186 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3187 | return NULL; |
| 3188 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3189 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3190 | Py_END_ALLOW_THREADS |
| 3191 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3192 | return os2_error(err); |
| 3193 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3194 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3195 | if (f != NULL) |
| 3196 | PyFile_SetBufSize(f, bufsize); |
| 3197 | return f; |
| 3198 | } |
| 3199 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3200 | #elif defined(PYCC_GCC) |
| 3201 | |
| 3202 | /* standard posix version of popen() support */ |
| 3203 | static PyObject * |
| 3204 | posix_popen(PyObject *self, PyObject *args) |
| 3205 | { |
| 3206 | char *name; |
| 3207 | char *mode = "r"; |
| 3208 | int bufsize = -1; |
| 3209 | FILE *fp; |
| 3210 | PyObject *f; |
| 3211 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3212 | return NULL; |
| 3213 | Py_BEGIN_ALLOW_THREADS |
| 3214 | fp = popen(name, mode); |
| 3215 | Py_END_ALLOW_THREADS |
| 3216 | if (fp == NULL) |
| 3217 | return posix_error(); |
| 3218 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3219 | if (f != NULL) |
| 3220 | PyFile_SetBufSize(f, bufsize); |
| 3221 | return f; |
| 3222 | } |
| 3223 | |
| 3224 | /* fork() under OS/2 has lots'o'warts |
| 3225 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3226 | * most of this code is a ripoff of the win32 code, but using the |
| 3227 | * capabilities of EMX's C library routines |
| 3228 | */ |
| 3229 | |
| 3230 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3231 | #define POPEN_1 1 |
| 3232 | #define POPEN_2 2 |
| 3233 | #define POPEN_3 3 |
| 3234 | #define POPEN_4 4 |
| 3235 | |
| 3236 | static PyObject *_PyPopen(char *, int, int, int); |
| 3237 | static int _PyPclose(FILE *file); |
| 3238 | |
| 3239 | /* |
| 3240 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3241 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3242 | * for more information on this dictionary's use. |
| 3243 | */ |
| 3244 | static PyObject *_PyPopenProcs = NULL; |
| 3245 | |
| 3246 | /* os2emx version of popen2() |
| 3247 | * |
| 3248 | * The result of this function is a pipe (file) connected to the |
| 3249 | * process's stdin, and a pipe connected to the process's stdout. |
| 3250 | */ |
| 3251 | |
| 3252 | static PyObject * |
| 3253 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3254 | { |
| 3255 | PyObject *f; |
| 3256 | int tm=0; |
| 3257 | |
| 3258 | char *cmdstring; |
| 3259 | char *mode = "t"; |
| 3260 | int bufsize = -1; |
| 3261 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3262 | return NULL; |
| 3263 | |
| 3264 | if (*mode == 't') |
| 3265 | tm = O_TEXT; |
| 3266 | else if (*mode != 'b') { |
| 3267 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3268 | return NULL; |
| 3269 | } else |
| 3270 | tm = O_BINARY; |
| 3271 | |
| 3272 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3273 | |
| 3274 | return f; |
| 3275 | } |
| 3276 | |
| 3277 | /* |
| 3278 | * Variation on os2emx.popen2 |
| 3279 | * |
| 3280 | * The result of this function is 3 pipes - the process's stdin, |
| 3281 | * stdout and stderr |
| 3282 | */ |
| 3283 | |
| 3284 | static PyObject * |
| 3285 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3286 | { |
| 3287 | PyObject *f; |
| 3288 | int tm = 0; |
| 3289 | |
| 3290 | char *cmdstring; |
| 3291 | char *mode = "t"; |
| 3292 | int bufsize = -1; |
| 3293 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3294 | return NULL; |
| 3295 | |
| 3296 | if (*mode == 't') |
| 3297 | tm = O_TEXT; |
| 3298 | else if (*mode != 'b') { |
| 3299 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3300 | return NULL; |
| 3301 | } else |
| 3302 | tm = O_BINARY; |
| 3303 | |
| 3304 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3305 | |
| 3306 | return f; |
| 3307 | } |
| 3308 | |
| 3309 | /* |
| 3310 | * Variation on os2emx.popen2 |
| 3311 | * |
| 3312 | * The result of this function is 2 pipes - the processes stdin, |
| 3313 | * and stdout+stderr combined as a single pipe. |
| 3314 | */ |
| 3315 | |
| 3316 | static PyObject * |
| 3317 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3318 | { |
| 3319 | PyObject *f; |
| 3320 | int tm = 0; |
| 3321 | |
| 3322 | char *cmdstring; |
| 3323 | char *mode = "t"; |
| 3324 | int bufsize = -1; |
| 3325 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3326 | return NULL; |
| 3327 | |
| 3328 | if (*mode == 't') |
| 3329 | tm = O_TEXT; |
| 3330 | else if (*mode != 'b') { |
| 3331 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3332 | return NULL; |
| 3333 | } else |
| 3334 | tm = O_BINARY; |
| 3335 | |
| 3336 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3337 | |
| 3338 | return f; |
| 3339 | } |
| 3340 | |
| 3341 | /* a couple of structures for convenient handling of multiple |
| 3342 | * file handles and pipes |
| 3343 | */ |
| 3344 | struct file_ref |
| 3345 | { |
| 3346 | int handle; |
| 3347 | int flags; |
| 3348 | }; |
| 3349 | |
| 3350 | struct pipe_ref |
| 3351 | { |
| 3352 | int rd; |
| 3353 | int wr; |
| 3354 | }; |
| 3355 | |
| 3356 | /* The following code is derived from the win32 code */ |
| 3357 | |
| 3358 | static PyObject * |
| 3359 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3360 | { |
| 3361 | struct file_ref stdio[3]; |
| 3362 | struct pipe_ref p_fd[3]; |
| 3363 | FILE *p_s[3]; |
| 3364 | int file_count, i, pipe_err, pipe_pid; |
| 3365 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3366 | PyObject *f, *p_f[3]; |
| 3367 | |
| 3368 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3369 | if (mode == O_TEXT) |
| 3370 | { |
| 3371 | rd_mode = "rt"; |
| 3372 | wr_mode = "wt"; |
| 3373 | } |
| 3374 | else |
| 3375 | { |
| 3376 | rd_mode = "rb"; |
| 3377 | wr_mode = "wb"; |
| 3378 | } |
| 3379 | |
| 3380 | /* prepare shell references */ |
| 3381 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3382 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3383 | { |
| 3384 | errno = ENOENT; |
| 3385 | return posix_error(); |
| 3386 | } |
| 3387 | |
| 3388 | sh_name = _getname(shell); |
| 3389 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3390 | opt = "/c"; |
| 3391 | else |
| 3392 | opt = "-c"; |
| 3393 | |
| 3394 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3395 | i = pipe_err = 0; |
| 3396 | while (pipe_err >= 0 && i < 3) |
| 3397 | { |
| 3398 | pipe_err = stdio[i].handle = dup(i); |
| 3399 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3400 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3401 | i++; |
| 3402 | } |
| 3403 | if (pipe_err < 0) |
| 3404 | { |
| 3405 | /* didn't get them all saved - clean up and bail out */ |
| 3406 | int saved_err = errno; |
| 3407 | while (i-- > 0) |
| 3408 | { |
| 3409 | close(stdio[i].handle); |
| 3410 | } |
| 3411 | errno = saved_err; |
| 3412 | return posix_error(); |
| 3413 | } |
| 3414 | |
| 3415 | /* create pipe ends */ |
| 3416 | file_count = 2; |
| 3417 | if (n == POPEN_3) |
| 3418 | file_count = 3; |
| 3419 | i = pipe_err = 0; |
| 3420 | while ((pipe_err == 0) && (i < file_count)) |
| 3421 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3422 | if (pipe_err < 0) |
| 3423 | { |
| 3424 | /* didn't get them all made - clean up and bail out */ |
| 3425 | while (i-- > 0) |
| 3426 | { |
| 3427 | close(p_fd[i].wr); |
| 3428 | close(p_fd[i].rd); |
| 3429 | } |
| 3430 | errno = EPIPE; |
| 3431 | return posix_error(); |
| 3432 | } |
| 3433 | |
| 3434 | /* change the actual standard IO streams over temporarily, |
| 3435 | * making the retained pipe ends non-inheritable |
| 3436 | */ |
| 3437 | pipe_err = 0; |
| 3438 | |
| 3439 | /* - stdin */ |
| 3440 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3441 | { |
| 3442 | close(p_fd[0].rd); |
| 3443 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3444 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3445 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3446 | { |
| 3447 | close(p_fd[0].wr); |
| 3448 | pipe_err = -1; |
| 3449 | } |
| 3450 | } |
| 3451 | else |
| 3452 | { |
| 3453 | pipe_err = -1; |
| 3454 | } |
| 3455 | |
| 3456 | /* - stdout */ |
| 3457 | if (pipe_err == 0) |
| 3458 | { |
| 3459 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3460 | { |
| 3461 | close(p_fd[1].wr); |
| 3462 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3463 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3464 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3465 | { |
| 3466 | close(p_fd[1].rd); |
| 3467 | pipe_err = -1; |
| 3468 | } |
| 3469 | } |
| 3470 | else |
| 3471 | { |
| 3472 | pipe_err = -1; |
| 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | /* - stderr, as required */ |
| 3477 | if (pipe_err == 0) |
| 3478 | switch (n) |
| 3479 | { |
| 3480 | case POPEN_3: |
| 3481 | { |
| 3482 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3483 | { |
| 3484 | close(p_fd[2].wr); |
| 3485 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3486 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3487 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3488 | { |
| 3489 | close(p_fd[2].rd); |
| 3490 | pipe_err = -1; |
| 3491 | } |
| 3492 | } |
| 3493 | else |
| 3494 | { |
| 3495 | pipe_err = -1; |
| 3496 | } |
| 3497 | break; |
| 3498 | } |
| 3499 | |
| 3500 | case POPEN_4: |
| 3501 | { |
| 3502 | if (dup2(1, 2) != 2) |
| 3503 | { |
| 3504 | pipe_err = -1; |
| 3505 | } |
| 3506 | break; |
| 3507 | } |
| 3508 | } |
| 3509 | |
| 3510 | /* spawn the child process */ |
| 3511 | if (pipe_err == 0) |
| 3512 | { |
| 3513 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3514 | if (pipe_pid == -1) |
| 3515 | { |
| 3516 | pipe_err = -1; |
| 3517 | } |
| 3518 | else |
| 3519 | { |
| 3520 | /* save the PID into the FILE structure |
| 3521 | * NOTE: this implementation doesn't actually |
| 3522 | * take advantage of this, but do it for |
| 3523 | * completeness - AIM Apr01 |
| 3524 | */ |
| 3525 | for (i = 0; i < file_count; i++) |
| 3526 | p_s[i]->_pid = pipe_pid; |
| 3527 | } |
| 3528 | } |
| 3529 | |
| 3530 | /* reset standard IO to normal */ |
| 3531 | for (i = 0; i < 3; i++) |
| 3532 | { |
| 3533 | dup2(stdio[i].handle, i); |
| 3534 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3535 | close(stdio[i].handle); |
| 3536 | } |
| 3537 | |
| 3538 | /* if any remnant problems, clean up and bail out */ |
| 3539 | if (pipe_err < 0) |
| 3540 | { |
| 3541 | for (i = 0; i < 3; i++) |
| 3542 | { |
| 3543 | close(p_fd[i].rd); |
| 3544 | close(p_fd[i].wr); |
| 3545 | } |
| 3546 | errno = EPIPE; |
| 3547 | return posix_error_with_filename(cmdstring); |
| 3548 | } |
| 3549 | |
| 3550 | /* build tuple of file objects to return */ |
| 3551 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3552 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3553 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3554 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3555 | if (n == POPEN_3) |
| 3556 | { |
| 3557 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3558 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3559 | f = Py_BuildValue("OOO", p_f[0], p_f[1], p_f[2]); |
| 3560 | } |
| 3561 | else |
| 3562 | f = Py_BuildValue("OO", p_f[0], p_f[1]); |
| 3563 | |
| 3564 | /* |
| 3565 | * Insert the files we've created into the process dictionary |
| 3566 | * all referencing the list with the process handle and the |
| 3567 | * initial number of files (see description below in _PyPclose). |
| 3568 | * Since if _PyPclose later tried to wait on a process when all |
| 3569 | * handles weren't closed, it could create a deadlock with the |
| 3570 | * child, we spend some energy here to try to ensure that we |
| 3571 | * either insert all file handles into the dictionary or none |
| 3572 | * at all. It's a little clumsy with the various popen modes |
| 3573 | * and variable number of files involved. |
| 3574 | */ |
| 3575 | if (!_PyPopenProcs) |
| 3576 | { |
| 3577 | _PyPopenProcs = PyDict_New(); |
| 3578 | } |
| 3579 | |
| 3580 | if (_PyPopenProcs) |
| 3581 | { |
| 3582 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3583 | int ins_rc[3]; |
| 3584 | |
| 3585 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3586 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3587 | |
| 3588 | procObj = PyList_New(2); |
| 3589 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3590 | intObj = PyInt_FromLong((long) file_count); |
| 3591 | |
| 3592 | if (procObj && pidObj && intObj) |
| 3593 | { |
| 3594 | PyList_SetItem(procObj, 0, pidObj); |
| 3595 | PyList_SetItem(procObj, 1, intObj); |
| 3596 | |
| 3597 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3598 | if (fileObj[0]) |
| 3599 | { |
| 3600 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3601 | fileObj[0], |
| 3602 | procObj); |
| 3603 | } |
| 3604 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3605 | if (fileObj[1]) |
| 3606 | { |
| 3607 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3608 | fileObj[1], |
| 3609 | procObj); |
| 3610 | } |
| 3611 | if (file_count >= 3) |
| 3612 | { |
| 3613 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3614 | if (fileObj[2]) |
| 3615 | { |
| 3616 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3617 | fileObj[2], |
| 3618 | procObj); |
| 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3623 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3624 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3625 | { |
| 3626 | /* Something failed - remove any dictionary |
| 3627 | * entries that did make it. |
| 3628 | */ |
| 3629 | if (!ins_rc[0] && fileObj[0]) |
| 3630 | { |
| 3631 | PyDict_DelItem(_PyPopenProcs, |
| 3632 | fileObj[0]); |
| 3633 | } |
| 3634 | if (!ins_rc[1] && fileObj[1]) |
| 3635 | { |
| 3636 | PyDict_DelItem(_PyPopenProcs, |
| 3637 | fileObj[1]); |
| 3638 | } |
| 3639 | if (!ins_rc[2] && fileObj[2]) |
| 3640 | { |
| 3641 | PyDict_DelItem(_PyPopenProcs, |
| 3642 | fileObj[2]); |
| 3643 | } |
| 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | /* |
| 3648 | * Clean up our localized references for the dictionary keys |
| 3649 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3650 | * that got placed in the dictionary. |
| 3651 | */ |
| 3652 | Py_XDECREF(procObj); |
| 3653 | Py_XDECREF(fileObj[0]); |
| 3654 | Py_XDECREF(fileObj[1]); |
| 3655 | Py_XDECREF(fileObj[2]); |
| 3656 | } |
| 3657 | |
| 3658 | /* Child is launched. */ |
| 3659 | return f; |
| 3660 | } |
| 3661 | |
| 3662 | /* |
| 3663 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3664 | * exit code for the child process and return as a result of the close. |
| 3665 | * |
| 3666 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3667 | * input file pointer to information about the process that was |
| 3668 | * originally created by the popen* call that created the file pointer. |
| 3669 | * The dictionary uses the file pointer as a key (with one entry |
| 3670 | * inserted for each file returned by the original popen* call) and a |
| 3671 | * single list object as the value for all files from a single call. |
| 3672 | * The list object contains the Win32 process handle at [0], and a file |
| 3673 | * count at [1], which is initialized to the total number of file |
| 3674 | * handles using that list. |
| 3675 | * |
| 3676 | * This function closes whichever handle it is passed, and decrements |
| 3677 | * the file count in the dictionary for the process handle pointed to |
| 3678 | * by this file. On the last close (when the file count reaches zero), |
| 3679 | * this function will wait for the child process and then return its |
| 3680 | * exit code as the result of the close() operation. This permits the |
| 3681 | * files to be closed in any order - it is always the close() of the |
| 3682 | * final handle that will return the exit code. |
| 3683 | */ |
| 3684 | |
| 3685 | /* RED_FLAG 31-Aug-2000 Tim |
| 3686 | * This is always called (today!) between a pair of |
| 3687 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 3688 | * macros. So the thread running this has no valid thread state, as |
| 3689 | * far as Python is concerned. However, this calls some Python API |
| 3690 | * functions that cannot be called safely without a valid thread |
| 3691 | * state, in particular PyDict_GetItem. |
| 3692 | * As a temporary hack (although it may last for years ...), we |
| 3693 | * *rely* on not having a valid thread state in this function, in |
| 3694 | * order to create our own "from scratch". |
| 3695 | * This will deadlock if _PyPclose is ever called by a thread |
| 3696 | * holding the global lock. |
| 3697 | * (The OS/2 EMX thread support appears to cover the case where the |
| 3698 | * lock is already held - AIM Apr01) |
| 3699 | */ |
| 3700 | |
| 3701 | static int _PyPclose(FILE *file) |
| 3702 | { |
| 3703 | int result; |
| 3704 | int exit_code; |
| 3705 | int pipe_pid; |
| 3706 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 3707 | int file_count; |
| 3708 | #ifdef WITH_THREAD |
| 3709 | PyInterpreterState* pInterpreterState; |
| 3710 | PyThreadState* pThreadState; |
| 3711 | #endif |
| 3712 | |
| 3713 | /* Close the file handle first, to ensure it can't block the |
| 3714 | * child from exiting if it's the last handle. |
| 3715 | */ |
| 3716 | result = fclose(file); |
| 3717 | |
| 3718 | #ifdef WITH_THREAD |
| 3719 | /* Bootstrap a valid thread state into existence. */ |
| 3720 | pInterpreterState = PyInterpreterState_New(); |
| 3721 | if (!pInterpreterState) { |
| 3722 | /* Well, we're hosed now! We don't have a thread |
| 3723 | * state, so can't call a nice error routine, or raise |
| 3724 | * an exception. Just die. |
| 3725 | */ |
| 3726 | Py_FatalError("unable to allocate interpreter state " |
| 3727 | "when closing popen object."); |
| 3728 | return -1; /* unreachable */ |
| 3729 | } |
| 3730 | pThreadState = PyThreadState_New(pInterpreterState); |
| 3731 | if (!pThreadState) { |
| 3732 | Py_FatalError("unable to allocate thread state " |
| 3733 | "when closing popen object."); |
| 3734 | return -1; /* unreachable */ |
| 3735 | } |
| 3736 | /* Grab the global lock. Note that this will deadlock if the |
| 3737 | * current thread already has the lock! (see RED_FLAG comments |
| 3738 | * before this function) |
| 3739 | */ |
| 3740 | PyEval_RestoreThread(pThreadState); |
| 3741 | #endif |
| 3742 | |
| 3743 | if (_PyPopenProcs) |
| 3744 | { |
| 3745 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3746 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3747 | fileObj)) != NULL && |
| 3748 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 3749 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 3750 | { |
| 3751 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 3752 | file_count = (int) PyInt_AsLong(intObj); |
| 3753 | |
| 3754 | if (file_count > 1) |
| 3755 | { |
| 3756 | /* Still other files referencing process */ |
| 3757 | file_count--; |
| 3758 | PyList_SetItem(procObj,1, |
| 3759 | PyInt_FromLong((long) file_count)); |
| 3760 | } |
| 3761 | else |
| 3762 | { |
| 3763 | /* Last file for this process */ |
| 3764 | if (result != EOF && |
| 3765 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 3766 | { |
| 3767 | /* extract exit status */ |
| 3768 | if (WIFEXITED(exit_code)) |
| 3769 | { |
| 3770 | result = WEXITSTATUS(exit_code); |
| 3771 | } |
| 3772 | else |
| 3773 | { |
| 3774 | errno = EPIPE; |
| 3775 | result = -1; |
| 3776 | } |
| 3777 | } |
| 3778 | else |
| 3779 | { |
| 3780 | /* Indicate failure - this will cause the file object |
| 3781 | * to raise an I/O error and translate the last |
| 3782 | * error code from errno. We do have a problem with |
| 3783 | * last errors that overlap the normal errno table, |
| 3784 | * but that's a consistent problem with the file object. |
| 3785 | */ |
| 3786 | result = -1; |
| 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | /* Remove this file pointer from dictionary */ |
| 3791 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3792 | |
| 3793 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 3794 | { |
| 3795 | Py_DECREF(_PyPopenProcs); |
| 3796 | _PyPopenProcs = NULL; |
| 3797 | } |
| 3798 | |
| 3799 | } /* if object retrieval ok */ |
| 3800 | |
| 3801 | Py_XDECREF(fileObj); |
| 3802 | } /* if _PyPopenProcs */ |
| 3803 | |
| 3804 | #ifdef WITH_THREAD |
| 3805 | /* Tear down the thread & interpreter states. |
| 3806 | * Note that interpreter state clear & delete functions automatically |
| 3807 | * call the thread clear & delete functions, and indeed insist on |
| 3808 | * doing that themselves. The lock must be held during the clear, but |
| 3809 | * need not be held during the delete. |
| 3810 | */ |
| 3811 | PyInterpreterState_Clear(pInterpreterState); |
| 3812 | PyEval_ReleaseThread(pThreadState); |
| 3813 | PyInterpreterState_Delete(pInterpreterState); |
| 3814 | #endif |
| 3815 | |
| 3816 | return result; |
| 3817 | } |
| 3818 | |
| 3819 | #endif /* PYCC_??? */ |
| 3820 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3821 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3822 | |
| 3823 | /* |
| 3824 | * Portable 'popen' replacement for Win32. |
| 3825 | * |
| 3826 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3827 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3828 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3829 | */ |
| 3830 | |
| 3831 | #include <malloc.h> |
| 3832 | #include <io.h> |
| 3833 | #include <fcntl.h> |
| 3834 | |
| 3835 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 3836 | #define POPEN_1 1 |
| 3837 | #define POPEN_2 2 |
| 3838 | #define POPEN_3 3 |
| 3839 | #define POPEN_4 4 |
| 3840 | |
| 3841 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3842 | static int _PyPclose(FILE *file); |
| 3843 | |
| 3844 | /* |
| 3845 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3846 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3847 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3848 | */ |
| 3849 | static PyObject *_PyPopenProcs = NULL; |
| 3850 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3851 | |
| 3852 | /* popen that works from a GUI. |
| 3853 | * |
| 3854 | * The result of this function is a pipe (file) connected to the |
| 3855 | * processes stdin or stdout, depending on the requested mode. |
| 3856 | */ |
| 3857 | |
| 3858 | static PyObject * |
| 3859 | posix_popen(PyObject *self, PyObject *args) |
| 3860 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3861 | PyObject *f, *s; |
| 3862 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3863 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3864 | char *cmdstring; |
| 3865 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3866 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3867 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3868 | return NULL; |
| 3869 | |
| 3870 | s = PyTuple_New(0); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3871 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3872 | if (*mode == 'r') |
| 3873 | tm = _O_RDONLY; |
| 3874 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3875 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3876 | return NULL; |
| 3877 | } else |
| 3878 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3879 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3880 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3881 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3882 | return NULL; |
| 3883 | } |
| 3884 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3885 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3886 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3887 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3888 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3889 | else |
| 3890 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 3891 | |
| 3892 | return f; |
| 3893 | } |
| 3894 | |
| 3895 | /* Variation on win32pipe.popen |
| 3896 | * |
| 3897 | * The result of this function is a pipe (file) connected to the |
| 3898 | * process's stdin, and a pipe connected to the process's stdout. |
| 3899 | */ |
| 3900 | |
| 3901 | static PyObject * |
| 3902 | win32_popen2(PyObject *self, PyObject *args) |
| 3903 | { |
| 3904 | PyObject *f; |
| 3905 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3906 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3907 | char *cmdstring; |
| 3908 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3909 | int bufsize = -1; |
| 3910 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3911 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3912 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3913 | if (*mode == 't') |
| 3914 | tm = _O_TEXT; |
| 3915 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3916 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3917 | return NULL; |
| 3918 | } else |
| 3919 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3920 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3921 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3922 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3923 | return NULL; |
| 3924 | } |
| 3925 | |
| 3926 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3927 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3928 | return f; |
| 3929 | } |
| 3930 | |
| 3931 | /* |
| 3932 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3933 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3934 | * The result of this function is 3 pipes - the process's stdin, |
| 3935 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3936 | */ |
| 3937 | |
| 3938 | static PyObject * |
| 3939 | win32_popen3(PyObject *self, PyObject *args) |
| 3940 | { |
| 3941 | PyObject *f; |
| 3942 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3943 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3944 | char *cmdstring; |
| 3945 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3946 | int bufsize = -1; |
| 3947 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3948 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3949 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3950 | if (*mode == 't') |
| 3951 | tm = _O_TEXT; |
| 3952 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3953 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3954 | return NULL; |
| 3955 | } else |
| 3956 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3957 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3958 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3959 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3960 | return NULL; |
| 3961 | } |
| 3962 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3963 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3964 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3965 | return f; |
| 3966 | } |
| 3967 | |
| 3968 | /* |
| 3969 | * Variation on win32pipe.popen |
| 3970 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3971 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3972 | * and stdout+stderr combined as a single pipe. |
| 3973 | */ |
| 3974 | |
| 3975 | static PyObject * |
| 3976 | win32_popen4(PyObject *self, PyObject *args) |
| 3977 | { |
| 3978 | PyObject *f; |
| 3979 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3980 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3981 | char *cmdstring; |
| 3982 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3983 | int bufsize = -1; |
| 3984 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3985 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3986 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3987 | if (*mode == 't') |
| 3988 | tm = _O_TEXT; |
| 3989 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3990 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3991 | return NULL; |
| 3992 | } else |
| 3993 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3994 | |
| 3995 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3996 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3997 | return NULL; |
| 3998 | } |
| 3999 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4000 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4001 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4002 | return f; |
| 4003 | } |
| 4004 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4005 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4006 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4007 | HANDLE hStdin, |
| 4008 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4009 | HANDLE hStderr, |
| 4010 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4011 | { |
| 4012 | PROCESS_INFORMATION piProcInfo; |
| 4013 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4014 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4015 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4016 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4017 | int i; |
| 4018 | int x; |
| 4019 | |
| 4020 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4021 | char *comshell; |
| 4022 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4023 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4024 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 4025 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4026 | |
| 4027 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4028 | * then use the w9xpopen hack. |
| 4029 | */ |
| 4030 | comshell = s1 + x; |
| 4031 | while (comshell >= s1 && *comshell != '\\') |
| 4032 | --comshell; |
| 4033 | ++comshell; |
| 4034 | |
| 4035 | if (GetVersion() < 0x80000000 && |
| 4036 | _stricmp(comshell, "command.com") != 0) { |
| 4037 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4038 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4039 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4040 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4041 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4042 | } |
| 4043 | else { |
| 4044 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4045 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4046 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4047 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4048 | char modulepath[_MAX_PATH]; |
| 4049 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4050 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 4051 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4052 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4053 | x = i+1; |
| 4054 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4055 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4056 | strncat(modulepath, |
| 4057 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4058 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4059 | -strlen(modulepath)); |
| 4060 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4061 | /* Eeek - file-not-found - possibly an embedding |
| 4062 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4063 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4064 | strncpy(modulepath, |
| 4065 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4066 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4067 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4068 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4069 | strncat(modulepath, |
| 4070 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4071 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4072 | -strlen(modulepath)); |
| 4073 | /* No where else to look - raise an easily identifiable |
| 4074 | error, rather than leaving Windows to report |
| 4075 | "file not found" - as the user is probably blissfully |
| 4076 | unaware this shim EXE is used, and it will confuse them. |
| 4077 | (well, it confused me for a while ;-) |
| 4078 | */ |
| 4079 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4080 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4081 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4082 | "for popen to work with your shell " |
| 4083 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4084 | szConsoleSpawn); |
| 4085 | return FALSE; |
| 4086 | } |
| 4087 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4088 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4089 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4090 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4091 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4092 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4093 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4094 | /* To maintain correct argument passing semantics, |
| 4095 | we pass the command-line as it stands, and allow |
| 4096 | quoting to be applied. w9xpopen.exe will then |
| 4097 | use its argv vector, and re-quote the necessary |
| 4098 | args for the ultimate child process. |
| 4099 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4100 | PyOS_snprintf( |
| 4101 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4102 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4103 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4104 | s1, |
| 4105 | s3, |
| 4106 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4107 | /* Not passing CREATE_NEW_CONSOLE has been known to |
| 4108 | cause random failures on win9x. Specifically a |
| 4109 | dialog: |
| 4110 | "Your program accessed mem currently in use at xxx" |
| 4111 | and a hopeful warning about the stability of your |
| 4112 | system. |
| 4113 | Cost is Ctrl+C wont kill children, but anyone |
| 4114 | who cares can have a go! |
| 4115 | */ |
| 4116 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4117 | } |
| 4118 | } |
| 4119 | |
| 4120 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4121 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4122 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4123 | PyErr_SetString(PyExc_RuntimeError, |
| 4124 | "Cannot locate a COMSPEC environment variable to " |
| 4125 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4126 | return FALSE; |
| 4127 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4128 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4129 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4130 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4131 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4132 | siStartInfo.hStdInput = hStdin; |
| 4133 | siStartInfo.hStdOutput = hStdout; |
| 4134 | siStartInfo.hStdError = hStderr; |
| 4135 | siStartInfo.wShowWindow = SW_HIDE; |
| 4136 | |
| 4137 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4138 | s2, |
| 4139 | NULL, |
| 4140 | NULL, |
| 4141 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4142 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4143 | NULL, |
| 4144 | NULL, |
| 4145 | &siStartInfo, |
| 4146 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4147 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4148 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4149 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4150 | /* Return process handle */ |
| 4151 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4152 | return TRUE; |
| 4153 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4154 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4155 | return FALSE; |
| 4156 | } |
| 4157 | |
| 4158 | /* The following code is based off of KB: Q190351 */ |
| 4159 | |
| 4160 | static PyObject * |
| 4161 | _PyPopen(char *cmdstring, int mode, int n) |
| 4162 | { |
| 4163 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4164 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4165 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4166 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4167 | SECURITY_ATTRIBUTES saAttr; |
| 4168 | BOOL fSuccess; |
| 4169 | int fd1, fd2, fd3; |
| 4170 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4171 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4172 | PyObject *f; |
| 4173 | |
| 4174 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4175 | saAttr.bInheritHandle = TRUE; |
| 4176 | saAttr.lpSecurityDescriptor = NULL; |
| 4177 | |
| 4178 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4179 | return win32_error("CreatePipe", NULL); |
| 4180 | |
| 4181 | /* Create new output read handle and the input write handle. Set |
| 4182 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 4183 | * the these handles; resulting in non-closeable handles to the pipes |
| 4184 | * being created. */ |
| 4185 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4186 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4187 | FALSE, |
| 4188 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4189 | if (!fSuccess) |
| 4190 | return win32_error("DuplicateHandle", NULL); |
| 4191 | |
| 4192 | /* Close the inheritable version of ChildStdin |
| 4193 | that we're using. */ |
| 4194 | CloseHandle(hChildStdinWr); |
| 4195 | |
| 4196 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4197 | return win32_error("CreatePipe", NULL); |
| 4198 | |
| 4199 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4200 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4201 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4202 | if (!fSuccess) |
| 4203 | return win32_error("DuplicateHandle", NULL); |
| 4204 | |
| 4205 | /* Close the inheritable version of ChildStdout |
| 4206 | that we're using. */ |
| 4207 | CloseHandle(hChildStdoutRd); |
| 4208 | |
| 4209 | if (n != POPEN_4) { |
| 4210 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4211 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4212 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4213 | hChildStderrRd, |
| 4214 | GetCurrentProcess(), |
| 4215 | &hChildStderrRdDup, 0, |
| 4216 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4217 | if (!fSuccess) |
| 4218 | return win32_error("DuplicateHandle", NULL); |
| 4219 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4220 | CloseHandle(hChildStderrRd); |
| 4221 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4222 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4223 | switch (n) { |
| 4224 | case POPEN_1: |
| 4225 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4226 | case _O_WRONLY | _O_TEXT: |
| 4227 | /* Case for writing to child Stdin in text mode. */ |
| 4228 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4229 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4230 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4231 | PyFile_SetBufSize(f, 0); |
| 4232 | /* We don't care about these pipes anymore, so close them. */ |
| 4233 | CloseHandle(hChildStdoutRdDup); |
| 4234 | CloseHandle(hChildStderrRdDup); |
| 4235 | break; |
| 4236 | |
| 4237 | case _O_RDONLY | _O_TEXT: |
| 4238 | /* Case for reading from child Stdout in text mode. */ |
| 4239 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4240 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4241 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4242 | PyFile_SetBufSize(f, 0); |
| 4243 | /* We don't care about these pipes anymore, so close them. */ |
| 4244 | CloseHandle(hChildStdinWrDup); |
| 4245 | CloseHandle(hChildStderrRdDup); |
| 4246 | break; |
| 4247 | |
| 4248 | case _O_RDONLY | _O_BINARY: |
| 4249 | /* Case for readinig from child Stdout in binary mode. */ |
| 4250 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4251 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4252 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4253 | PyFile_SetBufSize(f, 0); |
| 4254 | /* We don't care about these pipes anymore, so close them. */ |
| 4255 | CloseHandle(hChildStdinWrDup); |
| 4256 | CloseHandle(hChildStderrRdDup); |
| 4257 | break; |
| 4258 | |
| 4259 | case _O_WRONLY | _O_BINARY: |
| 4260 | /* Case for writing to child Stdin in binary mode. */ |
| 4261 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4262 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4263 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4264 | PyFile_SetBufSize(f, 0); |
| 4265 | /* We don't care about these pipes anymore, so close them. */ |
| 4266 | CloseHandle(hChildStdoutRdDup); |
| 4267 | CloseHandle(hChildStderrRdDup); |
| 4268 | break; |
| 4269 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4270 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4271 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4272 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4273 | case POPEN_2: |
| 4274 | case POPEN_4: |
| 4275 | { |
| 4276 | char *m1, *m2; |
| 4277 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4278 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4279 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4280 | m1 = "r"; |
| 4281 | m2 = "w"; |
| 4282 | } else { |
| 4283 | m1 = "rb"; |
| 4284 | m2 = "wb"; |
| 4285 | } |
| 4286 | |
| 4287 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4288 | f1 = _fdopen(fd1, m2); |
| 4289 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4290 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4291 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4292 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4293 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4294 | PyFile_SetBufSize(p2, 0); |
| 4295 | |
| 4296 | if (n != 4) |
| 4297 | CloseHandle(hChildStderrRdDup); |
| 4298 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4299 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4300 | Py_XDECREF(p1); |
| 4301 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4302 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4303 | break; |
| 4304 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4305 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4306 | case POPEN_3: |
| 4307 | { |
| 4308 | char *m1, *m2; |
| 4309 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4310 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4311 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4312 | m1 = "r"; |
| 4313 | m2 = "w"; |
| 4314 | } else { |
| 4315 | m1 = "rb"; |
| 4316 | m2 = "wb"; |
| 4317 | } |
| 4318 | |
| 4319 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4320 | f1 = _fdopen(fd1, m2); |
| 4321 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4322 | f2 = _fdopen(fd2, m1); |
| 4323 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 4324 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4325 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4326 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4327 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4328 | PyFile_SetBufSize(p1, 0); |
| 4329 | PyFile_SetBufSize(p2, 0); |
| 4330 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4331 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4332 | Py_XDECREF(p1); |
| 4333 | Py_XDECREF(p2); |
| 4334 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4335 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4336 | break; |
| 4337 | } |
| 4338 | } |
| 4339 | |
| 4340 | if (n == POPEN_4) { |
| 4341 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4342 | hChildStdinRd, |
| 4343 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4344 | hChildStdoutWr, |
| 4345 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4346 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4347 | } |
| 4348 | else { |
| 4349 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4350 | hChildStdinRd, |
| 4351 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4352 | hChildStderrWr, |
| 4353 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4354 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4357 | /* |
| 4358 | * Insert the files we've created into the process dictionary |
| 4359 | * all referencing the list with the process handle and the |
| 4360 | * initial number of files (see description below in _PyPclose). |
| 4361 | * Since if _PyPclose later tried to wait on a process when all |
| 4362 | * handles weren't closed, it could create a deadlock with the |
| 4363 | * child, we spend some energy here to try to ensure that we |
| 4364 | * either insert all file handles into the dictionary or none |
| 4365 | * at all. It's a little clumsy with the various popen modes |
| 4366 | * and variable number of files involved. |
| 4367 | */ |
| 4368 | if (!_PyPopenProcs) { |
| 4369 | _PyPopenProcs = PyDict_New(); |
| 4370 | } |
| 4371 | |
| 4372 | if (_PyPopenProcs) { |
| 4373 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4374 | int ins_rc[3]; |
| 4375 | |
| 4376 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4377 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4378 | |
| 4379 | procObj = PyList_New(2); |
| 4380 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4381 | intObj = PyInt_FromLong(file_count); |
| 4382 | |
| 4383 | if (procObj && hProcessObj && intObj) { |
| 4384 | PyList_SetItem(procObj,0,hProcessObj); |
| 4385 | PyList_SetItem(procObj,1,intObj); |
| 4386 | |
| 4387 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4388 | if (fileObj[0]) { |
| 4389 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4390 | fileObj[0], |
| 4391 | procObj); |
| 4392 | } |
| 4393 | if (file_count >= 2) { |
| 4394 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4395 | if (fileObj[1]) { |
| 4396 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4397 | fileObj[1], |
| 4398 | procObj); |
| 4399 | } |
| 4400 | } |
| 4401 | if (file_count >= 3) { |
| 4402 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4403 | if (fileObj[2]) { |
| 4404 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4405 | fileObj[2], |
| 4406 | procObj); |
| 4407 | } |
| 4408 | } |
| 4409 | |
| 4410 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4411 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4412 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4413 | /* Something failed - remove any dictionary |
| 4414 | * entries that did make it. |
| 4415 | */ |
| 4416 | if (!ins_rc[0] && fileObj[0]) { |
| 4417 | PyDict_DelItem(_PyPopenProcs, |
| 4418 | fileObj[0]); |
| 4419 | } |
| 4420 | if (!ins_rc[1] && fileObj[1]) { |
| 4421 | PyDict_DelItem(_PyPopenProcs, |
| 4422 | fileObj[1]); |
| 4423 | } |
| 4424 | if (!ins_rc[2] && fileObj[2]) { |
| 4425 | PyDict_DelItem(_PyPopenProcs, |
| 4426 | fileObj[2]); |
| 4427 | } |
| 4428 | } |
| 4429 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4430 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4431 | /* |
| 4432 | * Clean up our localized references for the dictionary keys |
| 4433 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4434 | * that got placed in the dictionary. |
| 4435 | */ |
| 4436 | Py_XDECREF(procObj); |
| 4437 | Py_XDECREF(fileObj[0]); |
| 4438 | Py_XDECREF(fileObj[1]); |
| 4439 | Py_XDECREF(fileObj[2]); |
| 4440 | } |
| 4441 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4442 | /* Child is launched. Close the parents copy of those pipe |
| 4443 | * handles that only the child should have open. You need to |
| 4444 | * make sure that no handles to the write end of the output pipe |
| 4445 | * are maintained in this process or else the pipe will not close |
| 4446 | * when the child process exits and the ReadFile will hang. */ |
| 4447 | |
| 4448 | if (!CloseHandle(hChildStdinRd)) |
| 4449 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4450 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4451 | if (!CloseHandle(hChildStdoutWr)) |
| 4452 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4453 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4454 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4455 | return win32_error("CloseHandle", NULL); |
| 4456 | |
| 4457 | return f; |
| 4458 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4459 | |
| 4460 | /* |
| 4461 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4462 | * 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] | 4463 | * |
| 4464 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4465 | * input file pointer to information about the process that was |
| 4466 | * originally created by the popen* call that created the file pointer. |
| 4467 | * The dictionary uses the file pointer as a key (with one entry |
| 4468 | * inserted for each file returned by the original popen* call) and a |
| 4469 | * single list object as the value for all files from a single call. |
| 4470 | * The list object contains the Win32 process handle at [0], and a file |
| 4471 | * count at [1], which is initialized to the total number of file |
| 4472 | * handles using that list. |
| 4473 | * |
| 4474 | * This function closes whichever handle it is passed, and decrements |
| 4475 | * the file count in the dictionary for the process handle pointed to |
| 4476 | * by this file. On the last close (when the file count reaches zero), |
| 4477 | * this function will wait for the child process and then return its |
| 4478 | * exit code as the result of the close() operation. This permits the |
| 4479 | * files to be closed in any order - it is always the close() of the |
| 4480 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4481 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4482 | |
| 4483 | /* RED_FLAG 31-Aug-2000 Tim |
| 4484 | * This is always called (today!) between a pair of |
| 4485 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 4486 | * macros. So the thread running this has no valid thread state, as |
| 4487 | * far as Python is concerned. However, this calls some Python API |
| 4488 | * functions that cannot be called safely without a valid thread |
| 4489 | * state, in particular PyDict_GetItem. |
| 4490 | * As a temporary hack (although it may last for years ...), we |
| 4491 | * *rely* on not having a valid thread state in this function, in |
| 4492 | * order to create our own "from scratch". |
| 4493 | * This will deadlock if _PyPclose is ever called by a thread |
| 4494 | * holding the global lock. |
| 4495 | */ |
| 4496 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4497 | static int _PyPclose(FILE *file) |
| 4498 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4499 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4500 | DWORD exit_code; |
| 4501 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4502 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4503 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4504 | #ifdef WITH_THREAD |
| 4505 | PyInterpreterState* pInterpreterState; |
| 4506 | PyThreadState* pThreadState; |
| 4507 | #endif |
| 4508 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4509 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4510 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4511 | */ |
| 4512 | result = fclose(file); |
| 4513 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4514 | #ifdef WITH_THREAD |
| 4515 | /* Bootstrap a valid thread state into existence. */ |
| 4516 | pInterpreterState = PyInterpreterState_New(); |
| 4517 | if (!pInterpreterState) { |
| 4518 | /* Well, we're hosed now! We don't have a thread |
| 4519 | * state, so can't call a nice error routine, or raise |
| 4520 | * an exception. Just die. |
| 4521 | */ |
| 4522 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4523 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4524 | return -1; /* unreachable */ |
| 4525 | } |
| 4526 | pThreadState = PyThreadState_New(pInterpreterState); |
| 4527 | if (!pThreadState) { |
| 4528 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4529 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4530 | return -1; /* unreachable */ |
| 4531 | } |
| 4532 | /* Grab the global lock. Note that this will deadlock if the |
| 4533 | * current thread already has the lock! (see RED_FLAG comments |
| 4534 | * before this function) |
| 4535 | */ |
| 4536 | PyEval_RestoreThread(pThreadState); |
| 4537 | #endif |
| 4538 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4539 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4540 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4541 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4542 | fileObj)) != NULL && |
| 4543 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4544 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4545 | |
| 4546 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4547 | file_count = PyInt_AsLong(intObj); |
| 4548 | |
| 4549 | if (file_count > 1) { |
| 4550 | /* Still other files referencing process */ |
| 4551 | file_count--; |
| 4552 | PyList_SetItem(procObj,1, |
| 4553 | PyInt_FromLong(file_count)); |
| 4554 | } else { |
| 4555 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4556 | if (result != EOF && |
| 4557 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4558 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4559 | /* Possible truncation here in 16-bit environments, but |
| 4560 | * real exit codes are just the lower byte in any event. |
| 4561 | */ |
| 4562 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4563 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4564 | /* Indicate failure - this will cause the file object |
| 4565 | * to raise an I/O error and translate the last Win32 |
| 4566 | * error code from errno. We do have a problem with |
| 4567 | * last errors that overlap the normal errno table, |
| 4568 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4569 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4570 | if (result != EOF) { |
| 4571 | /* If the error wasn't from the fclose(), then |
| 4572 | * set errno for the file object error handling. |
| 4573 | */ |
| 4574 | errno = GetLastError(); |
| 4575 | } |
| 4576 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | /* Free up the native handle at this point */ |
| 4580 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4581 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4582 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4583 | /* Remove this file pointer from dictionary */ |
| 4584 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4585 | |
| 4586 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4587 | Py_DECREF(_PyPopenProcs); |
| 4588 | _PyPopenProcs = NULL; |
| 4589 | } |
| 4590 | |
| 4591 | } /* if object retrieval ok */ |
| 4592 | |
| 4593 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4594 | } /* if _PyPopenProcs */ |
| 4595 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4596 | #ifdef WITH_THREAD |
| 4597 | /* Tear down the thread & interpreter states. |
| 4598 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4599 | * call the thread clear & delete functions, and indeed insist on |
| 4600 | * doing that themselves. The lock must be held during the clear, but |
| 4601 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4602 | */ |
| 4603 | PyInterpreterState_Clear(pInterpreterState); |
| 4604 | PyEval_ReleaseThread(pThreadState); |
| 4605 | PyInterpreterState_Delete(pInterpreterState); |
| 4606 | #endif |
| 4607 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4608 | return result; |
| 4609 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4610 | |
| 4611 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4612 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4613 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4614 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4615 | char *name; |
| 4616 | char *mode = "r"; |
| 4617 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4618 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4619 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4620 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4621 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4622 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4623 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4624 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4625 | if (fp == NULL) |
| 4626 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4627 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4628 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4629 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4630 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4631 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4632 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4633 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4634 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4635 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4636 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4637 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4638 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4639 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4640 | Set the current process's user id."); |
| 4641 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4642 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4643 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4644 | { |
| 4645 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4646 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4647 | return NULL; |
| 4648 | if (setuid(uid) < 0) |
| 4649 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4650 | Py_INCREF(Py_None); |
| 4651 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4652 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4653 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4654 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4655 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4656 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4657 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4658 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4659 | Set the current process's effective user id."); |
| 4660 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4661 | static PyObject * |
| 4662 | posix_seteuid (PyObject *self, PyObject *args) |
| 4663 | { |
| 4664 | int euid; |
| 4665 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4666 | return NULL; |
| 4667 | } else if (seteuid(euid) < 0) { |
| 4668 | return posix_error(); |
| 4669 | } else { |
| 4670 | Py_INCREF(Py_None); |
| 4671 | return Py_None; |
| 4672 | } |
| 4673 | } |
| 4674 | #endif /* HAVE_SETEUID */ |
| 4675 | |
| 4676 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4677 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4678 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4679 | Set the current process's effective group id."); |
| 4680 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4681 | static PyObject * |
| 4682 | posix_setegid (PyObject *self, PyObject *args) |
| 4683 | { |
| 4684 | int egid; |
| 4685 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4686 | return NULL; |
| 4687 | } else if (setegid(egid) < 0) { |
| 4688 | return posix_error(); |
| 4689 | } else { |
| 4690 | Py_INCREF(Py_None); |
| 4691 | return Py_None; |
| 4692 | } |
| 4693 | } |
| 4694 | #endif /* HAVE_SETEGID */ |
| 4695 | |
| 4696 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4697 | PyDoc_STRVAR(posix_setreuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4698 | "seteuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4699 | Set the current process's real and effective user ids."); |
| 4700 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4701 | static PyObject * |
| 4702 | posix_setreuid (PyObject *self, PyObject *args) |
| 4703 | { |
| 4704 | int ruid, euid; |
| 4705 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4706 | return NULL; |
| 4707 | } else if (setreuid(ruid, euid) < 0) { |
| 4708 | return posix_error(); |
| 4709 | } else { |
| 4710 | Py_INCREF(Py_None); |
| 4711 | return Py_None; |
| 4712 | } |
| 4713 | } |
| 4714 | #endif /* HAVE_SETREUID */ |
| 4715 | |
| 4716 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4717 | PyDoc_STRVAR(posix_setregid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4718 | "setegid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4719 | Set the current process's real and effective group ids."); |
| 4720 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4721 | static PyObject * |
| 4722 | posix_setregid (PyObject *self, PyObject *args) |
| 4723 | { |
| 4724 | int rgid, egid; |
| 4725 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4726 | return NULL; |
| 4727 | } else if (setregid(rgid, egid) < 0) { |
| 4728 | return posix_error(); |
| 4729 | } else { |
| 4730 | Py_INCREF(Py_None); |
| 4731 | return Py_None; |
| 4732 | } |
| 4733 | } |
| 4734 | #endif /* HAVE_SETREGID */ |
| 4735 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4736 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4737 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4738 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4739 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4740 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4741 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4742 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4743 | { |
| 4744 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4745 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4746 | return NULL; |
| 4747 | if (setgid(gid) < 0) |
| 4748 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4749 | Py_INCREF(Py_None); |
| 4750 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4751 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4752 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4753 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4754 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4755 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4756 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4757 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4758 | |
| 4759 | static PyObject * |
| 4760 | posix_setgroups(PyObject *self, PyObject *args) |
| 4761 | { |
| 4762 | PyObject *groups; |
| 4763 | int i, len; |
| 4764 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4765 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4766 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 4767 | return NULL; |
| 4768 | if (!PySequence_Check(groups)) { |
| 4769 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4770 | return NULL; |
| 4771 | } |
| 4772 | len = PySequence_Size(groups); |
| 4773 | if (len > MAX_GROUPS) { |
| 4774 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4775 | return NULL; |
| 4776 | } |
| 4777 | for(i = 0; i < len; i++) { |
| 4778 | PyObject *elem; |
| 4779 | elem = PySequence_GetItem(groups, i); |
| 4780 | if (!elem) |
| 4781 | return NULL; |
| 4782 | if (!PyInt_Check(elem)) { |
| 4783 | PyErr_SetString(PyExc_TypeError, |
| 4784 | "groups must be integers"); |
| 4785 | Py_DECREF(elem); |
| 4786 | return NULL; |
| 4787 | } |
| 4788 | /* XXX: check that value fits into gid_t. */ |
| 4789 | grouplist[i] = PyInt_AsLong(elem); |
| 4790 | Py_DECREF(elem); |
| 4791 | } |
| 4792 | |
| 4793 | if (setgroups(len, grouplist) < 0) |
| 4794 | return posix_error(); |
| 4795 | Py_INCREF(Py_None); |
| 4796 | return Py_None; |
| 4797 | } |
| 4798 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4799 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4800 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4801 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4802 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4803 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4804 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4805 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4806 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4807 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4808 | int pid, options; |
| 4809 | #ifdef UNION_WAIT |
| 4810 | union wait status; |
| 4811 | #define status_i (status.w_status) |
| 4812 | #else |
| 4813 | int status; |
| 4814 | #define status_i status |
| 4815 | #endif |
| 4816 | status_i = 0; |
| 4817 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4818 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4819 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4820 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4821 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4822 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4823 | if (pid == -1) |
| 4824 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4825 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4826 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4829 | #elif defined(HAVE_CWAIT) |
| 4830 | |
| 4831 | /* 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] | 4832 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4833 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4834 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4835 | |
| 4836 | static PyObject * |
| 4837 | posix_waitpid(PyObject *self, PyObject *args) |
| 4838 | { |
| 4839 | int pid, options; |
| 4840 | int status; |
| 4841 | |
| 4842 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4843 | return NULL; |
| 4844 | Py_BEGIN_ALLOW_THREADS |
| 4845 | pid = _cwait(&status, pid, options); |
| 4846 | Py_END_ALLOW_THREADS |
| 4847 | if (pid == -1) |
| 4848 | return posix_error(); |
| 4849 | else |
| 4850 | /* shift the status left a byte so this is more like the |
| 4851 | POSIX waitpid */ |
| 4852 | return Py_BuildValue("ii", pid, status << 8); |
| 4853 | } |
| 4854 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4855 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4856 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4857 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4858 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4859 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4860 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4861 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4862 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4863 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4864 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4865 | #ifdef UNION_WAIT |
| 4866 | union wait status; |
| 4867 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4868 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4869 | int status; |
| 4870 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4871 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4872 | if (!PyArg_ParseTuple(args, ":wait")) |
| 4873 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4874 | status_i = 0; |
| 4875 | Py_BEGIN_ALLOW_THREADS |
| 4876 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4877 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4878 | if (pid == -1) |
| 4879 | return posix_error(); |
| 4880 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4881 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4882 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4883 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4884 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4885 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4887 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4888 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4889 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4890 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4891 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4892 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4893 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4894 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4895 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4896 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4897 | #ifdef MS_WINDOWS |
| 4898 | return posix_do_stat(self, args, "et:lstat", STAT, "u:lstat", _wstati64); |
| 4899 | #else |
| 4900 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4901 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4902 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4903 | } |
| 4904 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4905 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4906 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4907 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4908 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4909 | 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] | 4910 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4911 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4912 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4913 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4914 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4915 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4916 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4917 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4918 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4919 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4920 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4921 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4922 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4923 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4924 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4925 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4926 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4927 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4928 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4929 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4930 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4931 | "symlink(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4932 | Create a symbolic link."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4933 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4934 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4935 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4936 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4937 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4938 | } |
| 4939 | #endif /* HAVE_SYMLINK */ |
| 4940 | |
| 4941 | |
| 4942 | #ifdef HAVE_TIMES |
| 4943 | #ifndef HZ |
| 4944 | #define HZ 60 /* Universal constant :-) */ |
| 4945 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4946 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4947 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4948 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4949 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4950 | { |
| 4951 | ULONG value = 0; |
| 4952 | |
| 4953 | Py_BEGIN_ALLOW_THREADS |
| 4954 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4955 | Py_END_ALLOW_THREADS |
| 4956 | |
| 4957 | return value; |
| 4958 | } |
| 4959 | |
| 4960 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4961 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4962 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4963 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4964 | return NULL; |
| 4965 | |
| 4966 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4967 | return Py_BuildValue("ddddd", |
| 4968 | (double)0 /* t.tms_utime / HZ */, |
| 4969 | (double)0 /* t.tms_stime / HZ */, |
| 4970 | (double)0 /* t.tms_cutime / HZ */, |
| 4971 | (double)0 /* t.tms_cstime / HZ */, |
| 4972 | (double)system_uptime() / 1000); |
| 4973 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4974 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4975 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4976 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4977 | { |
| 4978 | struct tms t; |
| 4979 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4980 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4981 | return NULL; |
| 4982 | errno = 0; |
| 4983 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4984 | if (c == (clock_t) -1) |
| 4985 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4986 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4987 | (double)t.tms_utime / HZ, |
| 4988 | (double)t.tms_stime / HZ, |
| 4989 | (double)t.tms_cutime / HZ, |
| 4990 | (double)t.tms_cstime / HZ, |
| 4991 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4992 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4993 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4994 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4995 | |
| 4996 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4997 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4998 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4999 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5000 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5001 | { |
| 5002 | FILETIME create, exit, kernel, user; |
| 5003 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5004 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5005 | return NULL; |
| 5006 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5007 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5008 | /* The fields of a FILETIME structure are the hi and lo part |
| 5009 | of a 64-bit value expressed in 100 nanosecond units. |
| 5010 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5011 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5012 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5013 | return Py_BuildValue( |
| 5014 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5015 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5016 | kernel.dwLowDateTime*1e-7), |
| 5017 | (double)(user.dwHighDateTime*429.4967296 + |
| 5018 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5019 | (double)0, |
| 5020 | (double)0, |
| 5021 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5022 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5023 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5024 | |
| 5025 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5026 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5027 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5028 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5029 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5030 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5031 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5032 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5033 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5034 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5035 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5036 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5037 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5038 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5039 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5040 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5041 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5042 | if (setsid() < 0) |
| 5043 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5044 | Py_INCREF(Py_None); |
| 5045 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5046 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5047 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5048 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5049 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5050 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5051 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5052 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5053 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5054 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5055 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5056 | { |
| 5057 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5058 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5059 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5060 | if (setpgid(pid, pgrp) < 0) |
| 5061 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5062 | Py_INCREF(Py_None); |
| 5063 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5064 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5065 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5066 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5067 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5068 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5069 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5070 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5071 | 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] | 5072 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5073 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5074 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5075 | { |
| 5076 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5077 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5078 | return NULL; |
| 5079 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5080 | if (pgid < 0) |
| 5081 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5082 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5083 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5084 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5085 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5086 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5087 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5088 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5089 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5090 | 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] | 5091 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5092 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5093 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5094 | { |
| 5095 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5096 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5097 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5098 | if (tcsetpgrp(fd, pgid) < 0) |
| 5099 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5100 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5101 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5102 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5103 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5104 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5105 | /* Functions acting on file descriptors */ |
| 5106 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5107 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5108 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5109 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5110 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5111 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5112 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5113 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5114 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5115 | int flag; |
| 5116 | int mode = 0777; |
| 5117 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5118 | |
| 5119 | #ifdef MS_WINDOWS |
| 5120 | if (unicode_file_names()) { |
| 5121 | PyUnicodeObject *po; |
| 5122 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5123 | Py_BEGIN_ALLOW_THREADS |
| 5124 | /* PyUnicode_AS_UNICODE OK without thread |
| 5125 | lock as it is a simple dereference. */ |
| 5126 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5127 | Py_END_ALLOW_THREADS |
| 5128 | if (fd < 0) |
| 5129 | return posix_error(); |
| 5130 | return PyInt_FromLong((long)fd); |
| 5131 | } |
| 5132 | /* Drop the argument parsing error as narrow strings |
| 5133 | are also valid. */ |
| 5134 | PyErr_Clear(); |
| 5135 | } |
| 5136 | #endif |
| 5137 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5138 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5139 | Py_FileSystemDefaultEncoding, &file, |
| 5140 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5141 | return NULL; |
| 5142 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5143 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5144 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5145 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5146 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5147 | return posix_error_with_allocated_filename(file); |
| 5148 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5149 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5152 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5153 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5154 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5155 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5156 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5157 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5158 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5159 | { |
| 5160 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5161 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5162 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5163 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5164 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5165 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5166 | if (res < 0) |
| 5167 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5168 | Py_INCREF(Py_None); |
| 5169 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5170 | } |
| 5171 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5172 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5173 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5174 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5175 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5176 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5177 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5178 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5179 | { |
| 5180 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5181 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5182 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5183 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5184 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5185 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5186 | if (fd < 0) |
| 5187 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5188 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5191 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5192 | PyDoc_STRVAR(posix_dup2__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5193 | "dup2(fd, fd2)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5194 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5195 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5196 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5197 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5198 | { |
| 5199 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5200 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5201 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5202 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5203 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5204 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5205 | if (res < 0) |
| 5206 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5207 | Py_INCREF(Py_None); |
| 5208 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5212 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5213 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5214 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5215 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5216 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5217 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5218 | { |
| 5219 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5220 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5221 | LONG_LONG pos, res; |
| 5222 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5223 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5224 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5225 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5226 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5227 | return NULL; |
| 5228 | #ifdef SEEK_SET |
| 5229 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5230 | switch (how) { |
| 5231 | case 0: how = SEEK_SET; break; |
| 5232 | case 1: how = SEEK_CUR; break; |
| 5233 | case 2: how = SEEK_END; break; |
| 5234 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5235 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5236 | |
| 5237 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5238 | pos = PyInt_AsLong(posobj); |
| 5239 | #else |
| 5240 | pos = PyLong_Check(posobj) ? |
| 5241 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5242 | #endif |
| 5243 | if (PyErr_Occurred()) |
| 5244 | return NULL; |
| 5245 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5246 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5247 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5248 | res = _lseeki64(fd, pos, how); |
| 5249 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5250 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5251 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5252 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5253 | if (res < 0) |
| 5254 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5255 | |
| 5256 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5257 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5258 | #else |
| 5259 | return PyLong_FromLongLong(res); |
| 5260 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5261 | } |
| 5262 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5263 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5264 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5265 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5266 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5267 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5268 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5269 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5270 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5271 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5272 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5273 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5274 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5275 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5276 | if (buffer == NULL) |
| 5277 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5278 | Py_BEGIN_ALLOW_THREADS |
| 5279 | n = read(fd, PyString_AsString(buffer), size); |
| 5280 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5281 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5282 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5283 | return posix_error(); |
| 5284 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5285 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5286 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5287 | return buffer; |
| 5288 | } |
| 5289 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5290 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5291 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5292 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5293 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5294 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5295 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5296 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5297 | { |
| 5298 | int fd, size; |
| 5299 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5300 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5301 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5302 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5303 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5304 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5305 | if (size < 0) |
| 5306 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5307 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5310 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5311 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5312 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5313 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5314 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5315 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5316 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5317 | { |
| 5318 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5319 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5320 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5321 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5322 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5323 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5324 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5325 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5326 | if (res != 0) |
| 5327 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5328 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5329 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5332 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5333 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5334 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5335 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5336 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5337 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5338 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5339 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5340 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5341 | char *mode = "r"; |
| 5342 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5343 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5344 | PyObject *f; |
| 5345 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5346 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5347 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5348 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5349 | PyErr_Format(PyExc_ValueError, |
| 5350 | "invalid file mode '%s'", mode); |
| 5351 | return NULL; |
| 5352 | } |
| 5353 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5354 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5355 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5356 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5357 | if (fp == NULL) |
| 5358 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5359 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5360 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5361 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5362 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5365 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5366 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5367 | 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] | 5368 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5369 | |
| 5370 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5371 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5372 | { |
| 5373 | int fd; |
| 5374 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5375 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5376 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5377 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5378 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5379 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5380 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5381 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5382 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5383 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5384 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5385 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5386 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5387 | #if defined(PYOS_OS2) |
| 5388 | HFILE read, write; |
| 5389 | APIRET rc; |
| 5390 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5391 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5392 | return NULL; |
| 5393 | |
| 5394 | Py_BEGIN_ALLOW_THREADS |
| 5395 | rc = DosCreatePipe( &read, &write, 4096); |
| 5396 | Py_END_ALLOW_THREADS |
| 5397 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5398 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5399 | |
| 5400 | return Py_BuildValue("(ii)", read, write); |
| 5401 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5402 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5403 | int fds[2]; |
| 5404 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5405 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5406 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5407 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 5408 | #if defined(__VMS) |
| 5409 | res = pipe(fds,0,2100); /* bigger mailbox quota than 512 */ |
| 5410 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5411 | res = pipe(fds); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 5412 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5413 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5414 | if (res != 0) |
| 5415 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5416 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5417 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5418 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5419 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5420 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5421 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5422 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5423 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5424 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5425 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5426 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5427 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5428 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5429 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5430 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5431 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5432 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5433 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5434 | #endif /* HAVE_PIPE */ |
| 5435 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5436 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5437 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5438 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5439 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5440 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5441 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5442 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5443 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5444 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5445 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5446 | int mode = 0666; |
| 5447 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5448 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5449 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5450 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5451 | res = mkfifo(filename, mode); |
| 5452 | Py_END_ALLOW_THREADS |
| 5453 | if (res < 0) |
| 5454 | return posix_error(); |
| 5455 | Py_INCREF(Py_None); |
| 5456 | return Py_None; |
| 5457 | } |
| 5458 | #endif |
| 5459 | |
| 5460 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5461 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5462 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5463 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5464 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5465 | named filename. mode specifies both the permissions to use and the\n\ |
| 5466 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5467 | 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] | 5468 | device defines the newly created device special file (probably using\n\ |
| 5469 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5470 | |
| 5471 | |
| 5472 | static PyObject * |
| 5473 | posix_mknod(PyObject *self, PyObject *args) |
| 5474 | { |
| 5475 | char *filename; |
| 5476 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5477 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5478 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5479 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5480 | return NULL; |
| 5481 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5482 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5483 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5484 | if (res < 0) |
| 5485 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5486 | Py_INCREF(Py_None); |
| 5487 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5488 | } |
| 5489 | #endif |
| 5490 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5491 | #ifdef HAVE_DEVICE_MACROS |
| 5492 | PyDoc_STRVAR(posix_major__doc__, |
| 5493 | "major(device) -> major number\n\ |
| 5494 | Extracts a device major number from a raw device number."); |
| 5495 | |
| 5496 | static PyObject * |
| 5497 | posix_major(PyObject *self, PyObject *args) |
| 5498 | { |
| 5499 | int device; |
| 5500 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5501 | return NULL; |
| 5502 | return PyInt_FromLong((long)major(device)); |
| 5503 | } |
| 5504 | |
| 5505 | PyDoc_STRVAR(posix_minor__doc__, |
| 5506 | "minor(device) -> minor number\n\ |
| 5507 | Extracts a device minor number from a raw device number."); |
| 5508 | |
| 5509 | static PyObject * |
| 5510 | posix_minor(PyObject *self, PyObject *args) |
| 5511 | { |
| 5512 | int device; |
| 5513 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5514 | return NULL; |
| 5515 | return PyInt_FromLong((long)minor(device)); |
| 5516 | } |
| 5517 | |
| 5518 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5519 | "makedev(major, minor) -> device number\n\ |
| 5520 | Composes a raw device number from the major and minor device numbers."); |
| 5521 | |
| 5522 | static PyObject * |
| 5523 | posix_makedev(PyObject *self, PyObject *args) |
| 5524 | { |
| 5525 | int major, minor; |
| 5526 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5527 | return NULL; |
| 5528 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5529 | } |
| 5530 | #endif /* device macros */ |
| 5531 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5532 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5533 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5534 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5535 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5536 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5537 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5538 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5539 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5540 | { |
| 5541 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5542 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5543 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5544 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5545 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5546 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5547 | return NULL; |
| 5548 | |
| 5549 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5550 | length = PyInt_AsLong(lenobj); |
| 5551 | #else |
| 5552 | length = PyLong_Check(lenobj) ? |
| 5553 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5554 | #endif |
| 5555 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5556 | return NULL; |
| 5557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5558 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5559 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5560 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5561 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5562 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5563 | return NULL; |
| 5564 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5565 | Py_INCREF(Py_None); |
| 5566 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5567 | } |
| 5568 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5569 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5570 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5571 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5572 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5573 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5574 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5575 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5576 | * get re-set with another call for the same key. */ |
| 5577 | static PyObject *posix_putenv_garbage; |
| 5578 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5579 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5580 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5581 | { |
| 5582 | char *s1, *s2; |
| 5583 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5584 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5585 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5586 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5587 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5588 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5589 | |
| 5590 | #if defined(PYOS_OS2) |
| 5591 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5592 | APIRET rc; |
| 5593 | |
| 5594 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 5595 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 5596 | |
| 5597 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5598 | if (rc != NO_ERROR) |
| 5599 | return os2_error(rc); |
| 5600 | |
| 5601 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5602 | APIRET rc; |
| 5603 | |
| 5604 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 5605 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 5606 | |
| 5607 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5608 | if (rc != NO_ERROR) |
| 5609 | return os2_error(rc); |
| 5610 | } else { |
| 5611 | #endif |
| 5612 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5613 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5614 | len = strlen(s1) + strlen(s2) + 2; |
| 5615 | /* len includes space for a trailing \0; the size arg to |
| 5616 | PyString_FromStringAndSize does not count that */ |
| 5617 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5618 | if (newstr == NULL) |
| 5619 | return PyErr_NoMemory(); |
| 5620 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5621 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5622 | if (putenv(new)) { |
| 5623 | posix_error(); |
| 5624 | return NULL; |
| 5625 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5626 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5627 | * this will cause previous value to be collected. This has to |
| 5628 | * happen after the real putenv() call because the old value |
| 5629 | * was still accessible until then. */ |
| 5630 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5631 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5632 | /* really not much we can do; just leak */ |
| 5633 | PyErr_Clear(); |
| 5634 | } |
| 5635 | else { |
| 5636 | Py_DECREF(newstr); |
| 5637 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5638 | |
| 5639 | #if defined(PYOS_OS2) |
| 5640 | } |
| 5641 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5642 | Py_INCREF(Py_None); |
| 5643 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5644 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5645 | #endif /* putenv */ |
| 5646 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5647 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5648 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5649 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5650 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5651 | |
| 5652 | static PyObject * |
| 5653 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5654 | { |
| 5655 | char *s1; |
| 5656 | |
| 5657 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5658 | return NULL; |
| 5659 | |
| 5660 | unsetenv(s1); |
| 5661 | |
| 5662 | /* Remove the key from posix_putenv_garbage; |
| 5663 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5664 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5665 | * old value was still accessible until then. |
| 5666 | */ |
| 5667 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5668 | PyTuple_GET_ITEM(args, 0))) { |
| 5669 | /* really not much we can do; just leak */ |
| 5670 | PyErr_Clear(); |
| 5671 | } |
| 5672 | |
| 5673 | Py_INCREF(Py_None); |
| 5674 | return Py_None; |
| 5675 | } |
| 5676 | #endif /* unsetenv */ |
| 5677 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5678 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5679 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5680 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5681 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5682 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5683 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5684 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5685 | { |
| 5686 | int code; |
| 5687 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5688 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5689 | return NULL; |
| 5690 | message = strerror(code); |
| 5691 | if (message == NULL) { |
| 5692 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5693 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5694 | return NULL; |
| 5695 | } |
| 5696 | return PyString_FromString(message); |
| 5697 | } |
| 5698 | #endif /* strerror */ |
| 5699 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5700 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5701 | #ifdef HAVE_SYS_WAIT_H |
| 5702 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5703 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5704 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5705 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5706 | 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] | 5707 | |
| 5708 | static PyObject * |
| 5709 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5710 | { |
| 5711 | #ifdef UNION_WAIT |
| 5712 | union wait status; |
| 5713 | #define status_i (status.w_status) |
| 5714 | #else |
| 5715 | int status; |
| 5716 | #define status_i status |
| 5717 | #endif |
| 5718 | status_i = 0; |
| 5719 | |
| 5720 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) |
| 5721 | { |
| 5722 | return NULL; |
| 5723 | } |
| 5724 | |
| 5725 | return PyBool_FromLong(WCOREDUMP(status)); |
| 5726 | #undef status_i |
| 5727 | } |
| 5728 | #endif /* WCOREDUMP */ |
| 5729 | |
| 5730 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5731 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5732 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5733 | 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] | 5734 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5735 | |
| 5736 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5737 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5738 | { |
| 5739 | #ifdef UNION_WAIT |
| 5740 | union wait status; |
| 5741 | #define status_i (status.w_status) |
| 5742 | #else |
| 5743 | int status; |
| 5744 | #define status_i status |
| 5745 | #endif |
| 5746 | status_i = 0; |
| 5747 | |
| 5748 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) |
| 5749 | { |
| 5750 | return NULL; |
| 5751 | } |
| 5752 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5753 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5754 | #undef status_i |
| 5755 | } |
| 5756 | #endif /* WIFCONTINUED */ |
| 5757 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5758 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5759 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5760 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5761 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5762 | |
| 5763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5764 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5765 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5766 | #ifdef UNION_WAIT |
| 5767 | union wait status; |
| 5768 | #define status_i (status.w_status) |
| 5769 | #else |
| 5770 | int status; |
| 5771 | #define status_i status |
| 5772 | #endif |
| 5773 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5774 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5775 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5776 | { |
| 5777 | return NULL; |
| 5778 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5779 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5780 | return PyBool_FromLong(WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5781 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5782 | } |
| 5783 | #endif /* WIFSTOPPED */ |
| 5784 | |
| 5785 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5786 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5787 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5788 | 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] | 5789 | |
| 5790 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5791 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5792 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5793 | #ifdef UNION_WAIT |
| 5794 | union wait status; |
| 5795 | #define status_i (status.w_status) |
| 5796 | #else |
| 5797 | int status; |
| 5798 | #define status_i status |
| 5799 | #endif |
| 5800 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5801 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5802 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5803 | { |
| 5804 | return NULL; |
| 5805 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5806 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5807 | return PyBool_FromLong(WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5808 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5809 | } |
| 5810 | #endif /* WIFSIGNALED */ |
| 5811 | |
| 5812 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5813 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5814 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5815 | 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] | 5816 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5817 | |
| 5818 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5819 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5820 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5821 | #ifdef UNION_WAIT |
| 5822 | union wait status; |
| 5823 | #define status_i (status.w_status) |
| 5824 | #else |
| 5825 | int status; |
| 5826 | #define status_i status |
| 5827 | #endif |
| 5828 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5829 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5830 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5831 | { |
| 5832 | return NULL; |
| 5833 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5834 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5835 | return PyBool_FromLong(WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5836 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5837 | } |
| 5838 | #endif /* WIFEXITED */ |
| 5839 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5840 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5841 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5842 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5843 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5844 | |
| 5845 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5846 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5847 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5848 | #ifdef UNION_WAIT |
| 5849 | union wait status; |
| 5850 | #define status_i (status.w_status) |
| 5851 | #else |
| 5852 | int status; |
| 5853 | #define status_i status |
| 5854 | #endif |
| 5855 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5856 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5857 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5858 | { |
| 5859 | return NULL; |
| 5860 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5861 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5862 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5863 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5864 | } |
| 5865 | #endif /* WEXITSTATUS */ |
| 5866 | |
| 5867 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5868 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5869 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5870 | 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] | 5871 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5872 | |
| 5873 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5874 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5875 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5876 | #ifdef UNION_WAIT |
| 5877 | union wait status; |
| 5878 | #define status_i (status.w_status) |
| 5879 | #else |
| 5880 | int status; |
| 5881 | #define status_i status |
| 5882 | #endif |
| 5883 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5884 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5885 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5886 | { |
| 5887 | return NULL; |
| 5888 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5889 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5890 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5891 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5892 | } |
| 5893 | #endif /* WTERMSIG */ |
| 5894 | |
| 5895 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5896 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5897 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5898 | Return the signal that stopped the process that provided\n\ |
| 5899 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5900 | |
| 5901 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5902 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5903 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5904 | #ifdef UNION_WAIT |
| 5905 | union wait status; |
| 5906 | #define status_i (status.w_status) |
| 5907 | #else |
| 5908 | int status; |
| 5909 | #define status_i status |
| 5910 | #endif |
| 5911 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5912 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5913 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5914 | { |
| 5915 | return NULL; |
| 5916 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5917 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5918 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5919 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5920 | } |
| 5921 | #endif /* WSTOPSIG */ |
| 5922 | |
| 5923 | #endif /* HAVE_SYS_WAIT_H */ |
| 5924 | |
| 5925 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5926 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5927 | #ifdef _SCO_DS |
| 5928 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5929 | needed definitions in sys/statvfs.h */ |
| 5930 | #define _SVID3 |
| 5931 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5932 | #include <sys/statvfs.h> |
| 5933 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5934 | static PyObject* |
| 5935 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5936 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5937 | if (v == NULL) |
| 5938 | return NULL; |
| 5939 | |
| 5940 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5941 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5942 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5943 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5944 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5945 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5946 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5947 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5948 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5949 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5950 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5951 | #else |
| 5952 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5953 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5954 | PyStructSequence_SET_ITEM(v, 2, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5955 | PyLong_FromLongLong((LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5956 | PyStructSequence_SET_ITEM(v, 3, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5957 | PyLong_FromLongLong((LONG_LONG) st.f_bfree)); |
| 5958 | PyStructSequence_SET_ITEM(v, 4, |
| 5959 | PyLong_FromLongLong((LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5960 | PyStructSequence_SET_ITEM(v, 5, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5961 | PyLong_FromLongLong((LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5962 | PyStructSequence_SET_ITEM(v, 6, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5963 | PyLong_FromLongLong((LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5964 | PyStructSequence_SET_ITEM(v, 7, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5965 | PyLong_FromLongLong((LONG_LONG) st.f_favail)); |
| 5966 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5967 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5968 | #endif |
| 5969 | |
| 5970 | return v; |
| 5971 | } |
| 5972 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5973 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5974 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5975 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5976 | |
| 5977 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5978 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5979 | { |
| 5980 | int fd, res; |
| 5981 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5982 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5983 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5984 | return NULL; |
| 5985 | Py_BEGIN_ALLOW_THREADS |
| 5986 | res = fstatvfs(fd, &st); |
| 5987 | Py_END_ALLOW_THREADS |
| 5988 | if (res != 0) |
| 5989 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5990 | |
| 5991 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5992 | } |
| 5993 | #endif /* HAVE_FSTATVFS */ |
| 5994 | |
| 5995 | |
| 5996 | #if defined(HAVE_STATVFS) |
| 5997 | #include <sys/statvfs.h> |
| 5998 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5999 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6000 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6001 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6002 | |
| 6003 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6004 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6005 | { |
| 6006 | char *path; |
| 6007 | int res; |
| 6008 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6009 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6010 | return NULL; |
| 6011 | Py_BEGIN_ALLOW_THREADS |
| 6012 | res = statvfs(path, &st); |
| 6013 | Py_END_ALLOW_THREADS |
| 6014 | if (res != 0) |
| 6015 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6016 | |
| 6017 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6018 | } |
| 6019 | #endif /* HAVE_STATVFS */ |
| 6020 | |
| 6021 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6022 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6023 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6024 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6025 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6026 | 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] | 6027 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6028 | |
| 6029 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6030 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6031 | { |
| 6032 | PyObject *result = NULL; |
| 6033 | char *dir = NULL; |
| 6034 | char *pfx = NULL; |
| 6035 | char *name; |
| 6036 | |
| 6037 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6038 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6039 | |
| 6040 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6041 | "tempnam is a potential security risk to your program") < 0) |
| 6042 | return NULL; |
| 6043 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6044 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6045 | name = _tempnam(dir, pfx); |
| 6046 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6047 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6048 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6049 | if (name == NULL) |
| 6050 | return PyErr_NoMemory(); |
| 6051 | result = PyString_FromString(name); |
| 6052 | free(name); |
| 6053 | return result; |
| 6054 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6055 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6056 | |
| 6057 | |
| 6058 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6059 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6060 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6061 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6062 | |
| 6063 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6064 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6065 | { |
| 6066 | FILE *fp; |
| 6067 | |
| 6068 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 6069 | return NULL; |
| 6070 | fp = tmpfile(); |
| 6071 | if (fp == NULL) |
| 6072 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6073 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6074 | } |
| 6075 | #endif |
| 6076 | |
| 6077 | |
| 6078 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6079 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6080 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6081 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6082 | |
| 6083 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6084 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6085 | { |
| 6086 | char buffer[L_tmpnam]; |
| 6087 | char *name; |
| 6088 | |
| 6089 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 6090 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6091 | |
| 6092 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6093 | "tmpnam is a potential security risk to your program") < 0) |
| 6094 | return NULL; |
| 6095 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6096 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6097 | name = tmpnam_r(buffer); |
| 6098 | #else |
| 6099 | name = tmpnam(buffer); |
| 6100 | #endif |
| 6101 | if (name == NULL) { |
| 6102 | PyErr_SetObject(PyExc_OSError, |
| 6103 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6104 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6105 | "unexpected NULL from tmpnam_r" |
| 6106 | #else |
| 6107 | "unexpected NULL from tmpnam" |
| 6108 | #endif |
| 6109 | )); |
| 6110 | return NULL; |
| 6111 | } |
| 6112 | return PyString_FromString(buffer); |
| 6113 | } |
| 6114 | #endif |
| 6115 | |
| 6116 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6117 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6118 | * It maps strings representing configuration variable names to |
| 6119 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6120 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6121 | * rarely-used constants. There are three separate tables that use |
| 6122 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6123 | * |
| 6124 | * This code is always included, even if none of the interfaces that |
| 6125 | * need it are included. The #if hackery needed to avoid it would be |
| 6126 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6127 | */ |
| 6128 | struct constdef { |
| 6129 | char *name; |
| 6130 | long value; |
| 6131 | }; |
| 6132 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6133 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6134 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6135 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6136 | { |
| 6137 | if (PyInt_Check(arg)) { |
| 6138 | *valuep = PyInt_AS_LONG(arg); |
| 6139 | return 1; |
| 6140 | } |
| 6141 | if (PyString_Check(arg)) { |
| 6142 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6143 | size_t lo = 0; |
| 6144 | size_t mid; |
| 6145 | size_t hi = tablesize; |
| 6146 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6147 | char *confname = PyString_AS_STRING(arg); |
| 6148 | while (lo < hi) { |
| 6149 | mid = (lo + hi) / 2; |
| 6150 | cmp = strcmp(confname, table[mid].name); |
| 6151 | if (cmp < 0) |
| 6152 | hi = mid; |
| 6153 | else if (cmp > 0) |
| 6154 | lo = mid + 1; |
| 6155 | else { |
| 6156 | *valuep = table[mid].value; |
| 6157 | return 1; |
| 6158 | } |
| 6159 | } |
| 6160 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6161 | } |
| 6162 | else |
| 6163 | PyErr_SetString(PyExc_TypeError, |
| 6164 | "configuration names must be strings or integers"); |
| 6165 | return 0; |
| 6166 | } |
| 6167 | |
| 6168 | |
| 6169 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6170 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6171 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6172 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6173 | #endif |
| 6174 | #ifdef _PC_ABI_ASYNC_IO |
| 6175 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6176 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6177 | #ifdef _PC_ASYNC_IO |
| 6178 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6179 | #endif |
| 6180 | #ifdef _PC_CHOWN_RESTRICTED |
| 6181 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6182 | #endif |
| 6183 | #ifdef _PC_FILESIZEBITS |
| 6184 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6185 | #endif |
| 6186 | #ifdef _PC_LAST |
| 6187 | {"PC_LAST", _PC_LAST}, |
| 6188 | #endif |
| 6189 | #ifdef _PC_LINK_MAX |
| 6190 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6191 | #endif |
| 6192 | #ifdef _PC_MAX_CANON |
| 6193 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6194 | #endif |
| 6195 | #ifdef _PC_MAX_INPUT |
| 6196 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6197 | #endif |
| 6198 | #ifdef _PC_NAME_MAX |
| 6199 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6200 | #endif |
| 6201 | #ifdef _PC_NO_TRUNC |
| 6202 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6203 | #endif |
| 6204 | #ifdef _PC_PATH_MAX |
| 6205 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6206 | #endif |
| 6207 | #ifdef _PC_PIPE_BUF |
| 6208 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6209 | #endif |
| 6210 | #ifdef _PC_PRIO_IO |
| 6211 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6212 | #endif |
| 6213 | #ifdef _PC_SOCK_MAXBUF |
| 6214 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6215 | #endif |
| 6216 | #ifdef _PC_SYNC_IO |
| 6217 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6218 | #endif |
| 6219 | #ifdef _PC_VDISABLE |
| 6220 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6221 | #endif |
| 6222 | }; |
| 6223 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6224 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6225 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6226 | { |
| 6227 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6228 | sizeof(posix_constants_pathconf) |
| 6229 | / sizeof(struct constdef)); |
| 6230 | } |
| 6231 | #endif |
| 6232 | |
| 6233 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6234 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6235 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6236 | 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] | 6237 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6238 | |
| 6239 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6240 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6241 | { |
| 6242 | PyObject *result = NULL; |
| 6243 | int name, fd; |
| 6244 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6245 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6246 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6247 | long limit; |
| 6248 | |
| 6249 | errno = 0; |
| 6250 | limit = fpathconf(fd, name); |
| 6251 | if (limit == -1 && errno != 0) |
| 6252 | posix_error(); |
| 6253 | else |
| 6254 | result = PyInt_FromLong(limit); |
| 6255 | } |
| 6256 | return result; |
| 6257 | } |
| 6258 | #endif |
| 6259 | |
| 6260 | |
| 6261 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6262 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6263 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6264 | 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] | 6265 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6266 | |
| 6267 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6268 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6269 | { |
| 6270 | PyObject *result = NULL; |
| 6271 | int name; |
| 6272 | char *path; |
| 6273 | |
| 6274 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6275 | conv_path_confname, &name)) { |
| 6276 | long limit; |
| 6277 | |
| 6278 | errno = 0; |
| 6279 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6280 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6281 | if (errno == EINVAL) |
| 6282 | /* could be a path or name problem */ |
| 6283 | posix_error(); |
| 6284 | else |
| 6285 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6286 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6287 | else |
| 6288 | result = PyInt_FromLong(limit); |
| 6289 | } |
| 6290 | return result; |
| 6291 | } |
| 6292 | #endif |
| 6293 | |
| 6294 | #ifdef HAVE_CONFSTR |
| 6295 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6296 | #ifdef _CS_ARCHITECTURE |
| 6297 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6298 | #endif |
| 6299 | #ifdef _CS_HOSTNAME |
| 6300 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6301 | #endif |
| 6302 | #ifdef _CS_HW_PROVIDER |
| 6303 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6304 | #endif |
| 6305 | #ifdef _CS_HW_SERIAL |
| 6306 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6307 | #endif |
| 6308 | #ifdef _CS_INITTAB_NAME |
| 6309 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6310 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6311 | #ifdef _CS_LFS64_CFLAGS |
| 6312 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6313 | #endif |
| 6314 | #ifdef _CS_LFS64_LDFLAGS |
| 6315 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6316 | #endif |
| 6317 | #ifdef _CS_LFS64_LIBS |
| 6318 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6319 | #endif |
| 6320 | #ifdef _CS_LFS64_LINTFLAGS |
| 6321 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6322 | #endif |
| 6323 | #ifdef _CS_LFS_CFLAGS |
| 6324 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6325 | #endif |
| 6326 | #ifdef _CS_LFS_LDFLAGS |
| 6327 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6328 | #endif |
| 6329 | #ifdef _CS_LFS_LIBS |
| 6330 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6331 | #endif |
| 6332 | #ifdef _CS_LFS_LINTFLAGS |
| 6333 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6334 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6335 | #ifdef _CS_MACHINE |
| 6336 | {"CS_MACHINE", _CS_MACHINE}, |
| 6337 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6338 | #ifdef _CS_PATH |
| 6339 | {"CS_PATH", _CS_PATH}, |
| 6340 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6341 | #ifdef _CS_RELEASE |
| 6342 | {"CS_RELEASE", _CS_RELEASE}, |
| 6343 | #endif |
| 6344 | #ifdef _CS_SRPC_DOMAIN |
| 6345 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6346 | #endif |
| 6347 | #ifdef _CS_SYSNAME |
| 6348 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6349 | #endif |
| 6350 | #ifdef _CS_VERSION |
| 6351 | {"CS_VERSION", _CS_VERSION}, |
| 6352 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6353 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6354 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6355 | #endif |
| 6356 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6357 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6358 | #endif |
| 6359 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6360 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6361 | #endif |
| 6362 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6363 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6364 | #endif |
| 6365 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6366 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6367 | #endif |
| 6368 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6369 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6370 | #endif |
| 6371 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6372 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6373 | #endif |
| 6374 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6375 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6376 | #endif |
| 6377 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6378 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6379 | #endif |
| 6380 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6381 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6382 | #endif |
| 6383 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6384 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6385 | #endif |
| 6386 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6387 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6388 | #endif |
| 6389 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6390 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6391 | #endif |
| 6392 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6393 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6394 | #endif |
| 6395 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6396 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6397 | #endif |
| 6398 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6399 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6400 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6401 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6402 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6403 | #endif |
| 6404 | #ifdef _MIPS_CS_BASE |
| 6405 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6406 | #endif |
| 6407 | #ifdef _MIPS_CS_HOSTID |
| 6408 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6409 | #endif |
| 6410 | #ifdef _MIPS_CS_HW_NAME |
| 6411 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6412 | #endif |
| 6413 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6414 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6415 | #endif |
| 6416 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6417 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6418 | #endif |
| 6419 | #ifdef _MIPS_CS_OSREL_MIN |
| 6420 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6421 | #endif |
| 6422 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6423 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6424 | #endif |
| 6425 | #ifdef _MIPS_CS_OS_NAME |
| 6426 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6427 | #endif |
| 6428 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6429 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6430 | #endif |
| 6431 | #ifdef _MIPS_CS_PROCESSORS |
| 6432 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6433 | #endif |
| 6434 | #ifdef _MIPS_CS_SERIAL |
| 6435 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6436 | #endif |
| 6437 | #ifdef _MIPS_CS_VENDOR |
| 6438 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6439 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6440 | }; |
| 6441 | |
| 6442 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6443 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6444 | { |
| 6445 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6446 | sizeof(posix_constants_confstr) |
| 6447 | / sizeof(struct constdef)); |
| 6448 | } |
| 6449 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6450 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6451 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6452 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6453 | |
| 6454 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6455 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6456 | { |
| 6457 | PyObject *result = NULL; |
| 6458 | int name; |
| 6459 | char buffer[64]; |
| 6460 | |
| 6461 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6462 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6463 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6464 | errno = 0; |
| 6465 | if (len == 0) { |
| 6466 | if (errno != 0) |
| 6467 | posix_error(); |
| 6468 | else |
| 6469 | result = PyString_FromString(""); |
| 6470 | } |
| 6471 | else { |
| 6472 | if (len >= sizeof(buffer)) { |
| 6473 | result = PyString_FromStringAndSize(NULL, len); |
| 6474 | if (result != NULL) |
| 6475 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6476 | } |
| 6477 | else |
| 6478 | result = PyString_FromString(buffer); |
| 6479 | } |
| 6480 | } |
| 6481 | return result; |
| 6482 | } |
| 6483 | #endif |
| 6484 | |
| 6485 | |
| 6486 | #ifdef HAVE_SYSCONF |
| 6487 | static struct constdef posix_constants_sysconf[] = { |
| 6488 | #ifdef _SC_2_CHAR_TERM |
| 6489 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6490 | #endif |
| 6491 | #ifdef _SC_2_C_BIND |
| 6492 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6493 | #endif |
| 6494 | #ifdef _SC_2_C_DEV |
| 6495 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6496 | #endif |
| 6497 | #ifdef _SC_2_C_VERSION |
| 6498 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6499 | #endif |
| 6500 | #ifdef _SC_2_FORT_DEV |
| 6501 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6502 | #endif |
| 6503 | #ifdef _SC_2_FORT_RUN |
| 6504 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6505 | #endif |
| 6506 | #ifdef _SC_2_LOCALEDEF |
| 6507 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6508 | #endif |
| 6509 | #ifdef _SC_2_SW_DEV |
| 6510 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6511 | #endif |
| 6512 | #ifdef _SC_2_UPE |
| 6513 | {"SC_2_UPE", _SC_2_UPE}, |
| 6514 | #endif |
| 6515 | #ifdef _SC_2_VERSION |
| 6516 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6517 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6518 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6519 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6520 | #endif |
| 6521 | #ifdef _SC_ACL |
| 6522 | {"SC_ACL", _SC_ACL}, |
| 6523 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6524 | #ifdef _SC_AIO_LISTIO_MAX |
| 6525 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6526 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6527 | #ifdef _SC_AIO_MAX |
| 6528 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6529 | #endif |
| 6530 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6531 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6532 | #endif |
| 6533 | #ifdef _SC_ARG_MAX |
| 6534 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6535 | #endif |
| 6536 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6537 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6538 | #endif |
| 6539 | #ifdef _SC_ATEXIT_MAX |
| 6540 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6541 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6542 | #ifdef _SC_AUDIT |
| 6543 | {"SC_AUDIT", _SC_AUDIT}, |
| 6544 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6545 | #ifdef _SC_AVPHYS_PAGES |
| 6546 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6547 | #endif |
| 6548 | #ifdef _SC_BC_BASE_MAX |
| 6549 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6550 | #endif |
| 6551 | #ifdef _SC_BC_DIM_MAX |
| 6552 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6553 | #endif |
| 6554 | #ifdef _SC_BC_SCALE_MAX |
| 6555 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6556 | #endif |
| 6557 | #ifdef _SC_BC_STRING_MAX |
| 6558 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6559 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6560 | #ifdef _SC_CAP |
| 6561 | {"SC_CAP", _SC_CAP}, |
| 6562 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6563 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6564 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6565 | #endif |
| 6566 | #ifdef _SC_CHAR_BIT |
| 6567 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6568 | #endif |
| 6569 | #ifdef _SC_CHAR_MAX |
| 6570 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6571 | #endif |
| 6572 | #ifdef _SC_CHAR_MIN |
| 6573 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6574 | #endif |
| 6575 | #ifdef _SC_CHILD_MAX |
| 6576 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6577 | #endif |
| 6578 | #ifdef _SC_CLK_TCK |
| 6579 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6580 | #endif |
| 6581 | #ifdef _SC_COHER_BLKSZ |
| 6582 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6583 | #endif |
| 6584 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6585 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6586 | #endif |
| 6587 | #ifdef _SC_DCACHE_ASSOC |
| 6588 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6589 | #endif |
| 6590 | #ifdef _SC_DCACHE_BLKSZ |
| 6591 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6592 | #endif |
| 6593 | #ifdef _SC_DCACHE_LINESZ |
| 6594 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6595 | #endif |
| 6596 | #ifdef _SC_DCACHE_SZ |
| 6597 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6598 | #endif |
| 6599 | #ifdef _SC_DCACHE_TBLKSZ |
| 6600 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6601 | #endif |
| 6602 | #ifdef _SC_DELAYTIMER_MAX |
| 6603 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6604 | #endif |
| 6605 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6606 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6607 | #endif |
| 6608 | #ifdef _SC_EXPR_NEST_MAX |
| 6609 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6610 | #endif |
| 6611 | #ifdef _SC_FSYNC |
| 6612 | {"SC_FSYNC", _SC_FSYNC}, |
| 6613 | #endif |
| 6614 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6615 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6616 | #endif |
| 6617 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6618 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6619 | #endif |
| 6620 | #ifdef _SC_ICACHE_ASSOC |
| 6621 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6622 | #endif |
| 6623 | #ifdef _SC_ICACHE_BLKSZ |
| 6624 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6625 | #endif |
| 6626 | #ifdef _SC_ICACHE_LINESZ |
| 6627 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6628 | #endif |
| 6629 | #ifdef _SC_ICACHE_SZ |
| 6630 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6631 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6632 | #ifdef _SC_INF |
| 6633 | {"SC_INF", _SC_INF}, |
| 6634 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6635 | #ifdef _SC_INT_MAX |
| 6636 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6637 | #endif |
| 6638 | #ifdef _SC_INT_MIN |
| 6639 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6640 | #endif |
| 6641 | #ifdef _SC_IOV_MAX |
| 6642 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6643 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6644 | #ifdef _SC_IP_SECOPTS |
| 6645 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6646 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6647 | #ifdef _SC_JOB_CONTROL |
| 6648 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6649 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6650 | #ifdef _SC_KERN_POINTERS |
| 6651 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6652 | #endif |
| 6653 | #ifdef _SC_KERN_SIM |
| 6654 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6655 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6656 | #ifdef _SC_LINE_MAX |
| 6657 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6658 | #endif |
| 6659 | #ifdef _SC_LOGIN_NAME_MAX |
| 6660 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6661 | #endif |
| 6662 | #ifdef _SC_LOGNAME_MAX |
| 6663 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6664 | #endif |
| 6665 | #ifdef _SC_LONG_BIT |
| 6666 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6667 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6668 | #ifdef _SC_MAC |
| 6669 | {"SC_MAC", _SC_MAC}, |
| 6670 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6671 | #ifdef _SC_MAPPED_FILES |
| 6672 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6673 | #endif |
| 6674 | #ifdef _SC_MAXPID |
| 6675 | {"SC_MAXPID", _SC_MAXPID}, |
| 6676 | #endif |
| 6677 | #ifdef _SC_MB_LEN_MAX |
| 6678 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6679 | #endif |
| 6680 | #ifdef _SC_MEMLOCK |
| 6681 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6682 | #endif |
| 6683 | #ifdef _SC_MEMLOCK_RANGE |
| 6684 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6685 | #endif |
| 6686 | #ifdef _SC_MEMORY_PROTECTION |
| 6687 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6688 | #endif |
| 6689 | #ifdef _SC_MESSAGE_PASSING |
| 6690 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6691 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6692 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6693 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6694 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6695 | #ifdef _SC_MQ_OPEN_MAX |
| 6696 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6697 | #endif |
| 6698 | #ifdef _SC_MQ_PRIO_MAX |
| 6699 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6700 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6701 | #ifdef _SC_NACLS_MAX |
| 6702 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6703 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6704 | #ifdef _SC_NGROUPS_MAX |
| 6705 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6706 | #endif |
| 6707 | #ifdef _SC_NL_ARGMAX |
| 6708 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6709 | #endif |
| 6710 | #ifdef _SC_NL_LANGMAX |
| 6711 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6712 | #endif |
| 6713 | #ifdef _SC_NL_MSGMAX |
| 6714 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6715 | #endif |
| 6716 | #ifdef _SC_NL_NMAX |
| 6717 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6718 | #endif |
| 6719 | #ifdef _SC_NL_SETMAX |
| 6720 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6721 | #endif |
| 6722 | #ifdef _SC_NL_TEXTMAX |
| 6723 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6724 | #endif |
| 6725 | #ifdef _SC_NPROCESSORS_CONF |
| 6726 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6727 | #endif |
| 6728 | #ifdef _SC_NPROCESSORS_ONLN |
| 6729 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6730 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6731 | #ifdef _SC_NPROC_CONF |
| 6732 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6733 | #endif |
| 6734 | #ifdef _SC_NPROC_ONLN |
| 6735 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6736 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6737 | #ifdef _SC_NZERO |
| 6738 | {"SC_NZERO", _SC_NZERO}, |
| 6739 | #endif |
| 6740 | #ifdef _SC_OPEN_MAX |
| 6741 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6742 | #endif |
| 6743 | #ifdef _SC_PAGESIZE |
| 6744 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6745 | #endif |
| 6746 | #ifdef _SC_PAGE_SIZE |
| 6747 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6748 | #endif |
| 6749 | #ifdef _SC_PASS_MAX |
| 6750 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6751 | #endif |
| 6752 | #ifdef _SC_PHYS_PAGES |
| 6753 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6754 | #endif |
| 6755 | #ifdef _SC_PII |
| 6756 | {"SC_PII", _SC_PII}, |
| 6757 | #endif |
| 6758 | #ifdef _SC_PII_INTERNET |
| 6759 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6760 | #endif |
| 6761 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6762 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6763 | #endif |
| 6764 | #ifdef _SC_PII_INTERNET_STREAM |
| 6765 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6766 | #endif |
| 6767 | #ifdef _SC_PII_OSI |
| 6768 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6769 | #endif |
| 6770 | #ifdef _SC_PII_OSI_CLTS |
| 6771 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6772 | #endif |
| 6773 | #ifdef _SC_PII_OSI_COTS |
| 6774 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6775 | #endif |
| 6776 | #ifdef _SC_PII_OSI_M |
| 6777 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6778 | #endif |
| 6779 | #ifdef _SC_PII_SOCKET |
| 6780 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6781 | #endif |
| 6782 | #ifdef _SC_PII_XTI |
| 6783 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6784 | #endif |
| 6785 | #ifdef _SC_POLL |
| 6786 | {"SC_POLL", _SC_POLL}, |
| 6787 | #endif |
| 6788 | #ifdef _SC_PRIORITIZED_IO |
| 6789 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6790 | #endif |
| 6791 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6792 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6793 | #endif |
| 6794 | #ifdef _SC_REALTIME_SIGNALS |
| 6795 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6796 | #endif |
| 6797 | #ifdef _SC_RE_DUP_MAX |
| 6798 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6799 | #endif |
| 6800 | #ifdef _SC_RTSIG_MAX |
| 6801 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6802 | #endif |
| 6803 | #ifdef _SC_SAVED_IDS |
| 6804 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6805 | #endif |
| 6806 | #ifdef _SC_SCHAR_MAX |
| 6807 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6808 | #endif |
| 6809 | #ifdef _SC_SCHAR_MIN |
| 6810 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6811 | #endif |
| 6812 | #ifdef _SC_SELECT |
| 6813 | {"SC_SELECT", _SC_SELECT}, |
| 6814 | #endif |
| 6815 | #ifdef _SC_SEMAPHORES |
| 6816 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6817 | #endif |
| 6818 | #ifdef _SC_SEM_NSEMS_MAX |
| 6819 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6820 | #endif |
| 6821 | #ifdef _SC_SEM_VALUE_MAX |
| 6822 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6823 | #endif |
| 6824 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6825 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6826 | #endif |
| 6827 | #ifdef _SC_SHRT_MAX |
| 6828 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6829 | #endif |
| 6830 | #ifdef _SC_SHRT_MIN |
| 6831 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6832 | #endif |
| 6833 | #ifdef _SC_SIGQUEUE_MAX |
| 6834 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6835 | #endif |
| 6836 | #ifdef _SC_SIGRT_MAX |
| 6837 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6838 | #endif |
| 6839 | #ifdef _SC_SIGRT_MIN |
| 6840 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6841 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6842 | #ifdef _SC_SOFTPOWER |
| 6843 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6844 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6845 | #ifdef _SC_SPLIT_CACHE |
| 6846 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6847 | #endif |
| 6848 | #ifdef _SC_SSIZE_MAX |
| 6849 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6850 | #endif |
| 6851 | #ifdef _SC_STACK_PROT |
| 6852 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6853 | #endif |
| 6854 | #ifdef _SC_STREAM_MAX |
| 6855 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6856 | #endif |
| 6857 | #ifdef _SC_SYNCHRONIZED_IO |
| 6858 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6859 | #endif |
| 6860 | #ifdef _SC_THREADS |
| 6861 | {"SC_THREADS", _SC_THREADS}, |
| 6862 | #endif |
| 6863 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6864 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6865 | #endif |
| 6866 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6867 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6868 | #endif |
| 6869 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6870 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6871 | #endif |
| 6872 | #ifdef _SC_THREAD_KEYS_MAX |
| 6873 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6874 | #endif |
| 6875 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6876 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6877 | #endif |
| 6878 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6879 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6880 | #endif |
| 6881 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6882 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6883 | #endif |
| 6884 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6885 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6886 | #endif |
| 6887 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6888 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6889 | #endif |
| 6890 | #ifdef _SC_THREAD_STACK_MIN |
| 6891 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6892 | #endif |
| 6893 | #ifdef _SC_THREAD_THREADS_MAX |
| 6894 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6895 | #endif |
| 6896 | #ifdef _SC_TIMERS |
| 6897 | {"SC_TIMERS", _SC_TIMERS}, |
| 6898 | #endif |
| 6899 | #ifdef _SC_TIMER_MAX |
| 6900 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6901 | #endif |
| 6902 | #ifdef _SC_TTY_NAME_MAX |
| 6903 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6904 | #endif |
| 6905 | #ifdef _SC_TZNAME_MAX |
| 6906 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6907 | #endif |
| 6908 | #ifdef _SC_T_IOV_MAX |
| 6909 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6910 | #endif |
| 6911 | #ifdef _SC_UCHAR_MAX |
| 6912 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6913 | #endif |
| 6914 | #ifdef _SC_UINT_MAX |
| 6915 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6916 | #endif |
| 6917 | #ifdef _SC_UIO_MAXIOV |
| 6918 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6919 | #endif |
| 6920 | #ifdef _SC_ULONG_MAX |
| 6921 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6922 | #endif |
| 6923 | #ifdef _SC_USHRT_MAX |
| 6924 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6925 | #endif |
| 6926 | #ifdef _SC_VERSION |
| 6927 | {"SC_VERSION", _SC_VERSION}, |
| 6928 | #endif |
| 6929 | #ifdef _SC_WORD_BIT |
| 6930 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6931 | #endif |
| 6932 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6933 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6934 | #endif |
| 6935 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6936 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6937 | #endif |
| 6938 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6939 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6940 | #endif |
| 6941 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6942 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6943 | #endif |
| 6944 | #ifdef _SC_XOPEN_CRYPT |
| 6945 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6946 | #endif |
| 6947 | #ifdef _SC_XOPEN_ENH_I18N |
| 6948 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6949 | #endif |
| 6950 | #ifdef _SC_XOPEN_LEGACY |
| 6951 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6952 | #endif |
| 6953 | #ifdef _SC_XOPEN_REALTIME |
| 6954 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6955 | #endif |
| 6956 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6957 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6958 | #endif |
| 6959 | #ifdef _SC_XOPEN_SHM |
| 6960 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6961 | #endif |
| 6962 | #ifdef _SC_XOPEN_UNIX |
| 6963 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6964 | #endif |
| 6965 | #ifdef _SC_XOPEN_VERSION |
| 6966 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6967 | #endif |
| 6968 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6969 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6970 | #endif |
| 6971 | #ifdef _SC_XOPEN_XPG2 |
| 6972 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6973 | #endif |
| 6974 | #ifdef _SC_XOPEN_XPG3 |
| 6975 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6976 | #endif |
| 6977 | #ifdef _SC_XOPEN_XPG4 |
| 6978 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6979 | #endif |
| 6980 | }; |
| 6981 | |
| 6982 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6983 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6984 | { |
| 6985 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6986 | sizeof(posix_constants_sysconf) |
| 6987 | / sizeof(struct constdef)); |
| 6988 | } |
| 6989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6990 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6991 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6992 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6993 | |
| 6994 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6995 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6996 | { |
| 6997 | PyObject *result = NULL; |
| 6998 | int name; |
| 6999 | |
| 7000 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7001 | int value; |
| 7002 | |
| 7003 | errno = 0; |
| 7004 | value = sysconf(name); |
| 7005 | if (value == -1 && errno != 0) |
| 7006 | posix_error(); |
| 7007 | else |
| 7008 | result = PyInt_FromLong(value); |
| 7009 | } |
| 7010 | return result; |
| 7011 | } |
| 7012 | #endif |
| 7013 | |
| 7014 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7015 | /* This code is used to ensure that the tables of configuration value names |
| 7016 | * are in sorted order as required by conv_confname(), and also to build the |
| 7017 | * the exported dictionaries that are used to publish information about the |
| 7018 | * names available on the host platform. |
| 7019 | * |
| 7020 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7021 | * when used, even for platforms we're not able to test on. It also makes |
| 7022 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7023 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7024 | |
| 7025 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7026 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7027 | { |
| 7028 | const struct constdef *c1 = |
| 7029 | (const struct constdef *) v1; |
| 7030 | const struct constdef *c2 = |
| 7031 | (const struct constdef *) v2; |
| 7032 | |
| 7033 | return strcmp(c1->name, c2->name); |
| 7034 | } |
| 7035 | |
| 7036 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7037 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7038 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7039 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7040 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7041 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7042 | |
| 7043 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7044 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7045 | if (d == NULL) |
| 7046 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7047 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7048 | for (i=0; i < tablesize; ++i) { |
| 7049 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7050 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7051 | Py_XDECREF(o); |
| 7052 | Py_DECREF(d); |
| 7053 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7054 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7055 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7056 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7057 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7058 | } |
| 7059 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7060 | /* Return -1 on failure, 0 on success. */ |
| 7061 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7062 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7063 | { |
| 7064 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7065 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7066 | sizeof(posix_constants_pathconf) |
| 7067 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7068 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7069 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7070 | #endif |
| 7071 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7072 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7073 | sizeof(posix_constants_confstr) |
| 7074 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7075 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7076 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7077 | #endif |
| 7078 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7079 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7080 | sizeof(posix_constants_sysconf) |
| 7081 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7082 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7083 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7084 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7085 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7086 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7087 | |
| 7088 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7089 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7090 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7091 | 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] | 7092 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7093 | |
| 7094 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7095 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7096 | { |
| 7097 | if (!PyArg_ParseTuple(args, ":abort")) |
| 7098 | return NULL; |
| 7099 | abort(); |
| 7100 | /*NOTREACHED*/ |
| 7101 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7102 | return NULL; |
| 7103 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7104 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7105 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7106 | PyDoc_STRVAR(win32_startfile__doc__, |
| 7107 | "startfile(filepath) - Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7108 | \n\ |
| 7109 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 7110 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 7111 | with whatever application (if any) its extension is associated.\n\ |
| 7112 | \n\ |
| 7113 | startfile returns as soon as the associated application is launched.\n\ |
| 7114 | There is no option to wait for the application to close, and no way\n\ |
| 7115 | to retrieve the application's exit status.\n\ |
| 7116 | \n\ |
| 7117 | The filepath is relative to the current directory. If you want to use\n\ |
| 7118 | 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] | 7119 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7120 | |
| 7121 | static PyObject * |
| 7122 | win32_startfile(PyObject *self, PyObject *args) |
| 7123 | { |
| 7124 | char *filepath; |
| 7125 | HINSTANCE rc; |
| 7126 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 7127 | return NULL; |
| 7128 | Py_BEGIN_ALLOW_THREADS |
| 7129 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 7130 | Py_END_ALLOW_THREADS |
| 7131 | if (rc <= (HINSTANCE)32) |
| 7132 | return win32_error("startfile", filepath); |
| 7133 | Py_INCREF(Py_None); |
| 7134 | return Py_None; |
| 7135 | } |
| 7136 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7137 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7138 | #ifdef HAVE_GETLOADAVG |
| 7139 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7140 | "getloadavg() -> (float, float, float)\n\n\ |
| 7141 | Return the number of processes in the system run queue averaged over\n\ |
| 7142 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7143 | was unobtainable"); |
| 7144 | |
| 7145 | static PyObject * |
| 7146 | posix_getloadavg(PyObject *self, PyObject *args) |
| 7147 | { |
| 7148 | double loadavg[3]; |
| 7149 | if (!PyArg_ParseTuple(args, ":getloadavg")) |
| 7150 | return NULL; |
| 7151 | if (getloadavg(loadavg, 3)!=3) { |
| 7152 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7153 | return NULL; |
| 7154 | } else |
| 7155 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7156 | } |
| 7157 | #endif |
| 7158 | |
| 7159 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7160 | static PyMethodDef posix_methods[] = { |
| 7161 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7162 | #ifdef HAVE_TTYNAME |
| 7163 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7164 | #endif |
| 7165 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7166 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7167 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7168 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7169 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7170 | #ifdef HAVE_LCHOWN |
| 7171 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7172 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7173 | #ifdef HAVE_CHROOT |
| 7174 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7175 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7176 | #ifdef HAVE_CTERMID |
| 7177 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 7178 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7179 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7180 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7181 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7182 | {"getcwdu", posix_getcwdu, METH_VARARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7183 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7184 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7185 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7186 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7187 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7188 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7189 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7190 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7191 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7192 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7193 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7194 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7195 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7196 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7197 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7198 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7199 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7200 | {"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] | 7201 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7202 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7203 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7204 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7205 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7206 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7207 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7208 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7209 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7210 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7211 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7212 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7213 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7214 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7215 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7216 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7217 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7218 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7219 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7220 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7221 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7222 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7223 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7224 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7225 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7226 | #ifdef HAVE_FORK1 |
| 7227 | {"fork1", posix_fork1, METH_VARARGS, posix_fork1__doc__}, |
| 7228 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7229 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7230 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7231 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7232 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7233 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7234 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7235 | #ifdef HAVE_FORKPTY |
| 7236 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 7237 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7238 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7239 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7240 | #endif /* HAVE_GETEGID */ |
| 7241 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7242 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7243 | #endif /* HAVE_GETEUID */ |
| 7244 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7245 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7246 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7247 | #ifdef HAVE_GETGROUPS |
| 7248 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 7249 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7250 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7251 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7252 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7253 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7254 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7255 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7256 | #endif /* HAVE_GETPPID */ |
| 7257 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7258 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7259 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7260 | #ifdef HAVE_GETLOGIN |
| 7261 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 7262 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7263 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7264 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7265 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7266 | #ifdef HAVE_KILLPG |
| 7267 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7268 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7269 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7270 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7271 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7272 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7273 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7274 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7275 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7276 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7277 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7278 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7279 | #else |
| 7280 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7281 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7282 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7283 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7284 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7285 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7286 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7287 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7288 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7289 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7290 | #ifdef HAVE_SETEUID |
| 7291 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7292 | #endif /* HAVE_SETEUID */ |
| 7293 | #ifdef HAVE_SETEGID |
| 7294 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7295 | #endif /* HAVE_SETEGID */ |
| 7296 | #ifdef HAVE_SETREUID |
| 7297 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7298 | #endif /* HAVE_SETREUID */ |
| 7299 | #ifdef HAVE_SETREGID |
| 7300 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7301 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7302 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7303 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7304 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7305 | #ifdef HAVE_SETGROUPS |
| 7306 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7307 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7308 | #ifdef HAVE_GETPGID |
| 7309 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7310 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7311 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7312 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7313 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7314 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7315 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7316 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7317 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7318 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7319 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7320 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7321 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7322 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7323 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7324 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7325 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7326 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7327 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7328 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7329 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7330 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7331 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7332 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7333 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7334 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7335 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7336 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7337 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7338 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7339 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7340 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7341 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7342 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7343 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7344 | #endif |
| 7345 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7346 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7347 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7348 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7349 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7350 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7351 | #ifdef HAVE_DEVICE_MACROS |
| 7352 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7353 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7354 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7355 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7356 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7357 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7358 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7359 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7360 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7361 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7362 | #ifdef HAVE_UNSETENV |
| 7363 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7364 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7365 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7366 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7367 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7368 | #ifdef HAVE_FCHDIR |
| 7369 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7370 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7371 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7372 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7373 | #endif |
| 7374 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7375 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7376 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7377 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7378 | #ifdef WCOREDUMP |
| 7379 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7380 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7381 | #ifdef WIFCONTINUED |
| 7382 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7383 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7384 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7385 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7386 | #endif /* WIFSTOPPED */ |
| 7387 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7388 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7389 | #endif /* WIFSIGNALED */ |
| 7390 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7391 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7392 | #endif /* WIFEXITED */ |
| 7393 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7394 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7395 | #endif /* WEXITSTATUS */ |
| 7396 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7397 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7398 | #endif /* WTERMSIG */ |
| 7399 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7400 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7401 | #endif /* WSTOPSIG */ |
| 7402 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7403 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7404 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7405 | #endif |
| 7406 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7407 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7408 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7409 | #ifdef HAVE_TMPFILE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7410 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 7411 | #endif |
| 7412 | #ifdef HAVE_TEMPNAM |
| 7413 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7414 | #endif |
| 7415 | #ifdef HAVE_TMPNAM |
| 7416 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 7417 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7418 | #ifdef HAVE_CONFSTR |
| 7419 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7420 | #endif |
| 7421 | #ifdef HAVE_SYSCONF |
| 7422 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7423 | #endif |
| 7424 | #ifdef HAVE_FPATHCONF |
| 7425 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7426 | #endif |
| 7427 | #ifdef HAVE_PATHCONF |
| 7428 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7429 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7430 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7431 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7432 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7433 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7434 | #ifdef HAVE_GETLOADAVG |
| 7435 | {"getloadavg", posix_getloadavg, METH_VARARGS, posix_getloadavg__doc__}, |
| 7436 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7437 | {NULL, NULL} /* Sentinel */ |
| 7438 | }; |
| 7439 | |
| 7440 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7441 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7442 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7443 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7444 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7445 | } |
| 7446 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7447 | #if defined(PYOS_OS2) |
| 7448 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7449 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7450 | { |
| 7451 | APIRET rc; |
| 7452 | ULONG values[QSV_MAX+1]; |
| 7453 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7454 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7455 | |
| 7456 | Py_BEGIN_ALLOW_THREADS |
| 7457 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 7458 | Py_END_ALLOW_THREADS |
| 7459 | |
| 7460 | if (rc != NO_ERROR) { |
| 7461 | os2_error(rc); |
| 7462 | return -1; |
| 7463 | } |
| 7464 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7465 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7466 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7467 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7468 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7469 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7470 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7471 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7472 | |
| 7473 | switch (values[QSV_VERSION_MINOR]) { |
| 7474 | case 0: ver = "2.00"; break; |
| 7475 | case 10: ver = "2.10"; break; |
| 7476 | case 11: ver = "2.11"; break; |
| 7477 | case 30: ver = "3.00"; break; |
| 7478 | case 40: ver = "4.00"; break; |
| 7479 | case 50: ver = "5.00"; break; |
| 7480 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7481 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7482 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7483 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7484 | ver = &tmp[0]; |
| 7485 | } |
| 7486 | |
| 7487 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7488 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7489 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7490 | |
| 7491 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7492 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7493 | tmp[1] = ':'; |
| 7494 | tmp[2] = '\0'; |
| 7495 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7496 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7497 | } |
| 7498 | #endif |
| 7499 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7500 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7501 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7502 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7503 | #ifdef F_OK |
| 7504 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7505 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7506 | #ifdef R_OK |
| 7507 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7508 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7509 | #ifdef W_OK |
| 7510 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7511 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7512 | #ifdef X_OK |
| 7513 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7514 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7515 | #ifdef NGROUPS_MAX |
| 7516 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7517 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7518 | #ifdef TMP_MAX |
| 7519 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7520 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7521 | #ifdef WCONTINUED |
| 7522 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7523 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7524 | #ifdef WNOHANG |
| 7525 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7526 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7527 | #ifdef WUNTRACED |
| 7528 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7529 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7530 | #ifdef O_RDONLY |
| 7531 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7532 | #endif |
| 7533 | #ifdef O_WRONLY |
| 7534 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7535 | #endif |
| 7536 | #ifdef O_RDWR |
| 7537 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7538 | #endif |
| 7539 | #ifdef O_NDELAY |
| 7540 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7541 | #endif |
| 7542 | #ifdef O_NONBLOCK |
| 7543 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7544 | #endif |
| 7545 | #ifdef O_APPEND |
| 7546 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7547 | #endif |
| 7548 | #ifdef O_DSYNC |
| 7549 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7550 | #endif |
| 7551 | #ifdef O_RSYNC |
| 7552 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7553 | #endif |
| 7554 | #ifdef O_SYNC |
| 7555 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7556 | #endif |
| 7557 | #ifdef O_NOCTTY |
| 7558 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7559 | #endif |
| 7560 | #ifdef O_CREAT |
| 7561 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7562 | #endif |
| 7563 | #ifdef O_EXCL |
| 7564 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7565 | #endif |
| 7566 | #ifdef O_TRUNC |
| 7567 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7568 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7569 | #ifdef O_BINARY |
| 7570 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7571 | #endif |
| 7572 | #ifdef O_TEXT |
| 7573 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7574 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7575 | #ifdef O_LARGEFILE |
| 7576 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7577 | #endif |
| 7578 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7579 | /* MS Windows */ |
| 7580 | #ifdef O_NOINHERIT |
| 7581 | /* Don't inherit in child processes. */ |
| 7582 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7583 | #endif |
| 7584 | #ifdef _O_SHORT_LIVED |
| 7585 | /* Optimize for short life (keep in memory). */ |
| 7586 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7587 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7588 | #endif |
| 7589 | #ifdef O_TEMPORARY |
| 7590 | /* Automatically delete when last handle is closed. */ |
| 7591 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7592 | #endif |
| 7593 | #ifdef O_RANDOM |
| 7594 | /* Optimize for random access. */ |
| 7595 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7596 | #endif |
| 7597 | #ifdef O_SEQUENTIAL |
| 7598 | /* Optimize for sequential access. */ |
| 7599 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7600 | #endif |
| 7601 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7602 | /* GNU extensions. */ |
| 7603 | #ifdef O_DIRECT |
| 7604 | /* Direct disk access. */ |
| 7605 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7606 | #endif |
| 7607 | #ifdef O_DIRECTORY |
| 7608 | /* Must be a directory. */ |
| 7609 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7610 | #endif |
| 7611 | #ifdef O_NOFOLLOW |
| 7612 | /* Do not follow links. */ |
| 7613 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7614 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7615 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7616 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7617 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7618 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7619 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7620 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7621 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7622 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7623 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7624 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7625 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7626 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7627 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7628 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7629 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7630 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7631 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7632 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7633 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7634 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7635 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7636 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7637 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7638 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7639 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7640 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7641 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7642 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7643 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7644 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7645 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7646 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7647 | #if defined(PYOS_OS2) |
| 7648 | if (insertvalues(d)) return -1; |
| 7649 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7650 | return 0; |
| 7651 | } |
| 7652 | |
| 7653 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7654 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7655 | #define INITFUNC initnt |
| 7656 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7657 | |
| 7658 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7659 | #define INITFUNC initos2 |
| 7660 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7661 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7662 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7663 | #define INITFUNC initposix |
| 7664 | #define MODNAME "posix" |
| 7665 | #endif |
| 7666 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7667 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7668 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7669 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7670 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7671 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7672 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7673 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7674 | posix__doc__); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7675 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7676 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7677 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7678 | Py_XINCREF(v); |
| 7679 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7680 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7681 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7682 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7683 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7684 | return; |
| 7685 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7686 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7687 | return; |
| 7688 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7689 | Py_INCREF(PyExc_OSError); |
| 7690 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7691 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7692 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7693 | if (posix_putenv_garbage == NULL) |
| 7694 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7695 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7696 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7697 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7698 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7699 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7700 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7701 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7702 | structseq_new = StatResultType.tp_new; |
| 7703 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7704 | Py_INCREF((PyObject*) &StatResultType); |
| 7705 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7706 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7707 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7708 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7709 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7710 | PyModule_AddObject(m, "statvfs_result", |
| 7711 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7712 | } |