Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* POSIX module implementation */ |
| 33 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 34 | /* This file is also used for Windows NT and MS-Win. In that case the module |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 35 | actually calls itself 'nt', not 'posix', and a few functions are |
| 36 | either unimplemented or implemented differently. The source |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 37 | assumes that for Windows NT, the macro 'MS_WIN32' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 38 | of the compiler used. Different compilers define their own feature |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 39 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 41 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 43 | static char posix__doc__ [] = |
| 44 | "This module provides access to operating system functionality that is\n\ |
| 45 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 46 | disguised Unix interface). Refer to the library manual and\n\ |
| 47 | corresponding Unix manual entries for more information on calls."; |
| 48 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 49 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 51 | #if defined(PYOS_OS2) |
| 52 | #define INCL_DOS |
| 53 | #define INCL_DOSERRORS |
| 54 | #define INCL_DOSPROCESS |
| 55 | #define INCL_NOPMAPI |
| 56 | #include <os2.h> |
| 57 | #endif |
| 58 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 59 | #include <sys/types.h> |
| 60 | #include <sys/stat.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 61 | #ifdef HAVE_SYS_WAIT_H |
| 62 | #include <sys/wait.h> /* For WNOHANG */ |
| 63 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 65 | #ifdef HAVE_SIGNAL_H |
| 66 | #include <signal.h> |
| 67 | #endif |
| 68 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | #include "mytime.h" /* For clock_t on some systems */ |
| 70 | |
| 71 | #ifdef HAVE_FCNTL_H |
| 72 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 73 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 75 | /* Various compilers have only certain posix functions */ |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 76 | /* XXX Gosh I wish these were all moved into config.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 77 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 78 | #define HAVE_EXECV 1 |
| 79 | #define HAVE_GETCWD 1 |
| 80 | #define HAVE_SYSTEM 1 |
| 81 | #define HAVE_WAIT 1 |
| 82 | #define HAVE_KILL 1 |
| 83 | #define HAVE_PIPE 1 |
| 84 | #define HAVE_POPEN 1 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 86 | /* #define HAVE_FORK 1 */ |
| 87 | /* #define HAVE_GETEGID 1 */ |
| 88 | /* #define HAVE_GETEUID 1 */ |
| 89 | /* #define HAVE_GETGID 1 */ |
| 90 | /* #define HAVE_GETPPID 1 */ |
| 91 | /* #define HAVE_GETUID 1 */ |
| 92 | /* #define HAVE_OPENDIR 1 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 93 | #include <process.h> |
| 94 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 95 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 96 | #define HAVE_GETCWD 1 |
| 97 | #define HAVE_OPENDIR 1 |
| 98 | #define HAVE_SYSTEM 1 |
| 99 | #if defined(__OS2__) |
| 100 | #define HAVE_EXECV 1 |
| 101 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 102 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 103 | #include <process.h> |
| 104 | #else |
| 105 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 106 | #define HAVE_EXECV 1 |
| 107 | #define HAVE_GETCWD 1 |
| 108 | #define HAVE_GETEGID 1 |
| 109 | #define HAVE_GETEUID 1 |
| 110 | #define HAVE_GETGID 1 |
| 111 | #define HAVE_GETPPID 1 |
| 112 | #define HAVE_GETUID 1 |
| 113 | #define HAVE_KILL 1 |
| 114 | #define HAVE_OPENDIR 1 |
| 115 | #define HAVE_PIPE 1 |
| 116 | #define HAVE_POPEN 1 |
| 117 | #define HAVE_SYSTEM 1 |
| 118 | #define HAVE_WAIT 1 |
| 119 | #else |
| 120 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 121 | #define HAVE_GETCWD 1 |
| 122 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 123 | #define HAVE_EXECV 1 |
| 124 | #define HAVE_PIPE 1 |
| 125 | #define HAVE_POPEN 1 |
| 126 | #define HAVE_SYSTEM 1 |
| 127 | #else /* 16-bit Windows */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 128 | #endif /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 129 | #else /* all other compilers */ |
| 130 | /* Unix functions that the configure script doesn't check for */ |
| 131 | #define HAVE_EXECV 1 |
| 132 | #define HAVE_FORK 1 |
| 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 |
| 145 | #endif /* _MSC_VER */ |
| 146 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 147 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 148 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 152 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 153 | #include <unistd.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
| 156 | #ifdef NeXT |
| 157 | /* NeXT's <unistd.h> and <utime.h> aren't worth much */ |
| 158 | #undef HAVE_UNISTD_H |
| 159 | #undef HAVE_UTIME_H |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 160 | #define HAVE_WAITPID |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 161 | /* #undef HAVE_GETCWD */ |
| 162 | #endif |
| 163 | |
| 164 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 165 | /* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */ |
| 166 | extern int rename(); |
| 167 | extern int pclose(); |
| 168 | extern int lstat(); |
| 169 | extern int symlink(); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 170 | #else /* !HAVE_UNISTD_H */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 171 | #if defined(PYCC_VACPP) |
| 172 | extern int mkdir Py_PROTO((char *)); |
| 173 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 174 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 175 | extern int mkdir Py_PROTO((const char *)); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 176 | #else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 177 | extern int mkdir Py_PROTO((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__) |
| 181 | extern int chdir Py_PROTO((char *)); |
| 182 | extern int rmdir Py_PROTO((char *)); |
| 183 | #else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 184 | extern int chdir Py_PROTO((const char *)); |
| 185 | extern int rmdir Py_PROTO((const char *)); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 186 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 187 | extern int chmod Py_PROTO((const char *, mode_t)); |
| 188 | extern int chown Py_PROTO((const char *, uid_t, gid_t)); |
| 189 | extern char *getcwd Py_PROTO((char *, int)); |
| 190 | extern char *strerror Py_PROTO((int)); |
| 191 | extern int link Py_PROTO((const char *, const char *)); |
| 192 | extern int rename Py_PROTO((const char *, const char *)); |
| 193 | extern int stat Py_PROTO((const char *, struct stat *)); |
| 194 | extern int unlink Py_PROTO((const char *)); |
| 195 | extern int pclose Py_PROTO((FILE *)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | #ifdef HAVE_SYMLINK |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 197 | extern int symlink Py_PROTO((const char *, const char *)); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 198 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | #ifdef HAVE_LSTAT |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 200 | extern int lstat Py_PROTO((const char *, struct stat *)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 201 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 204 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | #ifdef HAVE_UTIME_H |
| 207 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 208 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 210 | #ifdef HAVE_SYS_UTIME_H |
| 211 | #include <sys/utime.h> |
| 212 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 213 | #endif /* HAVE_SYS_UTIME_H */ |
| 214 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #ifdef HAVE_SYS_TIMES_H |
| 216 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 217 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | |
| 219 | #ifdef HAVE_SYS_PARAM_H |
| 220 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 221 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
| 223 | #ifdef HAVE_SYS_UTSNAME_H |
| 224 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 225 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | |
| 227 | #ifndef MAXPATHLEN |
| 228 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 229 | #endif /* MAXPATHLEN */ |
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> |
| 257 | #include <windows.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 258 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 259 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 260 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 261 | #else /* 16-bit Windows */ |
| 262 | #include <dos.h> |
| 263 | #include <ctype.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 264 | #endif /* MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 265 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 267 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 269 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 270 | |
| 271 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 272 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 273 | #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 275 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 276 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 277 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 278 | convertenviron() |
| 279 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 280 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 282 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 283 | if (d == NULL) |
| 284 | return NULL; |
| 285 | if (environ == NULL) |
| 286 | return d; |
| 287 | /* XXX This part ignores errors */ |
| 288 | for (e = environ; *e != NULL; e++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 289 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 290 | char *p = strchr(*e, '='); |
| 291 | if (p == NULL) |
| 292 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 293 | v = PyString_FromString(p+1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 294 | if (v == NULL) |
| 295 | continue; |
| 296 | *p = '\0'; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 297 | (void) PyDict_SetItemString(d, *e, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | *p = '='; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 299 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 301 | #if defined(PYOS_OS2) |
| 302 | { |
| 303 | APIRET rc; |
| 304 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 305 | |
| 306 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 307 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 308 | PyObject *v = PyString_FromString(buffer); |
| 309 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 310 | Py_DECREF(v); |
| 311 | } |
| 312 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 313 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 314 | PyObject *v = PyString_FromString(buffer); |
| 315 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 316 | Py_DECREF(v); |
| 317 | } |
| 318 | } |
| 319 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 320 | return d; |
| 321 | } |
| 322 | |
| 323 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 324 | static PyObject *PosixError; /* Exception posix.error */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | |
| 326 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 327 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 328 | static PyObject * posix_error() |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 329 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 330 | return PyErr_SetFromErrno(PosixError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 333 | #if defined(PYOS_OS2) |
| 334 | /********************************************************************** |
| 335 | * Helper Function to Trim and Format OS/2 Messages |
| 336 | **********************************************************************/ |
| 337 | static void |
| 338 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 339 | { |
| 340 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 341 | |
| 342 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 343 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 344 | |
| 345 | while (lastc > msgbuf && isspace(*lastc)) |
| 346 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 347 | } |
| 348 | |
| 349 | /* Add Optional Reason Text */ |
| 350 | if (reason) { |
| 351 | strcat(msgbuf, " : "); |
| 352 | strcat(msgbuf, reason); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /********************************************************************** |
| 357 | * Decode an OS/2 Operating System Error Code |
| 358 | * |
| 359 | * A convenience function to lookup an OS/2 error code and return a |
| 360 | * text message we can use to raise a Python exception. |
| 361 | * |
| 362 | * Notes: |
| 363 | * The messages for errors returned from the OS/2 kernel reside in |
| 364 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 365 | * |
| 366 | **********************************************************************/ |
| 367 | static char * |
| 368 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 369 | { |
| 370 | APIRET rc; |
| 371 | ULONG msglen; |
| 372 | |
| 373 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 374 | Py_BEGIN_ALLOW_THREADS |
| 375 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 376 | errorcode, "oso001.msg", &msglen); |
| 377 | Py_END_ALLOW_THREADS |
| 378 | |
| 379 | if (rc == NO_ERROR) |
| 380 | os2_formatmsg(msgbuf, msglen, reason); |
| 381 | else |
| 382 | sprintf(msgbuf, "unknown OS error #%d", errorcode); |
| 383 | |
| 384 | return msgbuf; |
| 385 | } |
| 386 | |
| 387 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 388 | errors are not in a global variable e.g. 'errno' nor are |
| 389 | they congruent with posix error numbers. */ |
| 390 | |
| 391 | static PyObject * os2_error(int code) |
| 392 | { |
| 393 | char text[1024]; |
| 394 | PyObject *v; |
| 395 | |
| 396 | os2_strerror(text, sizeof(text), code, ""); |
| 397 | |
| 398 | v = Py_BuildValue("(is)", code, text); |
| 399 | if (v != NULL) { |
| 400 | PyErr_SetObject(PosixError, v); |
| 401 | Py_DECREF(v); |
| 402 | } |
| 403 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 404 | } |
| 405 | |
| 406 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 407 | |
| 408 | /* POSIX generic methods */ |
| 409 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 410 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 411 | posix_1str(args, func) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 412 | PyObject *args; |
| 413 | int (*func) Py_FPROTO((const char *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 414 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 415 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 416 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 417 | if (!PyArg_Parse(args, "s", &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 418 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 419 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 420 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 421 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 422 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 423 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 424 | Py_INCREF(Py_None); |
| 425 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 428 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 429 | posix_2str(args, func) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 430 | PyObject *args; |
| 431 | int (*func) Py_FPROTO((const char *, const char *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 432 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 433 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 434 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 435 | if (!PyArg_Parse(args, "(ss)", &path1, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 436 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 437 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 438 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 439 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 440 | if (res != 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 441 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 442 | Py_INCREF(Py_None); |
| 443 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 446 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 447 | posix_strint(args, func) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 448 | PyObject *args; |
| 449 | int (*func) Py_FPROTO((const char *, int)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 450 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 451 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 452 | int i; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 453 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 454 | if (!PyArg_Parse(args, "(si)", &path, &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 455 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 456 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 457 | res = (*func)(path, i); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 458 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 459 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 460 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 461 | Py_INCREF(Py_None); |
| 462 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 465 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 466 | posix_strintint(args, func) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 467 | PyObject *args; |
| 468 | int (*func) Py_FPROTO((const char *, int, int)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 469 | { |
| 470 | char *path; |
| 471 | int i,i2; |
| 472 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 473 | if (!PyArg_Parse(args, "(sii)", &path, &i, &i2)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 474 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 475 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 476 | res = (*func)(path, i, i2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 477 | Py_END_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 478 | if (res < 0) |
| 479 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 480 | Py_INCREF(Py_None); |
| 481 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 484 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 485 | posix_do_stat(self, args, statfunc) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 486 | PyObject *self; |
| 487 | PyObject *args; |
| 488 | int (*statfunc) Py_FPROTO((const char *, struct stat *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 489 | { |
| 490 | struct stat st; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 491 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 492 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 493 | if (!PyArg_Parse(args, "s", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 494 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 495 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 496 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 497 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 498 | if (res != 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 499 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 500 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 501 | (long)st.st_mode, |
| 502 | (long)st.st_ino, |
| 503 | (long)st.st_dev, |
| 504 | (long)st.st_nlink, |
| 505 | (long)st.st_uid, |
| 506 | (long)st.st_gid, |
| 507 | (long)st.st_size, |
| 508 | (long)st.st_atime, |
| 509 | (long)st.st_mtime, |
| 510 | (long)st.st_ctime); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | |
| 514 | /* POSIX methods */ |
| 515 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 516 | static char posix_chdir__doc__[] = |
| 517 | "chdir(path) -> None\n\ |
| 518 | Change the current working directory to the specified path."; |
| 519 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 520 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 521 | posix_chdir(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 522 | PyObject *self; |
| 523 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 524 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | return posix_1str(args, chdir); |
| 526 | } |
| 527 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 528 | |
| 529 | static char posix_chmod__doc__[] = |
| 530 | "chmod(path, mode) -> None\n\ |
| 531 | Change the access permissions of a file."; |
| 532 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 533 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 534 | posix_chmod(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 535 | PyObject *self; |
| 536 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 538 | return posix_strint(args, chmod); |
| 539 | } |
| 540 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 541 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 542 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 543 | static char posix_chown__doc__[] = |
| 544 | "chown(path, uid, gid) -> None\n\ |
| 545 | Change the owner and group id of path to the numeric uid and gid."; |
| 546 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 547 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 548 | posix_chown(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 549 | PyObject *self; |
| 550 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 551 | { |
| 552 | return posix_strintint(args, chown); |
| 553 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 554 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 555 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 556 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 557 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 558 | static char posix_getcwd__doc__[] = |
| 559 | "getcwd() -> path\n\ |
| 560 | Return a string representing the current working directory."; |
| 561 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 562 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 563 | posix_getcwd(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 564 | PyObject *self; |
| 565 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 566 | { |
| 567 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 568 | char *res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 569 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 570 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 571 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 572 | res = getcwd(buf, sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 573 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 574 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 575 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 576 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 577 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 578 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 579 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 580 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 581 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 582 | static char posix_link__doc__[] = |
| 583 | "link(src, dst) -> None\n\ |
| 584 | Create a hard link to a file."; |
| 585 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 586 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 587 | posix_link(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 588 | PyObject *self; |
| 589 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 590 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 591 | return posix_2str(args, link); |
| 592 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 593 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 594 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 595 | |
| 596 | static char posix_listdir__doc__[] = |
| 597 | "listdir(path) -> list_of_strings\n\ |
| 598 | Return a list containing the names of the entries in the directory.\n\ |
| 599 | \n\ |
| 600 | path: path of directory to list\n\ |
| 601 | \n\ |
| 602 | The list is in arbitrary order. It does not include the special\n\ |
| 603 | entries '.' and '..' even if they are present in the directory."; |
| 604 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 605 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 606 | posix_listdir(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 607 | PyObject *self; |
| 608 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 609 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 610 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 611 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 612 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 613 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 614 | char *name; |
| 615 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 616 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 617 | HANDLE hFindFile; |
| 618 | WIN32_FIND_DATA FileData; |
| 619 | char namebuf[MAX_PATH+5]; |
| 620 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 621 | if (!PyArg_Parse(args, "s#", &name, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 622 | return NULL; |
| 623 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 624 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 625 | return NULL; |
| 626 | } |
| 627 | strcpy(namebuf, name); |
| 628 | if (namebuf[len-1] != '/' && namebuf[len-1] != '\\') |
| 629 | namebuf[len++] = '/'; |
| 630 | strcpy(namebuf + len, "*.*"); |
| 631 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 632 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 633 | return NULL; |
| 634 | |
| 635 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 636 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 637 | errno = GetLastError(); |
| 638 | return posix_error(); |
| 639 | } |
| 640 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 641 | if (FileData.cFileName[0] == '.' && |
| 642 | (FileData.cFileName[1] == '\0' || |
| 643 | FileData.cFileName[1] == '.' && |
| 644 | FileData.cFileName[2] == '\0')) |
| 645 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 646 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 647 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 648 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 649 | d = NULL; |
| 650 | break; |
| 651 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 652 | if (PyList_Append(d, v) != 0) { |
| 653 | Py_DECREF(v); |
| 654 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 655 | d = NULL; |
| 656 | break; |
| 657 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 658 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 659 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 660 | |
| 661 | if (FindClose(hFindFile) == FALSE) { |
| 662 | errno = GetLastError(); |
| 663 | return posix_error(); |
| 664 | } |
| 665 | |
| 666 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 667 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 668 | #else /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 669 | #ifdef _MSC_VER /* 16-bit Windows */ |
| 670 | |
| 671 | #ifndef MAX_PATH |
| 672 | #define MAX_PATH 250 |
| 673 | #endif |
| 674 | char *name, *pt; |
| 675 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 676 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 677 | char namebuf[MAX_PATH+5]; |
| 678 | struct _find_t ep; |
| 679 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 680 | if (!PyArg_Parse(args, "s#", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 681 | return NULL; |
| 682 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 683 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 684 | return NULL; |
| 685 | } |
| 686 | strcpy(namebuf, name); |
| 687 | for (pt = namebuf; *pt; pt++) |
| 688 | if (*pt == '/') |
| 689 | *pt = '\\'; |
| 690 | if (namebuf[len-1] != '\\') |
| 691 | namebuf[len++] = '\\'; |
| 692 | strcpy(namebuf + len, "*.*"); |
| 693 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 694 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 695 | return NULL; |
| 696 | |
| 697 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 698 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 699 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 700 | errno = ENOENT; |
| 701 | return posix_error(); |
| 702 | } |
| 703 | do { |
| 704 | if (ep.name[0] == '.' && |
| 705 | (ep.name[1] == '\0' || |
| 706 | ep.name[1] == '.' && |
| 707 | ep.name[2] == '\0')) |
| 708 | continue; |
| 709 | strcpy(namebuf, ep.name); |
| 710 | for (pt = namebuf; *pt; pt++) |
| 711 | if (isupper(*pt)) |
| 712 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 713 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 714 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 715 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 716 | d = NULL; |
| 717 | break; |
| 718 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 719 | if (PyList_Append(d, v) != 0) { |
| 720 | Py_DECREF(v); |
| 721 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 722 | d = NULL; |
| 723 | break; |
| 724 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 725 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 726 | } while (_dos_findnext(&ep) == 0); |
| 727 | |
| 728 | return d; |
| 729 | |
| 730 | #else |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 731 | #if defined(PYOS_OS2) |
| 732 | |
| 733 | #ifndef MAX_PATH |
| 734 | #define MAX_PATH CCHMAXPATH |
| 735 | #endif |
| 736 | char *name, *pt; |
| 737 | int len; |
| 738 | PyObject *d, *v; |
| 739 | char namebuf[MAX_PATH+5]; |
| 740 | HDIR hdir = 1; |
| 741 | ULONG srchcnt = 1; |
| 742 | FILEFINDBUF3 ep; |
| 743 | APIRET rc; |
| 744 | |
| 745 | if (!PyArg_Parse(args, "s#", &name, &len)) |
| 746 | return NULL; |
| 747 | if (len >= MAX_PATH) { |
| 748 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 749 | return NULL; |
| 750 | } |
| 751 | strcpy(namebuf, name); |
| 752 | for (pt = namebuf; *pt; pt++) |
| 753 | if (*pt == '/') |
| 754 | *pt = '\\'; |
| 755 | if (namebuf[len-1] != '\\') |
| 756 | namebuf[len++] = '\\'; |
| 757 | strcpy(namebuf + len, "*.*"); |
| 758 | |
| 759 | if ((d = PyList_New(0)) == NULL) |
| 760 | return NULL; |
| 761 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 762 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 763 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 764 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 765 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 766 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 767 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 768 | |
| 769 | if (rc != NO_ERROR) { |
| 770 | errno = ENOENT; |
| 771 | return posix_error(); |
| 772 | } |
| 773 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 774 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 775 | do { |
| 776 | if (ep.achName[0] == '.' |
| 777 | && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0')) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 778 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 779 | |
| 780 | strcpy(namebuf, ep.achName); |
| 781 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 782 | /* Leave Case of Name Alone -- In Native Form */ |
| 783 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 784 | |
| 785 | v = PyString_FromString(namebuf); |
| 786 | if (v == NULL) { |
| 787 | Py_DECREF(d); |
| 788 | d = NULL; |
| 789 | break; |
| 790 | } |
| 791 | if (PyList_Append(d, v) != 0) { |
| 792 | Py_DECREF(v); |
| 793 | Py_DECREF(d); |
| 794 | d = NULL; |
| 795 | break; |
| 796 | } |
| 797 | Py_DECREF(v); |
| 798 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 799 | } |
| 800 | |
| 801 | return d; |
| 802 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 803 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 804 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 805 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 806 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 807 | struct dirent *ep; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 808 | if (!PyArg_Parse(args, "s", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 809 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 810 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 811 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 812 | Py_BLOCK_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 813 | return posix_error(); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 814 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 815 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 816 | closedir(dirp); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 817 | Py_BLOCK_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 818 | return NULL; |
| 819 | } |
| 820 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 821 | if (ep->d_name[0] == '.' && |
| 822 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 823 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 824 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 825 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 826 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 827 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 828 | d = NULL; |
| 829 | break; |
| 830 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 831 | if (PyList_Append(d, v) != 0) { |
| 832 | Py_DECREF(v); |
| 833 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 834 | d = NULL; |
| 835 | break; |
| 836 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 837 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 838 | } |
| 839 | closedir(dirp); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 840 | Py_END_ALLOW_THREADS |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 841 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 842 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 843 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 844 | #endif /* !PYOS_OS2 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 845 | #endif /* !_MSC_VER */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 846 | #endif /* !MS_WIN32 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 849 | static char posix_mkdir__doc__[] = |
| 850 | "mkdir(path [, mode=0777]) -> None\n\ |
| 851 | Create a directory."; |
| 852 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 853 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 854 | posix_mkdir(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 855 | PyObject *self; |
| 856 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 857 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 858 | int res; |
| 859 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 860 | int mode = 0777; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 861 | if (!PyArg_ParseTuple(args, "s|i", &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 862 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 863 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 864 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 865 | res = mkdir(path); |
| 866 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 867 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 868 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 869 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 870 | if (res < 0) |
| 871 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 872 | Py_INCREF(Py_None); |
| 873 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 876 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 877 | #ifdef HAVE_NICE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 878 | static char posix_nice__doc__[] = |
| 879 | "nice(inc) -> new_priority\n\ |
| 880 | Decrease the priority of process and return new priority."; |
| 881 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 882 | static PyObject * |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 883 | posix_nice(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 884 | PyObject *self; |
| 885 | PyObject *args; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 886 | { |
| 887 | int increment, value; |
| 888 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 889 | if (!PyArg_Parse(args, "i", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 890 | return NULL; |
| 891 | value = nice(increment); |
| 892 | if (value == -1) |
| 893 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 894 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 895 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 896 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 897 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 898 | |
| 899 | static char posix_rename__doc__[] = |
| 900 | "rename(old, new) -> None\n\ |
| 901 | Rename a file or directory."; |
| 902 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 903 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 904 | posix_rename(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 905 | PyObject *self; |
| 906 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 907 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 908 | return posix_2str(args, rename); |
| 909 | } |
| 910 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 911 | |
| 912 | static char posix_rmdir__doc__[] = |
| 913 | "rmdir(path) -> None\n\ |
| 914 | Remove a directory."; |
| 915 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 916 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 917 | posix_rmdir(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 918 | PyObject *self; |
| 919 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 920 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 921 | return posix_1str(args, rmdir); |
| 922 | } |
| 923 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 924 | |
| 925 | static char posix_stat__doc__[] = |
| 926 | "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 927 | Perform a stat system call on the given path."; |
| 928 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 929 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 930 | posix_stat(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 931 | PyObject *self; |
| 932 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 933 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 934 | return posix_do_stat(self, args, stat); |
| 935 | } |
| 936 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 937 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 938 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 939 | static char posix_system__doc__[] = |
| 940 | "system(command) -> exit_status\n\ |
| 941 | Execute the command (a string) in a subshell."; |
| 942 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 943 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 944 | posix_system(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 945 | PyObject *self; |
| 946 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 947 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 948 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 949 | long sts; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 950 | if (!PyArg_Parse(args, "s", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 951 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 952 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 953 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 954 | Py_END_ALLOW_THREADS |
| 955 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 956 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 957 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 958 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 959 | |
| 960 | static char posix_umask__doc__[] = |
| 961 | "umask(new_mask) -> old_mask\n\ |
| 962 | Set the current numeric umask and return the previous umask."; |
| 963 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 964 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 965 | posix_umask(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 966 | PyObject *self; |
| 967 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 968 | { |
| 969 | int i; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 970 | if (!PyArg_Parse(args, "i", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 971 | return NULL; |
| 972 | i = umask(i); |
| 973 | if (i < 0) |
| 974 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 975 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 978 | |
| 979 | static char posix_unlink__doc__[] = |
| 980 | "unlink(path) -> None\n\ |
| 981 | Remove a file (same as remove(path))."; |
| 982 | |
| 983 | static char posix_remove__doc__[] = |
| 984 | "remove(path) -> None\n\ |
| 985 | Remove a file (same as unlink(path))."; |
| 986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 987 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 988 | posix_unlink(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 989 | PyObject *self; |
| 990 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 991 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 992 | return posix_1str(args, unlink); |
| 993 | } |
| 994 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 995 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 996 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 997 | static char posix_uname__doc__[] = |
| 998 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 999 | Return a tuple identifying the current operating system."; |
| 1000 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1001 | static PyObject * |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1002 | posix_uname(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1003 | PyObject *self; |
| 1004 | PyObject *args; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1005 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1006 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1007 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1008 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1009 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1010 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1011 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1012 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1013 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1014 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1015 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1016 | u.sysname, |
| 1017 | u.nodename, |
| 1018 | u.release, |
| 1019 | u.version, |
| 1020 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1021 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1022 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1023 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1024 | |
| 1025 | static char posix_utime__doc__[] = |
| 1026 | "utime(path, (atime, utime)) -> None\n\ |
| 1027 | Set the access and modified time of the file to the given values."; |
| 1028 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1029 | static PyObject * |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1030 | posix_utime(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1031 | PyObject *self; |
| 1032 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1033 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1034 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1035 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1036 | int res; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1037 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1038 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1039 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1040 | struct utimbuf buf; |
| 1041 | #define ATIME buf.actime |
| 1042 | #define MTIME buf.modtime |
| 1043 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1044 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1045 | time_t buf[2]; |
| 1046 | #define ATIME buf[0] |
| 1047 | #define MTIME buf[1] |
| 1048 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1049 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1050 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1051 | if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1052 | return NULL; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1053 | ATIME = atime; |
Guido van Rossum | d1b3481 | 1995-02-07 15:39:29 +0000 | [diff] [blame] | 1054 | MTIME = mtime; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1055 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1056 | res = utime(path, UTIME_ARG); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1057 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1058 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1059 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1060 | Py_INCREF(Py_None); |
| 1061 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1062 | #undef UTIME_ARG |
| 1063 | #undef ATIME |
| 1064 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1067 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1068 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1069 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1070 | static char posix__exit__doc__[] = |
| 1071 | "_exit(status)\n\ |
| 1072 | Exit to the system with specified status, without normal exit processing."; |
| 1073 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1074 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1075 | posix__exit(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1076 | PyObject *self; |
| 1077 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1078 | { |
| 1079 | int sts; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1080 | if (!PyArg_Parse(args, "i", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1081 | return NULL; |
| 1082 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1083 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1086 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1087 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1088 | static char posix_execv__doc__[] = |
| 1089 | "execv(path, args)\n\ |
| 1090 | Execute an executable path with arguments, replacing current process.\n\ |
| 1091 | \n\ |
| 1092 | path: path of executable file\n\ |
| 1093 | args: tuple or list of strings"; |
| 1094 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1095 | static PyObject * |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1096 | posix_execv(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1097 | PyObject *self; |
| 1098 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1099 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1100 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1101 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1102 | char **argvlist; |
| 1103 | int i, argc; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1104 | PyObject *(*getitem) Py_PROTO((PyObject *, int)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1105 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1106 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1107 | argv is a list or tuple of strings. */ |
| 1108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1109 | if (!PyArg_Parse(args, "(sO)", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1110 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1111 | if (PyList_Check(argv)) { |
| 1112 | argc = PyList_Size(argv); |
| 1113 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1114 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1115 | else if (PyTuple_Check(argv)) { |
| 1116 | argc = PyTuple_Size(argv); |
| 1117 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1118 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1119 | else { |
| 1120 | badarg: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1121 | PyErr_BadArgument(); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1122 | return NULL; |
| 1123 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1124 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1125 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1126 | if (argvlist == NULL) |
| 1127 | return NULL; |
| 1128 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1129 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1130 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1131 | goto badarg; |
| 1132 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1133 | } |
| 1134 | argvlist[argc] = NULL; |
| 1135 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1136 | #ifdef BAD_EXEC_PROTOTYPES |
| 1137 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1138 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1139 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1140 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1141 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1142 | /* If we get here it's definitely an error */ |
| 1143 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1144 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1145 | return posix_error(); |
| 1146 | } |
| 1147 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1148 | |
| 1149 | static char posix_execve__doc__[] = |
| 1150 | "execve(path, args, env)\n\ |
| 1151 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1152 | \n\ |
| 1153 | path: path of executable file\n\ |
| 1154 | args: tuple or list of arguments\n\ |
| 1155 | env: dictonary of strings mapping to strings"; |
| 1156 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1157 | static PyObject * |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1158 | posix_execve(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1159 | PyObject *self; |
| 1160 | PyObject *args; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1161 | { |
| 1162 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1163 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1164 | char **argvlist; |
| 1165 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1166 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1167 | int i, pos, argc, envc; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1168 | PyObject *(*getitem) Py_PROTO((PyObject *, int)); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1169 | |
| 1170 | /* execve has three arguments: (path, argv, env), where |
| 1171 | argv is a list or tuple of strings and env is a dictionary |
| 1172 | like posix.environ. */ |
| 1173 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1174 | if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1175 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1176 | if (PyList_Check(argv)) { |
| 1177 | argc = PyList_Size(argv); |
| 1178 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1179 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1180 | else if (PyTuple_Check(argv)) { |
| 1181 | argc = PyTuple_Size(argv); |
| 1182 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1183 | } |
| 1184 | else { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1185 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1186 | return NULL; |
| 1187 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1188 | if (!PyMapping_Check(env)) { |
| 1189 | PyErr_SetString(PyExc_TypeError, "env must be mapping object"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1190 | return NULL; |
| 1191 | } |
| 1192 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1193 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1194 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1195 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1196 | return NULL; |
| 1197 | } |
| 1198 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1199 | if (!PyArg_Parse((*getitem)(argv, i), |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1200 | "s;argv must be list of strings", |
| 1201 | &argvlist[i])) |
| 1202 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1203 | goto fail_1; |
| 1204 | } |
| 1205 | } |
| 1206 | argvlist[argc] = NULL; |
| 1207 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1208 | i = PyMapping_Length(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1209 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1210 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1211 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1212 | goto fail_1; |
| 1213 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1214 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1215 | keys = PyMapping_Keys(env); |
| 1216 | vals = PyMapping_Values(env); |
| 1217 | if (!keys || !vals) |
| 1218 | goto fail_2; |
| 1219 | |
| 1220 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1221 | char *p, *k, *v; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1222 | |
| 1223 | key = PyList_GetItem(keys, pos); |
| 1224 | val = PyList_GetItem(vals, pos); |
| 1225 | if (!key || !val) |
| 1226 | goto fail_2; |
| 1227 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1228 | if (!PyArg_Parse(key, "s;non-string key in env", &k) || |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1229 | !PyArg_Parse(val, "s;non-string value in env", &v)) |
| 1230 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1231 | goto fail_2; |
| 1232 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1233 | |
| 1234 | #if defined(PYOS_OS2) |
| 1235 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1236 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1237 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1238 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1239 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1240 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1241 | goto fail_2; |
| 1242 | } |
| 1243 | sprintf(p, "%s=%s", k, v); |
| 1244 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1245 | #if defined(PYOS_OS2) |
| 1246 | } |
| 1247 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1248 | } |
| 1249 | envlist[envc] = 0; |
| 1250 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1251 | |
| 1252 | #ifdef BAD_EXEC_PROTOTYPES |
| 1253 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1254 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1255 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1256 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* If we get here it's definitely an error */ |
| 1259 | |
| 1260 | (void) posix_error(); |
| 1261 | |
| 1262 | fail_2: |
| 1263 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1264 | PyMem_DEL(envlist[envc]); |
| 1265 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1266 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1267 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1268 | Py_XDECREF(vals); |
| 1269 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1270 | return NULL; |
| 1271 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1272 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1273 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1274 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1275 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1276 | static char posix_fork__doc__[] = |
| 1277 | "fork() -> pid\n\ |
| 1278 | Fork a child process.\n\ |
| 1279 | \n\ |
| 1280 | Return 0 to child process and PID of child to parent process."; |
| 1281 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1282 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1283 | posix_fork(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1284 | PyObject *self; |
| 1285 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1286 | { |
| 1287 | int pid; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1288 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1289 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1290 | pid = fork(); |
| 1291 | if (pid == -1) |
| 1292 | return posix_error(); |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 1293 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1294 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1295 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1296 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1297 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1298 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1299 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1300 | static char posix_getegid__doc__[] = |
| 1301 | "getegid() -> egid\n\ |
| 1302 | Return the current process's effective group id."; |
| 1303 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1304 | static PyObject * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1305 | posix_getegid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1306 | PyObject *self; |
| 1307 | PyObject *args; |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1308 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1309 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1310 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1311 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1312 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1313 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1314 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1315 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1316 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1317 | static char posix_geteuid__doc__[] = |
| 1318 | "geteuid() -> euid\n\ |
| 1319 | Return the current process's effective user id."; |
| 1320 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1321 | static PyObject * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1322 | posix_geteuid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1323 | PyObject *self; |
| 1324 | PyObject *args; |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1325 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1326 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1327 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1328 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1329 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1330 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1331 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1332 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1333 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1334 | static char posix_getgid__doc__[] = |
| 1335 | "getgid() -> gid\n\ |
| 1336 | Return the current process's group id."; |
| 1337 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1338 | static PyObject * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1339 | posix_getgid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1340 | PyObject *self; |
| 1341 | PyObject *args; |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1342 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1343 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1344 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1345 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1346 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1347 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1348 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1349 | |
| 1350 | static char posix_getpid__doc__[] = |
| 1351 | "getpid() -> pid\n\ |
| 1352 | Return the current process id"; |
| 1353 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1354 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1355 | posix_getpid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1356 | PyObject *self; |
| 1357 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1358 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1359 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1360 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1361 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1364 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1365 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1366 | static char posix_getpgrp__doc__[] = |
| 1367 | "getpgrp() -> pgrp\n\ |
| 1368 | Return the current process group id."; |
| 1369 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1370 | static PyObject * |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1371 | posix_getpgrp(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1372 | PyObject *self; |
| 1373 | PyObject *args; |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1374 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1375 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1376 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1377 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1378 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1379 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1380 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1381 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1382 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1383 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1384 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1385 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1386 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1387 | static char posix_setpgrp__doc__[] = |
| 1388 | "setpgrp() -> None\n\ |
| 1389 | Make this process a session leader."; |
| 1390 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1391 | static PyObject * |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1392 | posix_setpgrp(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1393 | PyObject *self; |
| 1394 | PyObject *args; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1395 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1396 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1397 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1398 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1399 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1400 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1401 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1402 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1403 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1404 | Py_INCREF(Py_None); |
| 1405 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1408 | #endif /* HAVE_SETPGRP */ |
| 1409 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1410 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1411 | static char posix_getppid__doc__[] = |
| 1412 | "getppid() -> ppid\n\ |
| 1413 | Return the parent's process id."; |
| 1414 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1415 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1416 | posix_getppid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1417 | PyObject *self; |
| 1418 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1419 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1420 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1421 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1422 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1423 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1424 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1425 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1426 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1427 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1428 | static char posix_getuid__doc__[] = |
| 1429 | "getuid() -> uid\n\ |
| 1430 | Return the current process's user id."; |
| 1431 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1432 | static PyObject * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1433 | posix_getuid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1434 | PyObject *self; |
| 1435 | PyObject *args; |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1436 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1437 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1438 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1439 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1440 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1441 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1442 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1443 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1444 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1445 | static char posix_kill__doc__[] = |
| 1446 | "kill(pid, sig) -> None\n\ |
| 1447 | Kill a process with a signal."; |
| 1448 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1449 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1450 | posix_kill(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1451 | PyObject *self; |
| 1452 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1453 | { |
| 1454 | int pid, sig; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1455 | if (!PyArg_Parse(args, "(ii)", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1456 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1457 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1458 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 1459 | APIRET rc; |
| 1460 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1461 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1462 | |
| 1463 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 1464 | APIRET rc; |
| 1465 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1466 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1467 | |
| 1468 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1469 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1470 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1471 | if (kill(pid, sig) == -1) |
| 1472 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1473 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1474 | Py_INCREF(Py_None); |
| 1475 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1476 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1477 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1478 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1479 | #ifdef HAVE_PLOCK |
| 1480 | |
| 1481 | #ifdef HAVE_SYS_LOCK_H |
| 1482 | #include <sys/lock.h> |
| 1483 | #endif |
| 1484 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1485 | static char posix_plock__doc__[] = |
| 1486 | "plock(op) -> None\n\ |
| 1487 | Lock program segments into memory."; |
| 1488 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1489 | static PyObject * |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1490 | posix_plock(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1491 | PyObject *self; |
| 1492 | PyObject *args; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1493 | { |
| 1494 | int op; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1495 | if (!PyArg_Parse(args, "i", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1496 | return NULL; |
| 1497 | if (plock(op) == -1) |
| 1498 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1499 | Py_INCREF(Py_None); |
| 1500 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1501 | } |
| 1502 | #endif |
| 1503 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1504 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1505 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1506 | static char posix_popen__doc__[] = |
| 1507 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 1508 | Open a pipe to/from a command returning a file object."; |
| 1509 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1510 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1511 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1512 | async_system(const char *command) |
| 1513 | { |
| 1514 | char *p, errormsg[256], args[1024]; |
| 1515 | RESULTCODES rcodes; |
| 1516 | APIRET rc; |
| 1517 | char *shell = getenv("COMSPEC"); |
| 1518 | if (!shell) |
| 1519 | shell = "cmd"; |
| 1520 | |
| 1521 | strcpy(args, shell); |
| 1522 | p = &args[ strlen(args)+1 ]; |
| 1523 | strcpy(p, "/c "); |
| 1524 | strcat(p, command); |
| 1525 | p += strlen(p) + 1; |
| 1526 | *p = '\0'; |
| 1527 | |
| 1528 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1529 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1530 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1531 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1532 | &rcodes, shell); |
| 1533 | return rc; |
| 1534 | } |
| 1535 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1536 | static FILE * |
| 1537 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1538 | { |
| 1539 | HFILE rhan, whan; |
| 1540 | FILE *retfd = NULL; |
| 1541 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 1542 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1543 | if (rc != NO_ERROR) { |
| 1544 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1545 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1546 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1547 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1548 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 1549 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1550 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1551 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 1552 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1553 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1554 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 1555 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1556 | |
| 1557 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1558 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1561 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 1562 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1563 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1564 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 1565 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1566 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1567 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 1568 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1569 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1570 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 1571 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1572 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1573 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 1574 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1575 | |
| 1576 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1577 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1580 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 1581 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1582 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1583 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 1584 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1585 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1586 | } else { |
| 1587 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1588 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1589 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | static PyObject * |
| 1593 | posix_popen(self, args) |
| 1594 | PyObject *self; |
| 1595 | PyObject *args; |
| 1596 | { |
| 1597 | char *name; |
| 1598 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1599 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1600 | FILE *fp; |
| 1601 | PyObject *f; |
| 1602 | if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize)) |
| 1603 | return NULL; |
| 1604 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1605 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1606 | Py_END_ALLOW_THREADS |
| 1607 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1608 | return os2_error(err); |
| 1609 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1610 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 1611 | if (f != NULL) |
| 1612 | PyFile_SetBufSize(f, bufsize); |
| 1613 | return f; |
| 1614 | } |
| 1615 | |
| 1616 | #else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1617 | static PyObject * |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1618 | posix_popen(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1619 | PyObject *self; |
| 1620 | PyObject *args; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1621 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1622 | char *name; |
| 1623 | char *mode = "r"; |
| 1624 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1625 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1626 | PyObject *f; |
| 1627 | if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1628 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1629 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1630 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1631 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1632 | if (fp == NULL) |
| 1633 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1634 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1635 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1636 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1637 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1638 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1639 | #endif |
| 1640 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1641 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1642 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1643 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1644 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1645 | static char posix_setuid__doc__[] = |
| 1646 | "setuid(uid) -> None\n\ |
| 1647 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1648 | static PyObject * |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1649 | posix_setuid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1650 | PyObject *self; |
| 1651 | PyObject *args; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1652 | { |
| 1653 | int uid; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1654 | if (!PyArg_Parse(args, "i", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1655 | return NULL; |
| 1656 | if (setuid(uid) < 0) |
| 1657 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1658 | Py_INCREF(Py_None); |
| 1659 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1660 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1661 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1662 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1663 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1664 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1665 | static char posix_setgid__doc__[] = |
| 1666 | "setgid(gid) -> None\n\ |
| 1667 | Set the current process's group id."; |
| 1668 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1669 | static PyObject * |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1670 | posix_setgid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1671 | PyObject *self; |
| 1672 | PyObject *args; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1673 | { |
| 1674 | int gid; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1675 | if (!PyArg_Parse(args, "i", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1676 | return NULL; |
| 1677 | if (setgid(gid) < 0) |
| 1678 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1679 | Py_INCREF(Py_None); |
| 1680 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1681 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1682 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1683 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1684 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1685 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1686 | static char posix_waitpid__doc__[] = |
| 1687 | "waitpid(pid, options) -> (pid, status)\n\ |
| 1688 | Wait for completion of a give child process."; |
| 1689 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1690 | static PyObject * |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1691 | posix_waitpid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1692 | PyObject *self; |
| 1693 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1694 | { |
Guido van Rossum | fd03e2b | 1996-06-19 23:17:02 +0000 | [diff] [blame] | 1695 | int pid, options, sts = 0; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1696 | if (!PyArg_Parse(args, "(ii)", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1697 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1698 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 1699 | #ifdef NeXT |
| 1700 | pid = wait4(pid, (union wait *)&sts, options, NULL); |
| 1701 | #else |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1702 | pid = waitpid(pid, &sts, options); |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 1703 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1704 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1705 | if (pid == -1) |
| 1706 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1707 | else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1708 | return Py_BuildValue("ii", pid, sts); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1709 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1710 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1711 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1712 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1713 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1714 | static char posix_wait__doc__[] = |
| 1715 | "wait() -> (pid, status)\n\ |
| 1716 | Wait for completion of a child process."; |
| 1717 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1718 | static PyObject * |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1719 | posix_wait(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1720 | PyObject *self; |
| 1721 | PyObject *args; |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1722 | { |
| 1723 | int pid, sts; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1724 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 1725 | #ifdef NeXT |
| 1726 | pid = wait((union wait *)&sts); |
| 1727 | #else |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1728 | pid = wait(&sts); |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 1729 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1730 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1731 | if (pid == -1) |
| 1732 | return posix_error(); |
| 1733 | else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1734 | return Py_BuildValue("ii", pid, sts); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1735 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1736 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1737 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1738 | |
| 1739 | static char posix_lstat__doc__[] = |
| 1740 | "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 1741 | Like stat(path), but do not follow symbolic links."; |
| 1742 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1743 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1744 | posix_lstat(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1745 | PyObject *self; |
| 1746 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1747 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1748 | #ifdef HAVE_LSTAT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1749 | return posix_do_stat(self, args, lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1750 | #else /* !HAVE_LSTAT */ |
| 1751 | return posix_do_stat(self, args, stat); |
| 1752 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1755 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1756 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1757 | static char posix_readlink__doc__[] = |
| 1758 | "readlink(path) -> path\n\ |
| 1759 | Return a string representing the path to which the symbolic link points."; |
| 1760 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1761 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1762 | posix_readlink(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1763 | PyObject *self; |
| 1764 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1765 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1766 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1767 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1768 | int n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1769 | if (!PyArg_Parse(args, "s", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1770 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1771 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1772 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1773 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1774 | if (n < 0) |
| 1775 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1776 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1777 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1778 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1779 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1780 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1781 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1782 | static char posix_symlink__doc__[] = |
| 1783 | "symlink(src, dst) -> None\n\ |
| 1784 | Create a symbolic link."; |
| 1785 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 1786 | static PyObject * |
| 1787 | posix_symlink(self, args) |
| 1788 | PyObject *self; |
| 1789 | PyObject *args; |
| 1790 | { |
| 1791 | return posix_2str(args, symlink); |
| 1792 | } |
| 1793 | #endif /* HAVE_SYMLINK */ |
| 1794 | |
| 1795 | |
| 1796 | #ifdef HAVE_TIMES |
| 1797 | #ifndef HZ |
| 1798 | #define HZ 60 /* Universal constant :-) */ |
| 1799 | #endif /* HZ */ |
| 1800 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1801 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 1802 | static long |
| 1803 | system_uptime() |
| 1804 | { |
| 1805 | ULONG value = 0; |
| 1806 | |
| 1807 | Py_BEGIN_ALLOW_THREADS |
| 1808 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 1809 | Py_END_ALLOW_THREADS |
| 1810 | |
| 1811 | return value; |
| 1812 | } |
| 1813 | |
| 1814 | static PyObject * |
| 1815 | posix_times(self, args) |
| 1816 | PyObject *self; |
| 1817 | PyObject *args; |
| 1818 | { |
| 1819 | if (!PyArg_NoArgs(args)) |
| 1820 | return NULL; |
| 1821 | |
| 1822 | /* Currently Only Uptime is Provided -- Others Later */ |
| 1823 | return Py_BuildValue("ddddd", |
| 1824 | (double)0 /* t.tms_utime / HZ */, |
| 1825 | (double)0 /* t.tms_stime / HZ */, |
| 1826 | (double)0 /* t.tms_cutime / HZ */, |
| 1827 | (double)0 /* t.tms_cstime / HZ */, |
| 1828 | (double)system_uptime() / 1000); |
| 1829 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 1830 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1831 | static PyObject * |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1832 | posix_times(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1833 | PyObject *self; |
| 1834 | PyObject *args; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1835 | { |
| 1836 | struct tms t; |
| 1837 | clock_t c; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1838 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1839 | return NULL; |
| 1840 | errno = 0; |
| 1841 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1842 | if (c == (clock_t) -1) |
| 1843 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1844 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1845 | (double)t.tms_utime / HZ, |
| 1846 | (double)t.tms_stime / HZ, |
| 1847 | (double)t.tms_cutime / HZ, |
| 1848 | (double)t.tms_cstime / HZ, |
| 1849 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1850 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 1851 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1852 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 1853 | |
| 1854 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 1855 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 1856 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1857 | static PyObject * |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 1858 | posix_times(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1859 | PyObject *self; |
| 1860 | PyObject *args; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 1861 | { |
| 1862 | FILETIME create, exit, kernel, user; |
| 1863 | HANDLE hProc; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1864 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 1865 | return NULL; |
| 1866 | hProc = GetCurrentProcess(); |
| 1867 | GetProcessTimes(hProc,&create, &exit, &kernel, &user); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1868 | return Py_BuildValue( |
| 1869 | "ddddd", |
| 1870 | (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6, |
| 1871 | (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6, |
| 1872 | (double)0, |
| 1873 | (double)0, |
| 1874 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 1875 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1876 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 1877 | |
| 1878 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 1879 | static char posix_times__doc__[] = |
| 1880 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 1881 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 1882 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1883 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1884 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1885 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1886 | static char posix_setsid__doc__[] = |
| 1887 | "setsid() -> None\n\ |
| 1888 | Call the system call setsid()."; |
| 1889 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1890 | static PyObject * |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1891 | posix_setsid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1892 | PyObject *self; |
| 1893 | PyObject *args; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1894 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1895 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1896 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1897 | if (setsid() < 0) |
| 1898 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1899 | Py_INCREF(Py_None); |
| 1900 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1901 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1902 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1903 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1904 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1905 | static char posix_setpgid__doc__[] = |
| 1906 | "setpgid(pid, pgrp) -> None\n\ |
| 1907 | Call the system call setpgid()."; |
| 1908 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1909 | static PyObject * |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1910 | posix_setpgid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1911 | PyObject *self; |
| 1912 | PyObject *args; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1913 | { |
| 1914 | int pid, pgrp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1915 | if (!PyArg_Parse(args, "(ii)", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1916 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1917 | if (setpgid(pid, pgrp) < 0) |
| 1918 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1919 | Py_INCREF(Py_None); |
| 1920 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1921 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1922 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1923 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1924 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1925 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1926 | static char posix_tcgetpgrp__doc__[] = |
| 1927 | "tcgetpgrp(fd) -> pgid\n\ |
| 1928 | Return the process group associated with the terminal given by a fd."; |
| 1929 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1930 | static PyObject * |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1931 | posix_tcgetpgrp(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1932 | PyObject *self; |
| 1933 | PyObject *args; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1934 | { |
| 1935 | int fd, pgid; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1936 | if (!PyArg_Parse(args, "i", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1937 | return NULL; |
| 1938 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1939 | if (pgid < 0) |
| 1940 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1941 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1942 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1943 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1944 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1945 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1946 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1947 | static char posix_tcsetpgrp__doc__[] = |
| 1948 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 1949 | Set the process group associated with the terminal given by a fd."; |
| 1950 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1951 | static PyObject * |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1952 | posix_tcsetpgrp(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1953 | PyObject *self; |
| 1954 | PyObject *args; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1955 | { |
| 1956 | int fd, pgid; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1957 | if (!PyArg_Parse(args, "(ii)", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1958 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1959 | if (tcsetpgrp(fd, pgid) < 0) |
| 1960 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1961 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1962 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1963 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1964 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1965 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1966 | /* Functions acting on file descriptors */ |
| 1967 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1968 | static char posix_open__doc__[] = |
| 1969 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 1970 | Open a file (for low level IO)."; |
| 1971 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1972 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1973 | posix_open(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1974 | PyObject *self; |
| 1975 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1976 | { |
| 1977 | char *file; |
| 1978 | int flag; |
| 1979 | int mode = 0777; |
| 1980 | int fd; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1981 | if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode)) |
| 1982 | return NULL; |
| 1983 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1984 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1985 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1986 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1987 | if (fd < 0) |
| 1988 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1989 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1992 | |
| 1993 | static char posix_close__doc__[] = |
| 1994 | "close(fd) -> None\n\ |
| 1995 | Close a file descriptor (for low level IO)."; |
| 1996 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1997 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1998 | posix_close(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1999 | PyObject *self; |
| 2000 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2001 | { |
| 2002 | int fd, res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2003 | if (!PyArg_Parse(args, "i", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2004 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2005 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2006 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2007 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2008 | if (res < 0) |
| 2009 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2010 | Py_INCREF(Py_None); |
| 2011 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2014 | |
| 2015 | static char posix_dup__doc__[] = |
| 2016 | "dup(fd) -> fd2\n\ |
| 2017 | Return a duplicate of a file descriptor."; |
| 2018 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2019 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2020 | posix_dup(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2021 | PyObject *self; |
| 2022 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2023 | { |
| 2024 | int fd; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2025 | if (!PyArg_Parse(args, "i", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2026 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2027 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2028 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2029 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2030 | if (fd < 0) |
| 2031 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2032 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2035 | |
| 2036 | static char posix_dup2__doc__[] = |
| 2037 | "dup2(fd, fd2) -> None\n\ |
| 2038 | Duplicate file descriptor."; |
| 2039 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2040 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2041 | posix_dup2(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2042 | PyObject *self; |
| 2043 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2044 | { |
| 2045 | int fd, fd2, res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2046 | if (!PyArg_Parse(args, "(ii)", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2047 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2048 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2049 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2050 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2051 | if (res < 0) |
| 2052 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2053 | Py_INCREF(Py_None); |
| 2054 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2057 | |
| 2058 | static char posix_lseek__doc__[] = |
| 2059 | "lseek(fd, pos, how) -> newpos\n\ |
| 2060 | Set the current position of a file descriptor."; |
| 2061 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2062 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2063 | posix_lseek(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2064 | PyObject *self; |
| 2065 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2066 | { |
| 2067 | int fd, how; |
| 2068 | long pos, res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2069 | if (!PyArg_Parse(args, "(ili)", &fd, &pos, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2070 | return NULL; |
| 2071 | #ifdef SEEK_SET |
| 2072 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 2073 | switch (how) { |
| 2074 | case 0: how = SEEK_SET; break; |
| 2075 | case 1: how = SEEK_CUR; break; |
| 2076 | case 2: how = SEEK_END; break; |
| 2077 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2078 | #endif /* SEEK_END */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2079 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2080 | res = lseek(fd, pos, how); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2081 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2082 | if (res < 0) |
| 2083 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2084 | return PyInt_FromLong(res); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2087 | |
| 2088 | static char posix_read__doc__[] = |
| 2089 | "read(fd, buffersize) -> string\n\ |
| 2090 | Read a file descriptor."; |
| 2091 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2092 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2093 | posix_read(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2094 | PyObject *self; |
| 2095 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2096 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 2097 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2098 | PyObject *buffer; |
| 2099 | if (!PyArg_Parse(args, "(ii)", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2100 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2101 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2102 | if (buffer == NULL) |
| 2103 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2104 | Py_BEGIN_ALLOW_THREADS |
| 2105 | n = read(fd, PyString_AsString(buffer), size); |
| 2106 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 2107 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2108 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2109 | return posix_error(); |
| 2110 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 2111 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2112 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2113 | return buffer; |
| 2114 | } |
| 2115 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2116 | |
| 2117 | static char posix_write__doc__[] = |
| 2118 | "write(fd, string) -> byteswritten\n\ |
| 2119 | Write a string to a file descriptor."; |
| 2120 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2121 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2122 | posix_write(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2123 | PyObject *self; |
| 2124 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2125 | { |
| 2126 | int fd, size; |
| 2127 | char *buffer; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2128 | if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2129 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2130 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2131 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2132 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2133 | if (size < 0) |
| 2134 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2135 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2138 | |
| 2139 | static char posix_fstat__doc__[]= |
| 2140 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 2141 | Like stat(), but for an open file descriptor."; |
| 2142 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2143 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2144 | posix_fstat(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2145 | PyObject *self; |
| 2146 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2147 | { |
| 2148 | int fd; |
| 2149 | struct stat st; |
| 2150 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2151 | if (!PyArg_Parse(args, "i", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2152 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2153 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2154 | res = fstat(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2155 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2156 | if (res != 0) |
| 2157 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2158 | return Py_BuildValue("(llllllllll)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2159 | (long)st.st_mode, |
| 2160 | (long)st.st_ino, |
| 2161 | (long)st.st_dev, |
| 2162 | (long)st.st_nlink, |
| 2163 | (long)st.st_uid, |
| 2164 | (long)st.st_gid, |
| 2165 | (long)st.st_size, |
| 2166 | (long)st.st_atime, |
| 2167 | (long)st.st_mtime, |
| 2168 | (long)st.st_ctime); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2171 | |
| 2172 | static char posix_fdopen__doc__[] = |
| 2173 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 2174 | Return an open file object connected to a file descriptor."; |
| 2175 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2176 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2177 | posix_fdopen(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2178 | PyObject *self; |
| 2179 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2180 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2181 | extern int fclose Py_PROTO((FILE *)); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2182 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2183 | char *mode = "r"; |
| 2184 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2185 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2186 | PyObject *f; |
| 2187 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2188 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2189 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2190 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2191 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2192 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2193 | if (fp == NULL) |
| 2194 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2195 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2196 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2197 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2198 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2201 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2202 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2203 | static char posix_pipe__doc__[] = |
| 2204 | "pipe() -> (read_end, write_end)\n\ |
| 2205 | Create a pipe."; |
| 2206 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2207 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2208 | posix_pipe(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2209 | PyObject *self; |
| 2210 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2211 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2212 | #if defined(PYOS_OS2) |
| 2213 | HFILE read, write; |
| 2214 | APIRET rc; |
| 2215 | |
| 2216 | if (!PyArg_Parse(args, "")) |
| 2217 | return NULL; |
| 2218 | |
| 2219 | Py_BEGIN_ALLOW_THREADS |
| 2220 | rc = DosCreatePipe( &read, &write, 4096); |
| 2221 | Py_END_ALLOW_THREADS |
| 2222 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2223 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2224 | |
| 2225 | return Py_BuildValue("(ii)", read, write); |
| 2226 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2227 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2228 | int fds[2]; |
| 2229 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2230 | if (!PyArg_Parse(args, "")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2231 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2232 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2233 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2234 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2235 | if (res != 0) |
| 2236 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2237 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2238 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 2239 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 2240 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 2241 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2242 | if (!PyArg_Parse(args, "")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 2243 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2244 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 2245 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2246 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 2247 | if (!ok) |
| 2248 | return posix_error(); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 2249 | read_fd = _open_osfhandle((long)read, 0); |
| 2250 | write_fd = _open_osfhandle((long)write, 1); |
| 2251 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2252 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2253 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2254 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2255 | #endif /* HAVE_PIPE */ |
| 2256 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2257 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2258 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2259 | static char posix_mkfifo__doc__[] = |
| 2260 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 2261 | Create a FIFO (a POSIX named pipe)."; |
| 2262 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2263 | static PyObject * |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2264 | posix_mkfifo(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2265 | PyObject *self; |
| 2266 | PyObject *args; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2267 | { |
| 2268 | char *file; |
| 2269 | int mode = 0666; |
| 2270 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2271 | if (!PyArg_ParseTuple(args, "s|i", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2272 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2273 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2274 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2275 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2276 | if (res < 0) |
| 2277 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2278 | Py_INCREF(Py_None); |
| 2279 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2280 | } |
| 2281 | #endif |
| 2282 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2283 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2284 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2285 | static char posix_ftruncate__doc__[] = |
| 2286 | "ftruncate(fd, length) -> None\n\ |
| 2287 | Truncate a file to a specified length."; |
| 2288 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2289 | static PyObject * |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2290 | posix_ftruncate(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2291 | PyObject *self; /* Not used */ |
| 2292 | PyObject *args; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2293 | { |
| 2294 | int fd; |
| 2295 | long length; |
| 2296 | int res; |
| 2297 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2298 | if (!PyArg_Parse(args, "(il)", &fd, &length)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2299 | return NULL; |
| 2300 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2301 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2302 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2303 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2304 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2305 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2306 | return NULL; |
| 2307 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2308 | Py_INCREF(Py_None); |
| 2309 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2310 | } |
| 2311 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2313 | #ifdef NeXT |
| 2314 | #define HAVE_PUTENV |
| 2315 | /* Steve Spicklemire got this putenv from NeXTAnswers */ |
| 2316 | static int |
| 2317 | putenv(char *newval) |
| 2318 | { |
| 2319 | extern char **environ; |
| 2320 | |
| 2321 | static int firstTime = 1; |
| 2322 | char **ep; |
| 2323 | char *cp; |
| 2324 | int esiz; |
| 2325 | char *np; |
| 2326 | |
| 2327 | if (!(np = strchr(newval, '='))) |
| 2328 | return 1; |
| 2329 | *np = '\0'; |
| 2330 | |
| 2331 | /* look it up */ |
| 2332 | for (ep=environ ; *ep ; ep++) |
| 2333 | { |
| 2334 | /* this should always be true... */ |
| 2335 | if (cp = strchr(*ep, '=')) |
| 2336 | { |
| 2337 | *cp = '\0'; |
| 2338 | if (!strcmp(*ep, newval)) |
| 2339 | { |
| 2340 | /* got it! */ |
| 2341 | *cp = '='; |
| 2342 | break; |
| 2343 | } |
| 2344 | *cp = '='; |
| 2345 | } |
| 2346 | else |
| 2347 | { |
| 2348 | *np = '='; |
| 2349 | return 1; |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | *np = '='; |
| 2354 | if (*ep) |
| 2355 | { |
| 2356 | /* the string was already there: |
| 2357 | just replace it with the new one */ |
| 2358 | *ep = newval; |
| 2359 | return 0; |
| 2360 | } |
| 2361 | |
| 2362 | /* expand environ by one */ |
| 2363 | for (esiz=2, ep=environ ; *ep ; ep++) |
| 2364 | esiz++; |
| 2365 | if (firstTime) |
| 2366 | { |
| 2367 | char **epp; |
| 2368 | char **newenv; |
| 2369 | if (!(newenv = malloc(esiz * sizeof(char *)))) |
| 2370 | return 1; |
| 2371 | |
| 2372 | for (ep=environ, epp=newenv ; *ep ;) |
| 2373 | *epp++ = *ep++; |
| 2374 | *epp++ = newval; |
| 2375 | *epp = (char *) 0; |
| 2376 | environ = newenv; |
| 2377 | } |
| 2378 | else |
| 2379 | { |
| 2380 | if (!(environ = realloc(environ, esiz * sizeof(char *)))) |
| 2381 | return 1; |
| 2382 | environ[esiz - 2] = newval; |
| 2383 | environ[esiz - 1] = (char *) 0; |
| 2384 | firstTime = 0; |
| 2385 | } |
| 2386 | |
| 2387 | return 0; |
| 2388 | } |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 2389 | #endif /* NeXT */ |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2390 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2391 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2392 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2393 | static char posix_putenv__doc__[] = |
| 2394 | "putenv(key, value) -> None\n\ |
| 2395 | Change or add an environment variable."; |
| 2396 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2397 | static PyObject * |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 2398 | posix_putenv(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2399 | PyObject *self; |
| 2400 | PyObject *args; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2401 | { |
| 2402 | char *s1, *s2; |
| 2403 | char *new; |
| 2404 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2405 | if (!PyArg_ParseTuple(args, "ss", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2406 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2407 | |
| 2408 | #if defined(PYOS_OS2) |
| 2409 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 2410 | APIRET rc; |
| 2411 | |
| 2412 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 2413 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 2414 | |
| 2415 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 2416 | if (rc != NO_ERROR) |
| 2417 | return os2_error(rc); |
| 2418 | |
| 2419 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 2420 | APIRET rc; |
| 2421 | |
| 2422 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 2423 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 2424 | |
| 2425 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 2426 | if (rc != NO_ERROR) |
| 2427 | return os2_error(rc); |
| 2428 | } else { |
| 2429 | #endif |
| 2430 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2431 | /* XXX This leaks memory -- not easy to fix :-( */ |
| 2432 | if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2433 | return PyErr_NoMemory(); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2434 | (void) sprintf(new, "%s=%s", s1, s2); |
| 2435 | if (putenv(new)) { |
| 2436 | posix_error(); |
| 2437 | return NULL; |
| 2438 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2439 | |
| 2440 | #if defined(PYOS_OS2) |
| 2441 | } |
| 2442 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2443 | Py_INCREF(Py_None); |
| 2444 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2445 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 2446 | #endif /* putenv */ |
| 2447 | |
| 2448 | #ifdef HAVE_STRERROR |
| 2449 | static char posix_strerror__doc__[] = |
| 2450 | "strerror(code) -> string\n\ |
| 2451 | Translate an error code to a message string."; |
| 2452 | |
| 2453 | PyObject * |
| 2454 | posix_strerror(self, args) |
| 2455 | PyObject *self; |
| 2456 | PyObject *args; |
| 2457 | { |
| 2458 | int code; |
| 2459 | char *message; |
| 2460 | if (!PyArg_ParseTuple(args, "i", &code)) |
| 2461 | return NULL; |
| 2462 | message = strerror(code); |
| 2463 | if (message == NULL) { |
| 2464 | PyErr_SetString(PyExc_ValueError, |
| 2465 | "strerror code out of range"); |
| 2466 | return NULL; |
| 2467 | } |
| 2468 | return PyString_FromString(message); |
| 2469 | } |
| 2470 | #endif /* strerror */ |
| 2471 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2472 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2473 | static PyMethodDef posix_methods[] = { |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2474 | {"chdir", posix_chdir, 0, posix_chdir__doc__}, |
| 2475 | {"chmod", posix_chmod, 0, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2476 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2477 | {"chown", posix_chown, 0, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2478 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2479 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2480 | {"getcwd", posix_getcwd, 0, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2481 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2482 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2483 | {"link", posix_link, 0, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2484 | #endif /* HAVE_LINK */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2485 | {"listdir", posix_listdir, 0, posix_listdir__doc__}, |
| 2486 | {"lstat", posix_lstat, 0, posix_lstat__doc__}, |
| 2487 | {"mkdir", posix_mkdir, 1, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2488 | #ifdef HAVE_NICE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2489 | {"nice", posix_nice, 0, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2490 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2491 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2492 | {"readlink", posix_readlink, 0, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2493 | #endif /* HAVE_READLINK */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2494 | {"rename", posix_rename, 0, posix_rename__doc__}, |
| 2495 | {"rmdir", posix_rmdir, 0, posix_rmdir__doc__}, |
| 2496 | {"stat", posix_stat, 0, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2497 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2498 | {"symlink", posix_symlink, 0, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2499 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2500 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2501 | {"system", posix_system, 0, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2502 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2503 | {"umask", posix_umask, 0, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2504 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2505 | {"uname", posix_uname, 0, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2506 | #endif /* HAVE_UNAME */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2507 | {"unlink", posix_unlink, 0, posix_unlink__doc__}, |
| 2508 | {"remove", posix_unlink, 0, posix_remove__doc__}, |
| 2509 | {"utime", posix_utime, 0, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2510 | #ifdef HAVE_TIMES |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2511 | {"times", posix_times, 0, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2512 | #endif /* HAVE_TIMES */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2513 | {"_exit", posix__exit, 0, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2514 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2515 | {"execv", posix_execv, 0, posix_execv__doc__}, |
| 2516 | {"execve", posix_execve, 0, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2517 | #endif /* HAVE_EXECV */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2518 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2519 | {"fork", posix_fork, 0, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2520 | #endif /* HAVE_FORK */ |
| 2521 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2522 | {"getegid", posix_getegid, 0, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2523 | #endif /* HAVE_GETEGID */ |
| 2524 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2525 | {"geteuid", posix_geteuid, 0, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2526 | #endif /* HAVE_GETEUID */ |
| 2527 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2528 | {"getgid", posix_getgid, 0, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2529 | #endif /* HAVE_GETGID */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2530 | {"getpid", posix_getpid, 0, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2531 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2532 | {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2533 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2534 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2535 | {"getppid", posix_getppid, 0, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2536 | #endif /* HAVE_GETPPID */ |
| 2537 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2538 | {"getuid", posix_getuid, 0, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2539 | #endif /* HAVE_GETUID */ |
| 2540 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2541 | {"kill", posix_kill, 0, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2542 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2543 | #ifdef HAVE_PLOCK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2544 | {"plock", posix_plock, 0, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2545 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2546 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2547 | {"popen", posix_popen, 1, posix_popen__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2548 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2549 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2550 | {"setuid", posix_setuid, 0, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2551 | #endif /* HAVE_SETUID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2552 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2553 | {"setgid", posix_setgid, 0, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2554 | #endif /* HAVE_SETGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2555 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2556 | {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2557 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2558 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2559 | {"wait", posix_wait, 0, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2560 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2561 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2562 | {"waitpid", posix_waitpid, 0, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2563 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2564 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2565 | {"setsid", posix_setsid, 0, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2566 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2567 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2568 | {"setpgid", posix_setpgid, 0, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2569 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2570 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2571 | {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2572 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2573 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2574 | {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2575 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2576 | {"open", posix_open, 1, posix_open__doc__}, |
| 2577 | {"close", posix_close, 0, posix_close__doc__}, |
| 2578 | {"dup", posix_dup, 0, posix_dup__doc__}, |
| 2579 | {"dup2", posix_dup2, 0, posix_dup2__doc__}, |
| 2580 | {"lseek", posix_lseek, 0, posix_lseek__doc__}, |
| 2581 | {"read", posix_read, 0, posix_read__doc__}, |
| 2582 | {"write", posix_write, 0, posix_write__doc__}, |
| 2583 | {"fstat", posix_fstat, 0, posix_fstat__doc__}, |
| 2584 | {"fdopen", posix_fdopen, 1, posix_fdopen__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2585 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2586 | {"pipe", posix_pipe, 0, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2587 | #endif |
| 2588 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2589 | {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2590 | #endif |
| 2591 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2592 | {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2593 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2594 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2595 | {"putenv", posix_putenv, 1, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2596 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 2597 | #ifdef HAVE_STRERROR |
| 2598 | {"strerror", posix_strerror, 1, posix_strerror__doc__}, |
| 2599 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2600 | {NULL, NULL} /* Sentinel */ |
| 2601 | }; |
| 2602 | |
| 2603 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 2604 | static int |
| 2605 | ins(d, symbol, value) |
| 2606 | PyObject* d; |
| 2607 | char* symbol; |
| 2608 | long value; |
| 2609 | { |
| 2610 | PyObject* v = PyInt_FromLong(value); |
| 2611 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 2612 | return -1; /* triggers fatal error */ |
| 2613 | |
| 2614 | Py_DECREF(v); |
| 2615 | return 0; |
| 2616 | } |
| 2617 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2618 | #if defined(PYOS_OS2) |
| 2619 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 2620 | static int insertvalues(PyObject *d) |
| 2621 | { |
| 2622 | APIRET rc; |
| 2623 | ULONG values[QSV_MAX+1]; |
| 2624 | PyObject *v; |
| 2625 | char *ver, tmp[10]; |
| 2626 | |
| 2627 | Py_BEGIN_ALLOW_THREADS |
| 2628 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 2629 | Py_END_ALLOW_THREADS |
| 2630 | |
| 2631 | if (rc != NO_ERROR) { |
| 2632 | os2_error(rc); |
| 2633 | return -1; |
| 2634 | } |
| 2635 | |
| 2636 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 2637 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 2638 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 2639 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 2640 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 2641 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 2642 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 2643 | |
| 2644 | switch (values[QSV_VERSION_MINOR]) { |
| 2645 | case 0: ver = "2.00"; break; |
| 2646 | case 10: ver = "2.10"; break; |
| 2647 | case 11: ver = "2.11"; break; |
| 2648 | case 30: ver = "3.00"; break; |
| 2649 | case 40: ver = "4.00"; break; |
| 2650 | case 50: ver = "5.00"; break; |
| 2651 | default: |
| 2652 | sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR], |
| 2653 | values[QSV_VERSION_MINOR]); |
| 2654 | ver = &tmp[0]; |
| 2655 | } |
| 2656 | |
| 2657 | /* Add Indicator of the Version of the Operating System */ |
| 2658 | v = PyString_FromString(ver); |
| 2659 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 2660 | return -1; |
| 2661 | Py_DECREF(v); |
| 2662 | |
| 2663 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 2664 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 2665 | tmp[1] = ':'; |
| 2666 | tmp[2] = '\0'; |
| 2667 | |
| 2668 | v = PyString_FromString(tmp); |
| 2669 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 2670 | return -1; |
| 2671 | Py_DECREF(v); |
| 2672 | |
| 2673 | return 0; |
| 2674 | } |
| 2675 | #endif |
| 2676 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 2677 | static int |
| 2678 | all_ins(d) |
| 2679 | PyObject* d; |
| 2680 | { |
| 2681 | #ifdef WNOHANG |
| 2682 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
| 2683 | #endif |
| 2684 | #ifdef O_RDONLY |
| 2685 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 2686 | #endif |
| 2687 | #ifdef O_WRONLY |
| 2688 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 2689 | #endif |
| 2690 | #ifdef O_RDWR |
| 2691 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 2692 | #endif |
| 2693 | #ifdef O_NDELAY |
| 2694 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 2695 | #endif |
| 2696 | #ifdef O_NONBLOCK |
| 2697 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 2698 | #endif |
| 2699 | #ifdef O_APPEND |
| 2700 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 2701 | #endif |
| 2702 | #ifdef O_DSYNC |
| 2703 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 2704 | #endif |
| 2705 | #ifdef O_RSYNC |
| 2706 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 2707 | #endif |
| 2708 | #ifdef O_SYNC |
| 2709 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 2710 | #endif |
| 2711 | #ifdef O_NOCTTY |
| 2712 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 2713 | #endif |
| 2714 | #ifdef O_CREAT |
| 2715 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 2716 | #endif |
| 2717 | #ifdef O_EXCL |
| 2718 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 2719 | #endif |
| 2720 | #ifdef O_TRUNC |
| 2721 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 2722 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 2723 | #ifdef O_BINARY |
| 2724 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 2725 | #endif |
| 2726 | #ifdef O_TEXT |
| 2727 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 2728 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2729 | |
| 2730 | #if defined(PYOS_OS2) |
| 2731 | if (insertvalues(d)) return -1; |
| 2732 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 2733 | return 0; |
| 2734 | } |
| 2735 | |
| 2736 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2737 | #if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2738 | #define INITFUNC initnt |
| 2739 | #define MODNAME "nt" |
| 2740 | #else |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2741 | #if defined(PYOS_OS2) |
| 2742 | #define INITFUNC initos2 |
| 2743 | #define MODNAME "os2" |
| 2744 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2745 | #define INITFUNC initposix |
| 2746 | #define MODNAME "posix" |
| 2747 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2748 | #endif |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2749 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2750 | void |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2751 | INITFUNC() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2752 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2753 | PyObject *m, *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2754 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2755 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2756 | posix_methods, |
| 2757 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2758 | (PyObject *)NULL, |
| 2759 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2760 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2761 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2762 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2763 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2764 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2765 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2766 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2767 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 2768 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 2769 | return; |
| 2770 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2771 | /* Initialize exception */ |
| 2772 | PosixError = PyErr_NewException("os.error", NULL, NULL); |
| 2773 | if (PosixError != NULL) |
| 2774 | PyDict_SetItemString(d, "error", PosixError); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2775 | } |