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