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 | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 108 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_PIPE 1 |
| 111 | #define HAVE_POPEN 1 |
| 112 | #define HAVE_SYSTEM 1 |
| 113 | #else /* 16-bit Windows */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 114 | #endif /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 115 | #else /* all other compilers */ |
| 116 | /* Unix functions that the configure script doesn't check for */ |
| 117 | #define HAVE_EXECV 1 |
| 118 | #define HAVE_FORK 1 |
| 119 | #define HAVE_GETCWD 1 |
| 120 | #define HAVE_GETEGID 1 |
| 121 | #define HAVE_GETEUID 1 |
| 122 | #define HAVE_GETGID 1 |
| 123 | #define HAVE_GETPPID 1 |
| 124 | #define HAVE_GETUID 1 |
| 125 | #define HAVE_KILL 1 |
| 126 | #define HAVE_OPENDIR 1 |
| 127 | #define HAVE_PIPE 1 |
| 128 | #define HAVE_POPEN 1 |
| 129 | #define HAVE_SYSTEM 1 |
| 130 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 131 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 132 | #endif /* _MSC_VER */ |
| 133 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 134 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 135 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 139 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 140 | #include <unistd.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
| 143 | #ifdef NeXT |
| 144 | /* NeXT's <unistd.h> and <utime.h> aren't worth much */ |
| 145 | #undef HAVE_UNISTD_H |
| 146 | #undef HAVE_UTIME_H |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 147 | #define HAVE_WAITPID |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 148 | /* #undef HAVE_GETCWD */ |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 149 | #define UNION_WAIT /* This should really be checked for by autoconf */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
| 152 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 153 | /* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */ |
| 154 | extern int rename(); |
| 155 | extern int pclose(); |
| 156 | extern int lstat(); |
| 157 | extern int symlink(); |
Guido van Rossum | 8c67e4e | 1999-04-07 15:49:41 +0000 | [diff] [blame] | 158 | extern int fsync(); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 159 | #else /* !HAVE_UNISTD_H */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 160 | #if defined(PYCC_VACPP) |
| 161 | extern int mkdir Py_PROTO((char *)); |
| 162 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 163 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 164 | extern int mkdir Py_PROTO((const char *)); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 165 | #else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 166 | extern int mkdir Py_PROTO((const char *, mode_t)); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 167 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #endif |
| 169 | #if defined(__IBMC__) || defined(__IBMCPP__) |
| 170 | extern int chdir Py_PROTO((char *)); |
| 171 | extern int rmdir Py_PROTO((char *)); |
| 172 | #else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 173 | extern int chdir Py_PROTO((const char *)); |
| 174 | extern int rmdir Py_PROTO((const char *)); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 175 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 176 | extern int chmod Py_PROTO((const char *, mode_t)); |
| 177 | extern int chown Py_PROTO((const char *, uid_t, gid_t)); |
| 178 | extern char *getcwd Py_PROTO((char *, int)); |
| 179 | extern char *strerror Py_PROTO((int)); |
| 180 | extern int link Py_PROTO((const char *, const char *)); |
| 181 | extern int rename Py_PROTO((const char *, const char *)); |
| 182 | extern int stat Py_PROTO((const char *, struct stat *)); |
| 183 | extern int unlink Py_PROTO((const char *)); |
| 184 | extern int pclose Py_PROTO((FILE *)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 185 | #ifdef HAVE_SYMLINK |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 186 | extern int symlink Py_PROTO((const char *, const char *)); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 187 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 188 | #ifdef HAVE_LSTAT |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 189 | extern int lstat Py_PROTO((const char *, struct stat *)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 190 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 191 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 193 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | #ifdef HAVE_UTIME_H |
| 196 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 197 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 199 | #ifdef HAVE_SYS_UTIME_H |
| 200 | #include <sys/utime.h> |
| 201 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 202 | #endif /* HAVE_SYS_UTIME_H */ |
| 203 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 204 | #ifdef HAVE_SYS_TIMES_H |
| 205 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 206 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 207 | |
| 208 | #ifdef HAVE_SYS_PARAM_H |
| 209 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 210 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | |
| 212 | #ifdef HAVE_SYS_UTSNAME_H |
| 213 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | |
| 216 | #ifndef MAXPATHLEN |
| 217 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 218 | #endif /* MAXPATHLEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 220 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 222 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 223 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 224 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 225 | #include <direct.h> |
| 226 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 227 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 229 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 230 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 231 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #endif |
| 234 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 236 | #endif |
| 237 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 239 | #endif |
| 240 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 242 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #include <direct.h> |
| 244 | #include <io.h> |
| 245 | #include <process.h> |
| 246 | #include <windows.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 247 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 248 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 249 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 250 | #else /* 16-bit Windows */ |
| 251 | #include <dos.h> |
| 252 | #include <ctype.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 253 | #endif /* MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 254 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 256 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 258 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 260 | #ifdef UNION_WAIT |
| 261 | /* Emulate some macros on systems that have a union instead of macros */ |
| 262 | |
| 263 | #ifndef WIFEXITED |
| 264 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 265 | #endif |
| 266 | |
| 267 | #ifndef WEXITSTATUS |
| 268 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 269 | #endif |
| 270 | |
| 271 | #ifndef WTERMSIG |
| 272 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 273 | #endif |
| 274 | |
| 275 | #endif /* UNION_WAIT */ |
| 276 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 277 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 278 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 279 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 280 | #define USE_CTERMID_R |
| 281 | #endif |
| 282 | |
| 283 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 284 | #define USE_TMPNAM_R |
| 285 | #endif |
| 286 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 288 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 289 | #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 290 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 291 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 293 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 294 | convertenviron() |
| 295 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 296 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 297 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 298 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 299 | if (d == NULL) |
| 300 | return NULL; |
| 301 | if (environ == NULL) |
| 302 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 303 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 305 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 306 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 307 | char *p = strchr(*e, '='); |
| 308 | if (p == NULL) |
| 309 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 310 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 311 | if (k == NULL) { |
| 312 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 314 | } |
| 315 | v = PyString_FromString(p+1); |
| 316 | if (v == NULL) { |
| 317 | PyErr_Clear(); |
| 318 | Py_DECREF(k); |
| 319 | continue; |
| 320 | } |
| 321 | if (PyDict_GetItem(d, k) == NULL) { |
| 322 | if (PyDict_SetItem(d, k, v) != 0) |
| 323 | PyErr_Clear(); |
| 324 | } |
| 325 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 326 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 327 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 328 | #if defined(PYOS_OS2) |
| 329 | { |
| 330 | APIRET rc; |
| 331 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 332 | |
| 333 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 334 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 335 | PyObject *v = PyString_FromString(buffer); |
| 336 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 337 | Py_DECREF(v); |
| 338 | } |
| 339 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 340 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 341 | PyObject *v = PyString_FromString(buffer); |
| 342 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 343 | Py_DECREF(v); |
| 344 | } |
| 345 | } |
| 346 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 347 | return d; |
| 348 | } |
| 349 | |
| 350 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 351 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 352 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 353 | static PyObject * |
| 354 | posix_error() |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 355 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 356 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 358 | static PyObject * |
| 359 | posix_error_with_filename(name) |
| 360 | char* name; |
| 361 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 362 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 366 | #if defined(PYOS_OS2) |
| 367 | /********************************************************************** |
| 368 | * Helper Function to Trim and Format OS/2 Messages |
| 369 | **********************************************************************/ |
| 370 | static void |
| 371 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 372 | { |
| 373 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 374 | |
| 375 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 376 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 377 | |
| 378 | while (lastc > msgbuf && isspace(*lastc)) |
| 379 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 380 | } |
| 381 | |
| 382 | /* Add Optional Reason Text */ |
| 383 | if (reason) { |
| 384 | strcat(msgbuf, " : "); |
| 385 | strcat(msgbuf, reason); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /********************************************************************** |
| 390 | * Decode an OS/2 Operating System Error Code |
| 391 | * |
| 392 | * A convenience function to lookup an OS/2 error code and return a |
| 393 | * text message we can use to raise a Python exception. |
| 394 | * |
| 395 | * Notes: |
| 396 | * The messages for errors returned from the OS/2 kernel reside in |
| 397 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 398 | * |
| 399 | **********************************************************************/ |
| 400 | static char * |
| 401 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 402 | { |
| 403 | APIRET rc; |
| 404 | ULONG msglen; |
| 405 | |
| 406 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 407 | Py_BEGIN_ALLOW_THREADS |
| 408 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 409 | errorcode, "oso001.msg", &msglen); |
| 410 | Py_END_ALLOW_THREADS |
| 411 | |
| 412 | if (rc == NO_ERROR) |
| 413 | os2_formatmsg(msgbuf, msglen, reason); |
| 414 | else |
| 415 | sprintf(msgbuf, "unknown OS error #%d", errorcode); |
| 416 | |
| 417 | return msgbuf; |
| 418 | } |
| 419 | |
| 420 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 421 | errors are not in a global variable e.g. 'errno' nor are |
| 422 | they congruent with posix error numbers. */ |
| 423 | |
| 424 | static PyObject * os2_error(int code) |
| 425 | { |
| 426 | char text[1024]; |
| 427 | PyObject *v; |
| 428 | |
| 429 | os2_strerror(text, sizeof(text), code, ""); |
| 430 | |
| 431 | v = Py_BuildValue("(is)", code, text); |
| 432 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 433 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 434 | Py_DECREF(v); |
| 435 | } |
| 436 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 437 | } |
| 438 | |
| 439 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 440 | |
| 441 | /* POSIX generic methods */ |
| 442 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 443 | static PyObject * |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 444 | posix_int(args, format, func) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 445 | PyObject *args; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 446 | char *format; |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 447 | int (*func) Py_FPROTO((int)); |
| 448 | { |
| 449 | int fd; |
| 450 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 451 | if (!PyArg_ParseTuple(args, format, &fd)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 452 | return NULL; |
| 453 | Py_BEGIN_ALLOW_THREADS |
| 454 | res = (*func)(fd); |
| 455 | Py_END_ALLOW_THREADS |
| 456 | if (res < 0) |
| 457 | return posix_error(); |
| 458 | Py_INCREF(Py_None); |
| 459 | return Py_None; |
| 460 | } |
| 461 | |
| 462 | |
| 463 | static PyObject * |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 464 | posix_1str(args, format, func) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 465 | PyObject *args; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 466 | char *format; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 467 | int (*func) Py_FPROTO((const char *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 468 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 469 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 470 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 471 | if (!PyArg_ParseTuple(args, format, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 472 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 473 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 474 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 475 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 476 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 477 | return posix_error_with_filename(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 478 | Py_INCREF(Py_None); |
| 479 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 482 | static PyObject * |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 483 | posix_2str(args, format, func) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 484 | PyObject *args; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 485 | char *format; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 486 | int (*func) Py_FPROTO((const char *, const char *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 487 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 488 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 489 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 490 | if (!PyArg_ParseTuple(args, format, &path1, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 491 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 492 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 493 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 494 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 495 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 496 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 497 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 498 | Py_INCREF(Py_None); |
| 499 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 502 | static PyObject * |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 503 | posix_strint(args, format, func) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 504 | PyObject *args; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 505 | char *format; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 506 | int (*func) Py_FPROTO((const char *, int)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 507 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 508 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 509 | int i; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 510 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 511 | if (!PyArg_ParseTuple(args, format, &path, &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 512 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 513 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 514 | res = (*func)(path, i); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 515 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 516 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 517 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 518 | Py_INCREF(Py_None); |
| 519 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 522 | static PyObject * |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 523 | posix_strintint(args, format, func) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 524 | PyObject *args; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 525 | char *format; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 526 | int (*func) Py_FPROTO((const char *, int, int)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 527 | { |
| 528 | char *path; |
| 529 | int i,i2; |
| 530 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 531 | if (!PyArg_ParseTuple(args, format, &path, &i, &i2)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 532 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 533 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 534 | res = (*func)(path, i, i2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 535 | Py_END_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 536 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 537 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 538 | Py_INCREF(Py_None); |
| 539 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 542 | static PyObject * |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 543 | posix_do_stat(self, args, format, statfunc) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 544 | PyObject *self; |
| 545 | PyObject *args; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 546 | char *format; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 547 | int (*statfunc) Py_FPROTO((const char *, struct stat *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 548 | { |
| 549 | struct stat st; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 550 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 551 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame^] | 552 | |
| 553 | #ifdef MS_WIN32 |
| 554 | int pathlen; |
| 555 | char pathcopy[MAX_PATH]; |
| 556 | #endif /* MS_WIN32 */ |
| 557 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 558 | if (!PyArg_ParseTuple(args, format, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 559 | return NULL; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame^] | 560 | |
| 561 | #ifdef MS_WIN32 |
| 562 | pathlen = strlen(path); |
| 563 | /* the library call can blow up if the file name is too long! */ |
| 564 | if (pathlen > MAX_PATH) { |
| 565 | errno = ENAMETOOLONG; |
| 566 | return posix_error(); |
| 567 | } |
| 568 | |
| 569 | if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) { |
| 570 | /* exception for drive root */ |
| 571 | if (!((pathlen == 3) && |
| 572 | (path[1] == ':') && |
| 573 | (path[2] == '\\' || path[2] == '/'))) |
| 574 | { |
| 575 | strncpy(pathcopy, path, pathlen); |
| 576 | pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */ |
| 577 | path = pathcopy; |
| 578 | } |
| 579 | } |
| 580 | #endif /* MS_WIN32 */ |
| 581 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 582 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 583 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 584 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 585 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 586 | return posix_error_with_filename(path); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 587 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 588 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 589 | (long)st.st_mode, |
| 590 | (long)st.st_ino, |
| 591 | (long)st.st_dev, |
| 592 | (long)st.st_nlink, |
| 593 | (long)st.st_uid, |
| 594 | (long)st.st_gid, |
| 595 | (long)st.st_size, |
| 596 | (long)st.st_atime, |
| 597 | (long)st.st_mtime, |
| 598 | (long)st.st_ctime); |
| 599 | #else |
| 600 | return Py_BuildValue("(lLllllLlll)", |
| 601 | (long)st.st_mode, |
| 602 | (LONG_LONG)st.st_ino, |
| 603 | (long)st.st_dev, |
| 604 | (long)st.st_nlink, |
| 605 | (long)st.st_uid, |
| 606 | (long)st.st_gid, |
| 607 | (LONG_LONG)st.st_size, |
| 608 | (long)st.st_atime, |
| 609 | (long)st.st_mtime, |
| 610 | (long)st.st_ctime); |
| 611 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
| 615 | /* POSIX methods */ |
| 616 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 617 | static char posix_access__doc__[] = |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 618 | "access(path, mode) -> 1 if granted, 0 otherwise\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 619 | Test for access to a file."; |
| 620 | |
| 621 | static PyObject * |
| 622 | posix_access(self, args) |
| 623 | PyObject *self; |
| 624 | PyObject *args; |
| 625 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 626 | char *path; |
| 627 | int mode; |
| 628 | int res; |
| 629 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 630 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 631 | return NULL; |
| 632 | Py_BEGIN_ALLOW_THREADS |
| 633 | res = access(path, mode); |
| 634 | Py_END_ALLOW_THREADS |
| 635 | return(PyInt_FromLong(res == 0 ? 1L : 0L)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 638 | #ifndef F_OK |
| 639 | #define F_OK 0 |
| 640 | #endif |
| 641 | #ifndef R_OK |
| 642 | #define R_OK 4 |
| 643 | #endif |
| 644 | #ifndef W_OK |
| 645 | #define W_OK 2 |
| 646 | #endif |
| 647 | #ifndef X_OK |
| 648 | #define X_OK 1 |
| 649 | #endif |
| 650 | |
| 651 | #ifdef HAVE_TTYNAME |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 652 | static char posix_ttyname__doc__[] = |
Guido van Rossum | 61eeb04 | 1999-02-22 15:29:15 +0000 | [diff] [blame] | 653 | "ttyname(fd) -> String\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 654 | Return the name of the terminal device connected to 'fd'."; |
| 655 | |
| 656 | static PyObject * |
| 657 | posix_ttyname(self, args) |
| 658 | PyObject *self; |
| 659 | PyObject *args; |
| 660 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 661 | int id; |
| 662 | char *ret; |
| 663 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 664 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 665 | return NULL; |
| 666 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 667 | ret = ttyname(id); |
| 668 | if (ret == NULL) |
| 669 | return(posix_error()); |
| 670 | return(PyString_FromString(ret)); |
| 671 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 672 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 673 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 674 | #ifdef HAVE_CTERMID |
| 675 | static char posix_ctermid__doc__[] = |
| 676 | "ctermid() -> String\n\ |
| 677 | Return the name of the controlling terminal for this process."; |
| 678 | |
| 679 | static PyObject * |
| 680 | posix_ctermid(self, args) |
| 681 | PyObject *self; |
| 682 | PyObject *args; |
| 683 | { |
| 684 | char *ret; |
| 685 | char buffer[L_ctermid]; |
| 686 | |
| 687 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 688 | return NULL; |
| 689 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 690 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 691 | ret = ctermid_r(buffer); |
| 692 | #else |
| 693 | ret = ctermid(buffer); |
| 694 | #endif |
| 695 | if (ret == NULL) |
| 696 | return(posix_error()); |
| 697 | return(PyString_FromString(buffer)); |
| 698 | } |
| 699 | #endif |
| 700 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 701 | static char posix_chdir__doc__[] = |
| 702 | "chdir(path) -> None\n\ |
| 703 | Change the current working directory to the specified path."; |
| 704 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 705 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 706 | posix_chdir(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 707 | PyObject *self; |
| 708 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 709 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 710 | return posix_1str(args, "s:chdir", chdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 713 | |
| 714 | static char posix_chmod__doc__[] = |
| 715 | "chmod(path, mode) -> None\n\ |
| 716 | Change the access permissions of a file."; |
| 717 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 718 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 719 | posix_chmod(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 720 | PyObject *self; |
| 721 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 722 | { |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 723 | char *path; |
| 724 | int i; |
| 725 | int res; |
Guido van Rossum | 49679b4 | 2000-03-31 00:48:21 +0000 | [diff] [blame] | 726 | if (!PyArg_ParseTuple(args, "si", &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 727 | return NULL; |
| 728 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 729 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 730 | Py_END_ALLOW_THREADS |
| 731 | if (res < 0) |
| 732 | return posix_error_with_filename(path); |
| 733 | Py_INCREF(Py_None); |
| 734 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 737 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 738 | #ifdef HAVE_FSYNC |
| 739 | static char posix_fsync__doc__[] = |
| 740 | "fsync(fildes) -> None\n\ |
| 741 | force write of file with filedescriptor to disk."; |
| 742 | |
| 743 | static PyObject * |
| 744 | posix_fsync(self, args) |
| 745 | PyObject *self; |
| 746 | PyObject *args; |
| 747 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 748 | return posix_int(args, "i:fsync", fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 749 | } |
| 750 | #endif /* HAVE_FSYNC */ |
| 751 | |
| 752 | #ifdef HAVE_FDATASYNC |
| 753 | static char posix_fdatasync__doc__[] = |
| 754 | "fdatasync(fildes) -> None\n\ |
| 755 | force write of file with filedescriptor to disk.\n\ |
| 756 | does not force update of metadata."; |
| 757 | |
Guido van Rossum | 5d00b6d | 1999-01-08 21:28:05 +0000 | [diff] [blame] | 758 | extern int fdatasync(int); /* Prototype just in case */ |
| 759 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 760 | static PyObject * |
| 761 | posix_fdatasync(self, args) |
| 762 | PyObject *self; |
| 763 | PyObject *args; |
| 764 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 765 | return posix_int(args, "i:fdatasync", fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 766 | } |
| 767 | #endif /* HAVE_FDATASYNC */ |
| 768 | |
| 769 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 770 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 771 | static char posix_chown__doc__[] = |
| 772 | "chown(path, uid, gid) -> None\n\ |
| 773 | Change the owner and group id of path to the numeric uid and gid."; |
| 774 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 775 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 776 | posix_chown(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 777 | PyObject *self; |
| 778 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 779 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 780 | return posix_strintint(args, "sii:chown", chown); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 781 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 782 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 784 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 785 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 786 | static char posix_getcwd__doc__[] = |
| 787 | "getcwd() -> path\n\ |
| 788 | Return a string representing the current working directory."; |
| 789 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 790 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 791 | posix_getcwd(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 792 | PyObject *self; |
| 793 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 794 | { |
| 795 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 796 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 797 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 798 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 799 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 800 | res = getcwd(buf, sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 801 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 802 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 803 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 804 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 805 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 806 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 807 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 808 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 809 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 810 | static char posix_link__doc__[] = |
| 811 | "link(src, dst) -> None\n\ |
| 812 | Create a hard link to a file."; |
| 813 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 814 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 815 | posix_link(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 816 | PyObject *self; |
| 817 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 818 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 819 | return posix_2str(args, "ss:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 820 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 821 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 822 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 823 | |
| 824 | static char posix_listdir__doc__[] = |
| 825 | "listdir(path) -> list_of_strings\n\ |
| 826 | Return a list containing the names of the entries in the directory.\n\ |
| 827 | \n\ |
| 828 | path: path of directory to list\n\ |
| 829 | \n\ |
| 830 | The list is in arbitrary order. It does not include the special\n\ |
| 831 | entries '.' and '..' even if they are present in the directory."; |
| 832 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 833 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 834 | posix_listdir(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 835 | PyObject *self; |
| 836 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 837 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 838 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 839 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 840 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 841 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 842 | char *name; |
| 843 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 844 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 845 | HANDLE hFindFile; |
| 846 | WIN32_FIND_DATA FileData; |
| 847 | char namebuf[MAX_PATH+5]; |
| 848 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 849 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 850 | return NULL; |
| 851 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 852 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 853 | return NULL; |
| 854 | } |
| 855 | strcpy(namebuf, name); |
| 856 | if (namebuf[len-1] != '/' && namebuf[len-1] != '\\') |
| 857 | namebuf[len++] = '/'; |
| 858 | strcpy(namebuf + len, "*.*"); |
| 859 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 860 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 861 | return NULL; |
| 862 | |
| 863 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 864 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 865 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 866 | if (errno == ERROR_FILE_NOT_FOUND) |
| 867 | return PyList_New(0); |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 868 | return posix_error_with_filename(name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 869 | } |
| 870 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 871 | if (FileData.cFileName[0] == '.' && |
| 872 | (FileData.cFileName[1] == '\0' || |
| 873 | FileData.cFileName[1] == '.' && |
| 874 | FileData.cFileName[2] == '\0')) |
| 875 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 876 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 877 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 878 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 879 | d = NULL; |
| 880 | break; |
| 881 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 882 | if (PyList_Append(d, v) != 0) { |
| 883 | Py_DECREF(v); |
| 884 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 885 | d = NULL; |
| 886 | break; |
| 887 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 888 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 889 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 890 | |
| 891 | if (FindClose(hFindFile) == FALSE) { |
| 892 | errno = GetLastError(); |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 893 | return posix_error_with_filename(&name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 897 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 898 | #else /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 899 | #ifdef _MSC_VER /* 16-bit Windows */ |
| 900 | |
| 901 | #ifndef MAX_PATH |
| 902 | #define MAX_PATH 250 |
| 903 | #endif |
| 904 | char *name, *pt; |
| 905 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 906 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 907 | char namebuf[MAX_PATH+5]; |
| 908 | struct _find_t ep; |
| 909 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 910 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 911 | return NULL; |
| 912 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 913 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 914 | return NULL; |
| 915 | } |
| 916 | strcpy(namebuf, name); |
| 917 | for (pt = namebuf; *pt; pt++) |
| 918 | if (*pt == '/') |
| 919 | *pt = '\\'; |
| 920 | if (namebuf[len-1] != '\\') |
| 921 | namebuf[len++] = '\\'; |
| 922 | strcpy(namebuf + len, "*.*"); |
| 923 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 924 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 925 | return NULL; |
| 926 | |
| 927 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 928 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 929 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 930 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 931 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 932 | } |
| 933 | do { |
| 934 | if (ep.name[0] == '.' && |
| 935 | (ep.name[1] == '\0' || |
| 936 | ep.name[1] == '.' && |
| 937 | ep.name[2] == '\0')) |
| 938 | continue; |
| 939 | strcpy(namebuf, ep.name); |
| 940 | for (pt = namebuf; *pt; pt++) |
| 941 | if (isupper(*pt)) |
| 942 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 943 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 944 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 945 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 946 | d = NULL; |
| 947 | break; |
| 948 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 949 | if (PyList_Append(d, v) != 0) { |
| 950 | Py_DECREF(v); |
| 951 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 952 | d = NULL; |
| 953 | break; |
| 954 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 955 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 956 | } while (_dos_findnext(&ep) == 0); |
| 957 | |
| 958 | return d; |
| 959 | |
| 960 | #else |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 961 | #if defined(PYOS_OS2) |
| 962 | |
| 963 | #ifndef MAX_PATH |
| 964 | #define MAX_PATH CCHMAXPATH |
| 965 | #endif |
| 966 | char *name, *pt; |
| 967 | int len; |
| 968 | PyObject *d, *v; |
| 969 | char namebuf[MAX_PATH+5]; |
| 970 | HDIR hdir = 1; |
| 971 | ULONG srchcnt = 1; |
| 972 | FILEFINDBUF3 ep; |
| 973 | APIRET rc; |
| 974 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 975 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 976 | return NULL; |
| 977 | if (len >= MAX_PATH) { |
| 978 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 979 | return NULL; |
| 980 | } |
| 981 | strcpy(namebuf, name); |
| 982 | for (pt = namebuf; *pt; pt++) |
| 983 | if (*pt == '/') |
| 984 | *pt = '\\'; |
| 985 | if (namebuf[len-1] != '\\') |
| 986 | namebuf[len++] = '\\'; |
| 987 | strcpy(namebuf + len, "*.*"); |
| 988 | |
| 989 | if ((d = PyList_New(0)) == NULL) |
| 990 | return NULL; |
| 991 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 992 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 993 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 994 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 995 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 996 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 997 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 998 | |
| 999 | if (rc != NO_ERROR) { |
| 1000 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1001 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1004 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1005 | do { |
| 1006 | if (ep.achName[0] == '.' |
| 1007 | && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0')) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1008 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1009 | |
| 1010 | strcpy(namebuf, ep.achName); |
| 1011 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1012 | /* Leave Case of Name Alone -- In Native Form */ |
| 1013 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1014 | |
| 1015 | v = PyString_FromString(namebuf); |
| 1016 | if (v == NULL) { |
| 1017 | Py_DECREF(d); |
| 1018 | d = NULL; |
| 1019 | break; |
| 1020 | } |
| 1021 | if (PyList_Append(d, v) != 0) { |
| 1022 | Py_DECREF(v); |
| 1023 | Py_DECREF(d); |
| 1024 | d = NULL; |
| 1025 | break; |
| 1026 | } |
| 1027 | Py_DECREF(v); |
| 1028 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1029 | } |
| 1030 | |
| 1031 | return d; |
| 1032 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1033 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1034 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1035 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1036 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1037 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1038 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1039 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1040 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1041 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1042 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1043 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1044 | closedir(dirp); |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1048 | if (ep->d_name[0] == '.' && |
| 1049 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1050 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1051 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1052 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1053 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1054 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1055 | d = NULL; |
| 1056 | break; |
| 1057 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1058 | if (PyList_Append(d, v) != 0) { |
| 1059 | Py_DECREF(v); |
| 1060 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1061 | d = NULL; |
| 1062 | break; |
| 1063 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1064 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1065 | } |
| 1066 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1067 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1068 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1069 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1070 | #endif /* !PYOS_OS2 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1071 | #endif /* !_MSC_VER */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1072 | #endif /* !MS_WIN32 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1075 | static char posix_mkdir__doc__[] = |
| 1076 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1077 | Create a directory."; |
| 1078 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1079 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1080 | posix_mkdir(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1081 | PyObject *self; |
| 1082 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1083 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1084 | int res; |
| 1085 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1086 | int mode = 0777; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1087 | if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1088 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1089 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1090 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1091 | res = mkdir(path); |
| 1092 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1093 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1094 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1095 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1096 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1097 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1098 | Py_INCREF(Py_None); |
| 1099 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1102 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1103 | #ifdef HAVE_NICE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1104 | static char posix_nice__doc__[] = |
| 1105 | "nice(inc) -> new_priority\n\ |
| 1106 | Decrease the priority of process and return new priority."; |
| 1107 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1108 | static PyObject * |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1109 | posix_nice(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1110 | PyObject *self; |
| 1111 | PyObject *args; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1112 | { |
| 1113 | int increment, value; |
| 1114 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1115 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1116 | return NULL; |
| 1117 | value = nice(increment); |
| 1118 | if (value == -1) |
| 1119 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1120 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1121 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1122 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1123 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1124 | |
| 1125 | static char posix_rename__doc__[] = |
| 1126 | "rename(old, new) -> None\n\ |
| 1127 | Rename a file or directory."; |
| 1128 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1129 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1130 | posix_rename(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1131 | PyObject *self; |
| 1132 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1133 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1134 | return posix_2str(args, "ss:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1137 | |
| 1138 | static char posix_rmdir__doc__[] = |
| 1139 | "rmdir(path) -> None\n\ |
| 1140 | Remove a directory."; |
| 1141 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1142 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1143 | posix_rmdir(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1144 | PyObject *self; |
| 1145 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1146 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1147 | return posix_1str(args, "s:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1150 | |
| 1151 | static char posix_stat__doc__[] = |
| 1152 | "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 1153 | Perform a stat system call on the given path."; |
| 1154 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1155 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1156 | posix_stat(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1157 | PyObject *self; |
| 1158 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1159 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1160 | return posix_do_stat(self, args, "s:stat", stat); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1163 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1164 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1165 | static char posix_system__doc__[] = |
| 1166 | "system(command) -> exit_status\n\ |
| 1167 | Execute the command (a string) in a subshell."; |
| 1168 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1169 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1170 | posix_system(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1171 | PyObject *self; |
| 1172 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1173 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1174 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1175 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1176 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1177 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1178 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1179 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1180 | Py_END_ALLOW_THREADS |
| 1181 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1182 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1183 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1184 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1185 | |
| 1186 | static char posix_umask__doc__[] = |
| 1187 | "umask(new_mask) -> old_mask\n\ |
| 1188 | Set the current numeric umask and return the previous umask."; |
| 1189 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1190 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1191 | posix_umask(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1192 | PyObject *self; |
| 1193 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1194 | { |
| 1195 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1196 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1197 | return NULL; |
| 1198 | i = umask(i); |
| 1199 | if (i < 0) |
| 1200 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1201 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1204 | |
| 1205 | static char posix_unlink__doc__[] = |
| 1206 | "unlink(path) -> None\n\ |
| 1207 | Remove a file (same as remove(path))."; |
| 1208 | |
| 1209 | static char posix_remove__doc__[] = |
| 1210 | "remove(path) -> None\n\ |
| 1211 | Remove a file (same as unlink(path))."; |
| 1212 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1213 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1214 | posix_unlink(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1215 | PyObject *self; |
| 1216 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1217 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1218 | return posix_1str(args, "s:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1221 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1222 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1223 | static char posix_uname__doc__[] = |
| 1224 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1225 | Return a tuple identifying the current operating system."; |
| 1226 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1227 | static PyObject * |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1228 | posix_uname(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1229 | PyObject *self; |
| 1230 | PyObject *args; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1231 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1232 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1233 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1234 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1235 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1236 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1237 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1238 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1239 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1240 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1241 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1242 | u.sysname, |
| 1243 | u.nodename, |
| 1244 | u.release, |
| 1245 | u.version, |
| 1246 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1247 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1248 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1249 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1250 | |
| 1251 | static char posix_utime__doc__[] = |
| 1252 | "utime(path, (atime, utime)) -> None\n\ |
| 1253 | Set the access and modified time of the file to the given values."; |
| 1254 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1255 | static PyObject * |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1256 | posix_utime(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1257 | PyObject *self; |
| 1258 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1259 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1260 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1261 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1262 | int res; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1263 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1264 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1265 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1266 | struct utimbuf buf; |
| 1267 | #define ATIME buf.actime |
| 1268 | #define MTIME buf.modtime |
| 1269 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1270 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1271 | time_t buf[2]; |
| 1272 | #define ATIME buf[0] |
| 1273 | #define MTIME buf[1] |
| 1274 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1275 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1276 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1277 | if (!PyArg_ParseTuple(args, "s(ll):utime", &path, &atime, &mtime)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1278 | return NULL; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1279 | ATIME = atime; |
Guido van Rossum | d1b3481 | 1995-02-07 15:39:29 +0000 | [diff] [blame] | 1280 | MTIME = mtime; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1281 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1282 | res = utime(path, UTIME_ARG); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1283 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1284 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1285 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1286 | Py_INCREF(Py_None); |
| 1287 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1288 | #undef UTIME_ARG |
| 1289 | #undef ATIME |
| 1290 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1293 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1294 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1295 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1296 | static char posix__exit__doc__[] = |
| 1297 | "_exit(status)\n\ |
| 1298 | Exit to the system with specified status, without normal exit processing."; |
| 1299 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1300 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1301 | posix__exit(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1302 | PyObject *self; |
| 1303 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1304 | { |
| 1305 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1306 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1307 | return NULL; |
| 1308 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1309 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1312 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1313 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1314 | static char posix_execv__doc__[] = |
| 1315 | "execv(path, args)\n\ |
| 1316 | Execute an executable path with arguments, replacing current process.\n\ |
| 1317 | \n\ |
| 1318 | path: path of executable file\n\ |
| 1319 | args: tuple or list of strings"; |
| 1320 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1321 | static PyObject * |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1322 | posix_execv(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1323 | PyObject *self; |
| 1324 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1325 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1326 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1327 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1328 | char **argvlist; |
| 1329 | int i, argc; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1330 | PyObject *(*getitem) Py_PROTO((PyObject *, int)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1331 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1332 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1333 | argv is a list or tuple of strings. */ |
| 1334 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1335 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1336 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1337 | if (PyList_Check(argv)) { |
| 1338 | argc = PyList_Size(argv); |
| 1339 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1340 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1341 | else if (PyTuple_Check(argv)) { |
| 1342 | argc = PyTuple_Size(argv); |
| 1343 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1344 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1345 | else { |
| 1346 | badarg: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1347 | PyErr_BadArgument(); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1348 | return NULL; |
| 1349 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1350 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1351 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1352 | if (argvlist == NULL) |
| 1353 | return NULL; |
| 1354 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1355 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1356 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1357 | goto badarg; |
| 1358 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1359 | } |
| 1360 | argvlist[argc] = NULL; |
| 1361 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1362 | #ifdef BAD_EXEC_PROTOTYPES |
| 1363 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1364 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1365 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1366 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1367 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1368 | /* If we get here it's definitely an error */ |
| 1369 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1370 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1371 | return posix_error(); |
| 1372 | } |
| 1373 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1374 | |
| 1375 | static char posix_execve__doc__[] = |
| 1376 | "execve(path, args, env)\n\ |
| 1377 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1378 | \n\ |
| 1379 | path: path of executable file\n\ |
| 1380 | args: tuple or list of arguments\n\ |
| 1381 | env: dictonary of strings mapping to strings"; |
| 1382 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1383 | static PyObject * |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1384 | posix_execve(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1385 | PyObject *self; |
| 1386 | PyObject *args; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1387 | { |
| 1388 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1389 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1390 | char **argvlist; |
| 1391 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1392 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1393 | int i, pos, argc, envc; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1394 | PyObject *(*getitem) Py_PROTO((PyObject *, int)); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1395 | |
| 1396 | /* execve has three arguments: (path, argv, env), where |
| 1397 | argv is a list or tuple of strings and env is a dictionary |
| 1398 | like posix.environ. */ |
| 1399 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1400 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1401 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1402 | if (PyList_Check(argv)) { |
| 1403 | argc = PyList_Size(argv); |
| 1404 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1405 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1406 | else if (PyTuple_Check(argv)) { |
| 1407 | argc = PyTuple_Size(argv); |
| 1408 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1409 | } |
| 1410 | else { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1411 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1412 | return NULL; |
| 1413 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1414 | if (!PyMapping_Check(env)) { |
| 1415 | PyErr_SetString(PyExc_TypeError, "env must be mapping object"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1416 | return NULL; |
| 1417 | } |
| 1418 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1419 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1420 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1421 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1422 | return NULL; |
| 1423 | } |
| 1424 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1425 | if (!PyArg_Parse((*getitem)(argv, i), |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1426 | "s;argv must be list of strings", |
| 1427 | &argvlist[i])) |
| 1428 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1429 | goto fail_1; |
| 1430 | } |
| 1431 | } |
| 1432 | argvlist[argc] = NULL; |
| 1433 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1434 | i = PyMapping_Length(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1435 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1436 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1437 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1438 | goto fail_1; |
| 1439 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1440 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1441 | keys = PyMapping_Keys(env); |
| 1442 | vals = PyMapping_Values(env); |
| 1443 | if (!keys || !vals) |
| 1444 | goto fail_2; |
| 1445 | |
| 1446 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1447 | char *p, *k, *v; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1448 | |
| 1449 | key = PyList_GetItem(keys, pos); |
| 1450 | val = PyList_GetItem(vals, pos); |
| 1451 | if (!key || !val) |
| 1452 | goto fail_2; |
| 1453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1454 | if (!PyArg_Parse(key, "s;non-string key in env", &k) || |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1455 | !PyArg_Parse(val, "s;non-string value in env", &v)) |
| 1456 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1457 | goto fail_2; |
| 1458 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1459 | |
| 1460 | #if defined(PYOS_OS2) |
| 1461 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1462 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1463 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1464 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1465 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1466 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1467 | goto fail_2; |
| 1468 | } |
| 1469 | sprintf(p, "%s=%s", k, v); |
| 1470 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1471 | #if defined(PYOS_OS2) |
| 1472 | } |
| 1473 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1474 | } |
| 1475 | envlist[envc] = 0; |
| 1476 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1477 | |
| 1478 | #ifdef BAD_EXEC_PROTOTYPES |
| 1479 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1480 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1481 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1482 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1483 | |
| 1484 | /* If we get here it's definitely an error */ |
| 1485 | |
| 1486 | (void) posix_error(); |
| 1487 | |
| 1488 | fail_2: |
| 1489 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1490 | PyMem_DEL(envlist[envc]); |
| 1491 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1492 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1493 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1494 | Py_XDECREF(vals); |
| 1495 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1496 | return NULL; |
| 1497 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1498 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1499 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1500 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1501 | #ifdef HAVE_SPAWNV |
| 1502 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1503 | "spawnv(mode, path, args)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1504 | Execute an executable path with arguments, replacing current process.\n\ |
| 1505 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1506 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1507 | path: path of executable file\n\ |
| 1508 | args: tuple or list of strings"; |
| 1509 | |
| 1510 | static PyObject * |
| 1511 | posix_spawnv(self, args) |
| 1512 | PyObject *self; |
| 1513 | PyObject *args; |
| 1514 | { |
| 1515 | char *path; |
| 1516 | PyObject *argv; |
| 1517 | char **argvlist; |
| 1518 | int mode, i, argc; |
| 1519 | PyObject *(*getitem) Py_PROTO((PyObject *, int)); |
| 1520 | |
| 1521 | /* spawnv has three arguments: (mode, path, argv), where |
| 1522 | argv is a list or tuple of strings. */ |
| 1523 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1524 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1525 | return NULL; |
| 1526 | if (PyList_Check(argv)) { |
| 1527 | argc = PyList_Size(argv); |
| 1528 | getitem = PyList_GetItem; |
| 1529 | } |
| 1530 | else if (PyTuple_Check(argv)) { |
| 1531 | argc = PyTuple_Size(argv); |
| 1532 | getitem = PyTuple_GetItem; |
| 1533 | } |
| 1534 | else { |
| 1535 | badarg: |
| 1536 | PyErr_BadArgument(); |
| 1537 | return NULL; |
| 1538 | } |
| 1539 | |
| 1540 | argvlist = PyMem_NEW(char *, argc+1); |
| 1541 | if (argvlist == NULL) |
| 1542 | return NULL; |
| 1543 | for (i = 0; i < argc; i++) { |
| 1544 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1545 | PyMem_DEL(argvlist); |
| 1546 | goto badarg; |
| 1547 | } |
| 1548 | } |
| 1549 | argvlist[argc] = NULL; |
| 1550 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1551 | if (mode == _OLD_P_OVERLAY) |
| 1552 | mode = _P_OVERLAY; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1553 | i = _spawnv(mode, path, argvlist); |
| 1554 | |
| 1555 | PyMem_DEL(argvlist); |
| 1556 | |
| 1557 | if (i == -1) |
| 1558 | return posix_error(); |
| 1559 | else |
| 1560 | return Py_BuildValue("i", i); |
| 1561 | } |
| 1562 | |
| 1563 | |
| 1564 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1565 | "spawnve(mode, path, args, env)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1566 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1567 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1568 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1569 | path: path of executable file\n\ |
| 1570 | args: tuple or list of arguments\n\ |
| 1571 | env: dictonary of strings mapping to strings"; |
| 1572 | |
| 1573 | static PyObject * |
| 1574 | posix_spawnve(self, args) |
| 1575 | PyObject *self; |
| 1576 | PyObject *args; |
| 1577 | { |
| 1578 | char *path; |
| 1579 | PyObject *argv, *env; |
| 1580 | char **argvlist; |
| 1581 | char **envlist; |
| 1582 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1583 | int mode, i, pos, argc, envc; |
| 1584 | PyObject *(*getitem) Py_PROTO((PyObject *, int)); |
| 1585 | |
| 1586 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1587 | argv is a list or tuple of strings and env is a dictionary |
| 1588 | like posix.environ. */ |
| 1589 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1590 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1591 | return NULL; |
| 1592 | if (PyList_Check(argv)) { |
| 1593 | argc = PyList_Size(argv); |
| 1594 | getitem = PyList_GetItem; |
| 1595 | } |
| 1596 | else if (PyTuple_Check(argv)) { |
| 1597 | argc = PyTuple_Size(argv); |
| 1598 | getitem = PyTuple_GetItem; |
| 1599 | } |
| 1600 | else { |
| 1601 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | if (!PyMapping_Check(env)) { |
| 1605 | PyErr_SetString(PyExc_TypeError, "env must be mapping object"); |
| 1606 | return NULL; |
| 1607 | } |
| 1608 | |
| 1609 | argvlist = PyMem_NEW(char *, argc+1); |
| 1610 | if (argvlist == NULL) { |
| 1611 | PyErr_NoMemory(); |
| 1612 | return NULL; |
| 1613 | } |
| 1614 | for (i = 0; i < argc; i++) { |
| 1615 | if (!PyArg_Parse((*getitem)(argv, i), |
| 1616 | "s;argv must be list of strings", |
| 1617 | &argvlist[i])) |
| 1618 | { |
| 1619 | goto fail_1; |
| 1620 | } |
| 1621 | } |
| 1622 | argvlist[argc] = NULL; |
| 1623 | |
| 1624 | i = PyMapping_Length(env); |
| 1625 | envlist = PyMem_NEW(char *, i + 1); |
| 1626 | if (envlist == NULL) { |
| 1627 | PyErr_NoMemory(); |
| 1628 | goto fail_1; |
| 1629 | } |
| 1630 | envc = 0; |
| 1631 | keys = PyMapping_Keys(env); |
| 1632 | vals = PyMapping_Values(env); |
| 1633 | if (!keys || !vals) |
| 1634 | goto fail_2; |
| 1635 | |
| 1636 | for (pos = 0; pos < i; pos++) { |
| 1637 | char *p, *k, *v; |
| 1638 | |
| 1639 | key = PyList_GetItem(keys, pos); |
| 1640 | val = PyList_GetItem(vals, pos); |
| 1641 | if (!key || !val) |
| 1642 | goto fail_2; |
| 1643 | |
| 1644 | if (!PyArg_Parse(key, "s;non-string key in env", &k) || |
| 1645 | !PyArg_Parse(val, "s;non-string value in env", &v)) |
| 1646 | { |
| 1647 | goto fail_2; |
| 1648 | } |
| 1649 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
| 1650 | if (p == NULL) { |
| 1651 | PyErr_NoMemory(); |
| 1652 | goto fail_2; |
| 1653 | } |
| 1654 | sprintf(p, "%s=%s", k, v); |
| 1655 | envlist[envc++] = p; |
| 1656 | } |
| 1657 | envlist[envc] = 0; |
| 1658 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1659 | if (mode == _OLD_P_OVERLAY) |
| 1660 | mode = _P_OVERLAY; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1661 | i = _spawnve(mode, path, argvlist, envlist); |
| 1662 | if (i == -1) |
| 1663 | (void) posix_error(); |
| 1664 | else |
| 1665 | res = Py_BuildValue("i", i); |
| 1666 | |
| 1667 | fail_2: |
| 1668 | while (--envc >= 0) |
| 1669 | PyMem_DEL(envlist[envc]); |
| 1670 | PyMem_DEL(envlist); |
| 1671 | fail_1: |
| 1672 | PyMem_DEL(argvlist); |
| 1673 | Py_XDECREF(vals); |
| 1674 | Py_XDECREF(keys); |
| 1675 | return res; |
| 1676 | } |
| 1677 | #endif /* HAVE_SPAWNV */ |
| 1678 | |
| 1679 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1680 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1681 | static char posix_fork__doc__[] = |
| 1682 | "fork() -> pid\n\ |
| 1683 | Fork a child process.\n\ |
| 1684 | \n\ |
| 1685 | Return 0 to child process and PID of child to parent process."; |
| 1686 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1687 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1688 | posix_fork(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1689 | PyObject *self; |
| 1690 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1691 | { |
| 1692 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1693 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1694 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1695 | pid = fork(); |
| 1696 | if (pid == -1) |
| 1697 | return posix_error(); |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 1698 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1699 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1700 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1701 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1702 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1703 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1704 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1705 | static char posix_getegid__doc__[] = |
| 1706 | "getegid() -> egid\n\ |
| 1707 | Return the current process's effective group id."; |
| 1708 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1709 | static PyObject * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1710 | posix_getegid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1711 | PyObject *self; |
| 1712 | PyObject *args; |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1713 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1714 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1715 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1716 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1717 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1718 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1719 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1720 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1721 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1722 | static char posix_geteuid__doc__[] = |
| 1723 | "geteuid() -> euid\n\ |
| 1724 | Return the current process's effective user id."; |
| 1725 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1726 | static PyObject * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1727 | posix_geteuid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1728 | PyObject *self; |
| 1729 | PyObject *args; |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1730 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1731 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1732 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1733 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1734 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1735 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1736 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1737 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1738 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1739 | static char posix_getgid__doc__[] = |
| 1740 | "getgid() -> gid\n\ |
| 1741 | Return the current process's group id."; |
| 1742 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1743 | static PyObject * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1744 | posix_getgid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1745 | PyObject *self; |
| 1746 | PyObject *args; |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1747 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1748 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1749 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1750 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1751 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1752 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1753 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1754 | |
| 1755 | static char posix_getpid__doc__[] = |
| 1756 | "getpid() -> pid\n\ |
| 1757 | Return the current process id"; |
| 1758 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1759 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1760 | posix_getpid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1761 | PyObject *self; |
| 1762 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1763 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1764 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1765 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1766 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1769 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 1770 | #ifdef HAVE_GETGROUPS |
| 1771 | static char posix_getgroups__doc__[] = "\ |
| 1772 | getgroups() -> list of group IDs\n\ |
| 1773 | Return list of supplemental group IDs for the process."; |
| 1774 | |
| 1775 | static PyObject * |
| 1776 | posix_getgroups(self, args) |
| 1777 | PyObject *self; |
| 1778 | PyObject *args; |
| 1779 | { |
| 1780 | PyObject *result = NULL; |
| 1781 | |
| 1782 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 1783 | #ifdef NGROUPS_MAX |
| 1784 | #define MAX_GROUPS NGROUPS_MAX |
| 1785 | #else |
| 1786 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 1787 | #define MAX_GROUPS 64 |
| 1788 | #endif |
| 1789 | gid_t grouplist[MAX_GROUPS]; |
| 1790 | int n; |
| 1791 | |
| 1792 | n = getgroups(MAX_GROUPS, grouplist); |
| 1793 | if (n < 0) |
| 1794 | posix_error(); |
| 1795 | else { |
| 1796 | result = PyList_New(n); |
| 1797 | if (result != NULL) { |
| 1798 | PyObject *o; |
| 1799 | int i; |
| 1800 | for (i = 0; i < n; ++i) { |
| 1801 | o = PyInt_FromLong((long)grouplist[i]); |
| 1802 | if (o == NULL) { |
| 1803 | Py_DECREF(result); |
| 1804 | result = NULL; |
| 1805 | break; |
| 1806 | } |
| 1807 | PyList_SET_ITEM(result, i, o); |
| 1808 | } |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | return result; |
| 1813 | } |
| 1814 | #endif |
| 1815 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1816 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1817 | static char posix_getpgrp__doc__[] = |
| 1818 | "getpgrp() -> pgrp\n\ |
| 1819 | Return the current process group id."; |
| 1820 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1821 | static PyObject * |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1822 | posix_getpgrp(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1823 | PyObject *self; |
| 1824 | PyObject *args; |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1825 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1826 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1827 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1828 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1829 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1830 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1831 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1832 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1833 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1834 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1835 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1836 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1837 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1838 | static char posix_setpgrp__doc__[] = |
| 1839 | "setpgrp() -> None\n\ |
| 1840 | Make this process a session leader."; |
| 1841 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1842 | static PyObject * |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1843 | posix_setpgrp(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1844 | PyObject *self; |
| 1845 | PyObject *args; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1846 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1847 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1848 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1849 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1850 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1851 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1852 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1853 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1854 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1855 | Py_INCREF(Py_None); |
| 1856 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1859 | #endif /* HAVE_SETPGRP */ |
| 1860 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1861 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1862 | static char posix_getppid__doc__[] = |
| 1863 | "getppid() -> ppid\n\ |
| 1864 | Return the parent's process id."; |
| 1865 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1866 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1867 | posix_getppid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1868 | PyObject *self; |
| 1869 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1870 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1871 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1872 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1873 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1874 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1875 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1876 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1877 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1878 | #ifdef HAVE_GETLOGIN |
| 1879 | static char posix_getlogin__doc__[] = "\ |
| 1880 | getlogin() -> string\n\ |
| 1881 | Return the actual login name."; |
| 1882 | |
| 1883 | static PyObject * |
| 1884 | posix_getlogin(self, args) |
| 1885 | PyObject *self; |
| 1886 | PyObject *args; |
| 1887 | { |
| 1888 | PyObject *result = NULL; |
| 1889 | |
| 1890 | if (PyArg_ParseTuple(args, ":getlogin")) { |
| 1891 | char *name = getlogin(); |
| 1892 | |
| 1893 | if (name == NULL) |
| 1894 | posix_error(); |
| 1895 | else |
| 1896 | result = PyString_FromString(name); |
| 1897 | } |
| 1898 | return result; |
| 1899 | } |
| 1900 | #endif |
| 1901 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1902 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1903 | static char posix_getuid__doc__[] = |
| 1904 | "getuid() -> uid\n\ |
| 1905 | Return the current process's user id."; |
| 1906 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1907 | static PyObject * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1908 | posix_getuid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1909 | PyObject *self; |
| 1910 | PyObject *args; |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1911 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1912 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1913 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1914 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1915 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1916 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1917 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1918 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1919 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1920 | static char posix_kill__doc__[] = |
| 1921 | "kill(pid, sig) -> None\n\ |
| 1922 | Kill a process with a signal."; |
| 1923 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1924 | static PyObject * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1925 | posix_kill(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1926 | PyObject *self; |
| 1927 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1928 | { |
| 1929 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1930 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1931 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1932 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1933 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 1934 | APIRET rc; |
| 1935 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1936 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1937 | |
| 1938 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 1939 | APIRET rc; |
| 1940 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1941 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1942 | |
| 1943 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1944 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1945 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1946 | if (kill(pid, sig) == -1) |
| 1947 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1948 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1949 | Py_INCREF(Py_None); |
| 1950 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1951 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1952 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1953 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1954 | #ifdef HAVE_PLOCK |
| 1955 | |
| 1956 | #ifdef HAVE_SYS_LOCK_H |
| 1957 | #include <sys/lock.h> |
| 1958 | #endif |
| 1959 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1960 | static char posix_plock__doc__[] = |
| 1961 | "plock(op) -> None\n\ |
| 1962 | Lock program segments into memory."; |
| 1963 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1964 | static PyObject * |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1965 | posix_plock(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1966 | PyObject *self; |
| 1967 | PyObject *args; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1968 | { |
| 1969 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1970 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1971 | return NULL; |
| 1972 | if (plock(op) == -1) |
| 1973 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1974 | Py_INCREF(Py_None); |
| 1975 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1976 | } |
| 1977 | #endif |
| 1978 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1979 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1980 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1981 | static char posix_popen__doc__[] = |
| 1982 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 1983 | Open a pipe to/from a command returning a file object."; |
| 1984 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1985 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1986 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1987 | async_system(const char *command) |
| 1988 | { |
| 1989 | char *p, errormsg[256], args[1024]; |
| 1990 | RESULTCODES rcodes; |
| 1991 | APIRET rc; |
| 1992 | char *shell = getenv("COMSPEC"); |
| 1993 | if (!shell) |
| 1994 | shell = "cmd"; |
| 1995 | |
| 1996 | strcpy(args, shell); |
| 1997 | p = &args[ strlen(args)+1 ]; |
| 1998 | strcpy(p, "/c "); |
| 1999 | strcat(p, command); |
| 2000 | p += strlen(p) + 1; |
| 2001 | *p = '\0'; |
| 2002 | |
| 2003 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2004 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2005 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2006 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2007 | &rcodes, shell); |
| 2008 | return rc; |
| 2009 | } |
| 2010 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2011 | static FILE * |
| 2012 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2013 | { |
| 2014 | HFILE rhan, whan; |
| 2015 | FILE *retfd = NULL; |
| 2016 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2017 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2018 | if (rc != NO_ERROR) { |
| 2019 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2020 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2021 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2022 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2023 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2024 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2025 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2026 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2027 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2028 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2029 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2030 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2031 | |
| 2032 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2033 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2036 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2037 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2038 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2039 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2040 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2041 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2042 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2043 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2044 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2045 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2046 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2047 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2048 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2049 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2050 | |
| 2051 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2052 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2055 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2056 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2057 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2058 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2059 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2060 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2061 | } else { |
| 2062 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2063 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2064 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | static PyObject * |
| 2068 | posix_popen(self, args) |
| 2069 | PyObject *self; |
| 2070 | PyObject *args; |
| 2071 | { |
| 2072 | char *name; |
| 2073 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2074 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2075 | FILE *fp; |
| 2076 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2077 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2078 | return NULL; |
| 2079 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2080 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2081 | Py_END_ALLOW_THREADS |
| 2082 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2083 | return os2_error(err); |
| 2084 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2085 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2086 | if (f != NULL) |
| 2087 | PyFile_SetBufSize(f, bufsize); |
| 2088 | return f; |
| 2089 | } |
| 2090 | |
| 2091 | #else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2092 | static PyObject * |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2093 | posix_popen(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2094 | PyObject *self; |
| 2095 | PyObject *args; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2096 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2097 | char *name; |
| 2098 | char *mode = "r"; |
| 2099 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2100 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2101 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2102 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2103 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2104 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2105 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2106 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2107 | if (fp == NULL) |
| 2108 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2109 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2110 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2111 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2112 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2113 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2114 | #endif |
| 2115 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2116 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2117 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2118 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2119 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2120 | static char posix_setuid__doc__[] = |
| 2121 | "setuid(uid) -> None\n\ |
| 2122 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2123 | static PyObject * |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2124 | posix_setuid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2125 | PyObject *self; |
| 2126 | PyObject *args; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2127 | { |
| 2128 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2129 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2130 | return NULL; |
| 2131 | if (setuid(uid) < 0) |
| 2132 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2133 | Py_INCREF(Py_None); |
| 2134 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2135 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2136 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2137 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2138 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2139 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2140 | static char posix_setgid__doc__[] = |
| 2141 | "setgid(gid) -> None\n\ |
| 2142 | Set the current process's group id."; |
| 2143 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2144 | static PyObject * |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2145 | posix_setgid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2146 | PyObject *self; |
| 2147 | PyObject *args; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2148 | { |
| 2149 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2150 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2151 | return NULL; |
| 2152 | if (setgid(gid) < 0) |
| 2153 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2154 | Py_INCREF(Py_None); |
| 2155 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2156 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2157 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2158 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2159 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2160 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2161 | static char posix_waitpid__doc__[] = |
| 2162 | "waitpid(pid, options) -> (pid, status)\n\ |
| 2163 | Wait for completion of a give child process."; |
| 2164 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2165 | static PyObject * |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2166 | posix_waitpid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2167 | PyObject *self; |
| 2168 | PyObject *args; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2169 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2170 | int pid, options; |
| 2171 | #ifdef UNION_WAIT |
| 2172 | union wait status; |
| 2173 | #define status_i (status.w_status) |
| 2174 | #else |
| 2175 | int status; |
| 2176 | #define status_i status |
| 2177 | #endif |
| 2178 | status_i = 0; |
| 2179 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2180 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2181 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2182 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 2183 | #ifdef NeXT |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2184 | pid = wait4(pid, &status, options, NULL); |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 2185 | #else |
| 2186 | pid = waitpid(pid, &status, options); |
| 2187 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2188 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2189 | if (pid == -1) |
| 2190 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2191 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2192 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2193 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2194 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2195 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2196 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2197 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2198 | static char posix_wait__doc__[] = |
| 2199 | "wait() -> (pid, status)\n\ |
| 2200 | Wait for completion of a child process."; |
| 2201 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2202 | static PyObject * |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2203 | posix_wait(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2204 | PyObject *self; |
| 2205 | PyObject *args; |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2206 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2207 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2208 | #ifdef UNION_WAIT |
| 2209 | union wait status; |
| 2210 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2211 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2212 | int status; |
| 2213 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2214 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2215 | if (!PyArg_ParseTuple(args, ":wait")) |
| 2216 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2217 | status_i = 0; |
| 2218 | Py_BEGIN_ALLOW_THREADS |
| 2219 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2220 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2221 | if (pid == -1) |
| 2222 | return posix_error(); |
| 2223 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2224 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2225 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2226 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2227 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2228 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2229 | |
| 2230 | static char posix_lstat__doc__[] = |
| 2231 | "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 2232 | Like stat(path), but do not follow symbolic links."; |
| 2233 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2234 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2235 | posix_lstat(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2236 | PyObject *self; |
| 2237 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2238 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2239 | #ifdef HAVE_LSTAT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2240 | return posix_do_stat(self, args, "s:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2241 | #else /* !HAVE_LSTAT */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2242 | return posix_do_stat(self, args, "s:lstat", stat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2243 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2246 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2247 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2248 | static char posix_readlink__doc__[] = |
| 2249 | "readlink(path) -> path\n\ |
| 2250 | Return a string representing the path to which the symbolic link points."; |
| 2251 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2252 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2253 | posix_readlink(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2254 | PyObject *self; |
| 2255 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2256 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2257 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2258 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2259 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2260 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2261 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2262 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 2263 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2264 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2265 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 2266 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2267 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2268 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2269 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2270 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2271 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2272 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2273 | static char posix_symlink__doc__[] = |
| 2274 | "symlink(src, dst) -> None\n\ |
| 2275 | Create a symbolic link."; |
| 2276 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 2277 | static PyObject * |
| 2278 | posix_symlink(self, args) |
| 2279 | PyObject *self; |
| 2280 | PyObject *args; |
| 2281 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2282 | return posix_2str(args, "ss:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 2283 | } |
| 2284 | #endif /* HAVE_SYMLINK */ |
| 2285 | |
| 2286 | |
| 2287 | #ifdef HAVE_TIMES |
| 2288 | #ifndef HZ |
| 2289 | #define HZ 60 /* Universal constant :-) */ |
| 2290 | #endif /* HZ */ |
| 2291 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2292 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 2293 | static long |
| 2294 | system_uptime() |
| 2295 | { |
| 2296 | ULONG value = 0; |
| 2297 | |
| 2298 | Py_BEGIN_ALLOW_THREADS |
| 2299 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 2300 | Py_END_ALLOW_THREADS |
| 2301 | |
| 2302 | return value; |
| 2303 | } |
| 2304 | |
| 2305 | static PyObject * |
| 2306 | posix_times(self, args) |
| 2307 | PyObject *self; |
| 2308 | PyObject *args; |
| 2309 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2310 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2311 | return NULL; |
| 2312 | |
| 2313 | /* Currently Only Uptime is Provided -- Others Later */ |
| 2314 | return Py_BuildValue("ddddd", |
| 2315 | (double)0 /* t.tms_utime / HZ */, |
| 2316 | (double)0 /* t.tms_stime / HZ */, |
| 2317 | (double)0 /* t.tms_cutime / HZ */, |
| 2318 | (double)0 /* t.tms_cstime / HZ */, |
| 2319 | (double)system_uptime() / 1000); |
| 2320 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 2321 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2322 | static PyObject * |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 2323 | posix_times(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2324 | PyObject *self; |
| 2325 | PyObject *args; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 2326 | { |
| 2327 | struct tms t; |
| 2328 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2329 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 2330 | return NULL; |
| 2331 | errno = 0; |
| 2332 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2333 | if (c == (clock_t) -1) |
| 2334 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2335 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2336 | (double)t.tms_utime / HZ, |
| 2337 | (double)t.tms_stime / HZ, |
| 2338 | (double)t.tms_cutime / HZ, |
| 2339 | (double)t.tms_cstime / HZ, |
| 2340 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 2341 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 2342 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2343 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 2344 | |
| 2345 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 2346 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 2347 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2348 | static PyObject * |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 2349 | posix_times(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2350 | PyObject *self; |
| 2351 | PyObject *args; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 2352 | { |
| 2353 | FILETIME create, exit, kernel, user; |
| 2354 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2355 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 2356 | return NULL; |
| 2357 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 2358 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 2359 | /* The fields of a FILETIME structure are the hi and lo part |
| 2360 | of a 64-bit value expressed in 100 nanosecond units. |
| 2361 | 1e7 is one second in such units; 1e-7 the inverse. |
| 2362 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 2363 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2364 | return Py_BuildValue( |
| 2365 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 2366 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 2367 | kernel.dwLowDateTime*1e-7), |
| 2368 | (double)(user.dwHighDateTime*429.4967296 + |
| 2369 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2370 | (double)0, |
| 2371 | (double)0, |
| 2372 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 2373 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2374 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 2375 | |
| 2376 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 2377 | static char posix_times__doc__[] = |
| 2378 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 2379 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 2380 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 2381 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2382 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2383 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2384 | static char posix_setsid__doc__[] = |
| 2385 | "setsid() -> None\n\ |
| 2386 | Call the system call setsid()."; |
| 2387 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2388 | static PyObject * |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2389 | posix_setsid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2390 | PyObject *self; |
| 2391 | PyObject *args; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2392 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2393 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2394 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2395 | if (setsid() < 0) |
| 2396 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2397 | Py_INCREF(Py_None); |
| 2398 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2399 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2400 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2401 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2402 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2403 | static char posix_setpgid__doc__[] = |
| 2404 | "setpgid(pid, pgrp) -> None\n\ |
| 2405 | Call the system call setpgid()."; |
| 2406 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2407 | static PyObject * |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2408 | posix_setpgid(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2409 | PyObject *self; |
| 2410 | PyObject *args; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2411 | { |
| 2412 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2413 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2414 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2415 | if (setpgid(pid, pgrp) < 0) |
| 2416 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2417 | Py_INCREF(Py_None); |
| 2418 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2419 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2420 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2421 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2422 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2423 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2424 | static char posix_tcgetpgrp__doc__[] = |
| 2425 | "tcgetpgrp(fd) -> pgid\n\ |
| 2426 | Return the process group associated with the terminal given by a fd."; |
| 2427 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2428 | static PyObject * |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 2429 | posix_tcgetpgrp(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2430 | PyObject *self; |
| 2431 | PyObject *args; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 2432 | { |
| 2433 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2434 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 2435 | return NULL; |
| 2436 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2437 | if (pgid < 0) |
| 2438 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2439 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 2440 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2441 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 2442 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2443 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2444 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2445 | static char posix_tcsetpgrp__doc__[] = |
| 2446 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 2447 | Set the process group associated with the terminal given by a fd."; |
| 2448 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2449 | static PyObject * |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 2450 | posix_tcsetpgrp(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2451 | PyObject *self; |
| 2452 | PyObject *args; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 2453 | { |
| 2454 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2455 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 2456 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2457 | if (tcsetpgrp(fd, pgid) < 0) |
| 2458 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2459 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2460 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 2461 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2462 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 2463 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2464 | /* Functions acting on file descriptors */ |
| 2465 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2466 | static char posix_open__doc__[] = |
| 2467 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 2468 | Open a file (for low level IO)."; |
| 2469 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2470 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2471 | posix_open(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2472 | PyObject *self; |
| 2473 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2474 | { |
| 2475 | char *file; |
| 2476 | int flag; |
| 2477 | int mode = 0777; |
| 2478 | int fd; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2479 | if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode)) |
| 2480 | return NULL; |
| 2481 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2482 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2483 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2484 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2485 | if (fd < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 2486 | return posix_error_with_filename(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2487 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2490 | |
| 2491 | static char posix_close__doc__[] = |
| 2492 | "close(fd) -> None\n\ |
| 2493 | Close a file descriptor (for low level IO)."; |
| 2494 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2495 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2496 | posix_close(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2497 | PyObject *self; |
| 2498 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2499 | { |
| 2500 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2501 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2502 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2503 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2504 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2505 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2506 | if (res < 0) |
| 2507 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2508 | Py_INCREF(Py_None); |
| 2509 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2512 | |
| 2513 | static char posix_dup__doc__[] = |
| 2514 | "dup(fd) -> fd2\n\ |
| 2515 | Return a duplicate of a file descriptor."; |
| 2516 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2517 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2518 | posix_dup(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2519 | PyObject *self; |
| 2520 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2521 | { |
| 2522 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2523 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2524 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2525 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2526 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2527 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2528 | if (fd < 0) |
| 2529 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2530 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2533 | |
| 2534 | static char posix_dup2__doc__[] = |
| 2535 | "dup2(fd, fd2) -> None\n\ |
| 2536 | Duplicate file descriptor."; |
| 2537 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2538 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2539 | posix_dup2(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2540 | PyObject *self; |
| 2541 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2542 | { |
| 2543 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2544 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2545 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2546 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2547 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2548 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2549 | if (res < 0) |
| 2550 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2551 | Py_INCREF(Py_None); |
| 2552 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2555 | |
| 2556 | static char posix_lseek__doc__[] = |
| 2557 | "lseek(fd, pos, how) -> newpos\n\ |
| 2558 | Set the current position of a file descriptor."; |
| 2559 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2560 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2561 | posix_lseek(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2562 | PyObject *self; |
| 2563 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2564 | { |
| 2565 | int fd, how; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2566 | off_t pos, res; |
| 2567 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2568 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2569 | return NULL; |
| 2570 | #ifdef SEEK_SET |
| 2571 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 2572 | switch (how) { |
| 2573 | case 0: how = SEEK_SET; break; |
| 2574 | case 1: how = SEEK_CUR; break; |
| 2575 | case 2: how = SEEK_END; break; |
| 2576 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2577 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2578 | |
| 2579 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 2580 | pos = PyInt_AsLong(posobj); |
| 2581 | #else |
| 2582 | pos = PyLong_Check(posobj) ? |
| 2583 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 2584 | #endif |
| 2585 | if (PyErr_Occurred()) |
| 2586 | return NULL; |
| 2587 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2588 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2589 | res = lseek(fd, pos, how); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2590 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2591 | if (res < 0) |
| 2592 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2593 | |
| 2594 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2595 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2596 | #else |
| 2597 | return PyLong_FromLongLong(res); |
| 2598 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2601 | |
| 2602 | static char posix_read__doc__[] = |
| 2603 | "read(fd, buffersize) -> string\n\ |
| 2604 | Read a file descriptor."; |
| 2605 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2606 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2607 | posix_read(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2608 | PyObject *self; |
| 2609 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2610 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 2611 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2612 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2613 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2614 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2615 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2616 | if (buffer == NULL) |
| 2617 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2618 | Py_BEGIN_ALLOW_THREADS |
| 2619 | n = read(fd, PyString_AsString(buffer), size); |
| 2620 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 2621 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2622 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2623 | return posix_error(); |
| 2624 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 2625 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2626 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2627 | return buffer; |
| 2628 | } |
| 2629 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2630 | |
| 2631 | static char posix_write__doc__[] = |
| 2632 | "write(fd, string) -> byteswritten\n\ |
| 2633 | Write a string to a file descriptor."; |
| 2634 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2635 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2636 | posix_write(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2637 | PyObject *self; |
| 2638 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2639 | { |
| 2640 | int fd, size; |
| 2641 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2642 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2643 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2644 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2645 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2646 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2647 | if (size < 0) |
| 2648 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2649 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2652 | |
| 2653 | static char posix_fstat__doc__[]= |
| 2654 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 2655 | Like stat(), but for an open file descriptor."; |
| 2656 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2657 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2658 | posix_fstat(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2659 | PyObject *self; |
| 2660 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2661 | { |
| 2662 | int fd; |
| 2663 | struct stat st; |
| 2664 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2665 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2666 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2667 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2668 | res = fstat(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2669 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2670 | if (res != 0) |
| 2671 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2672 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2673 | return Py_BuildValue("(llllllllll)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2674 | (long)st.st_mode, |
| 2675 | (long)st.st_ino, |
| 2676 | (long)st.st_dev, |
| 2677 | (long)st.st_nlink, |
| 2678 | (long)st.st_uid, |
| 2679 | (long)st.st_gid, |
| 2680 | (long)st.st_size, |
| 2681 | (long)st.st_atime, |
| 2682 | (long)st.st_mtime, |
| 2683 | (long)st.st_ctime); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2684 | #else |
| 2685 | return Py_BuildValue("(lLllllLlll)", |
| 2686 | (long)st.st_mode, |
| 2687 | (LONG_LONG)st.st_ino, |
| 2688 | (long)st.st_dev, |
| 2689 | (long)st.st_nlink, |
| 2690 | (long)st.st_uid, |
| 2691 | (long)st.st_gid, |
| 2692 | (LONG_LONG)st.st_size, |
| 2693 | (long)st.st_atime, |
| 2694 | (long)st.st_mtime, |
| 2695 | (long)st.st_ctime); |
| 2696 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2699 | |
| 2700 | static char posix_fdopen__doc__[] = |
| 2701 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 2702 | Return an open file object connected to a file descriptor."; |
| 2703 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2704 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2705 | posix_fdopen(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2706 | PyObject *self; |
| 2707 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2708 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2709 | extern int fclose Py_PROTO((FILE *)); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2710 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2711 | char *mode = "r"; |
| 2712 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2713 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2714 | PyObject *f; |
| 2715 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2716 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2717 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2718 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2719 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2720 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2721 | if (fp == NULL) |
| 2722 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2723 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2724 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2725 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2726 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2729 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2730 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2731 | static char posix_pipe__doc__[] = |
| 2732 | "pipe() -> (read_end, write_end)\n\ |
| 2733 | Create a pipe."; |
| 2734 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2735 | static PyObject * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2736 | posix_pipe(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2737 | PyObject *self; |
| 2738 | PyObject *args; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2739 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2740 | #if defined(PYOS_OS2) |
| 2741 | HFILE read, write; |
| 2742 | APIRET rc; |
| 2743 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2744 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2745 | return NULL; |
| 2746 | |
| 2747 | Py_BEGIN_ALLOW_THREADS |
| 2748 | rc = DosCreatePipe( &read, &write, 4096); |
| 2749 | Py_END_ALLOW_THREADS |
| 2750 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2751 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2752 | |
| 2753 | return Py_BuildValue("(ii)", read, write); |
| 2754 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2755 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2756 | int fds[2]; |
| 2757 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2758 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2759 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2760 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2761 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2762 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2763 | if (res != 0) |
| 2764 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2765 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2766 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 2767 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 2768 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 2769 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2770 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 2771 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2772 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 2773 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2774 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 2775 | if (!ok) |
| 2776 | return posix_error(); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 2777 | read_fd = _open_osfhandle((long)read, 0); |
| 2778 | write_fd = _open_osfhandle((long)write, 1); |
| 2779 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2780 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2781 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2782 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2783 | #endif /* HAVE_PIPE */ |
| 2784 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2785 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2786 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2787 | static char posix_mkfifo__doc__[] = |
| 2788 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 2789 | Create a FIFO (a POSIX named pipe)."; |
| 2790 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2791 | static PyObject * |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2792 | posix_mkfifo(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2793 | PyObject *self; |
| 2794 | PyObject *args; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2795 | { |
| 2796 | char *file; |
| 2797 | int mode = 0666; |
| 2798 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2799 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2800 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2801 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2802 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2803 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2804 | if (res < 0) |
| 2805 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2806 | Py_INCREF(Py_None); |
| 2807 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2808 | } |
| 2809 | #endif |
| 2810 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2811 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2812 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2813 | static char posix_ftruncate__doc__[] = |
| 2814 | "ftruncate(fd, length) -> None\n\ |
| 2815 | Truncate a file to a specified length."; |
| 2816 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2817 | static PyObject * |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2818 | posix_ftruncate(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2819 | PyObject *self; /* Not used */ |
| 2820 | PyObject *args; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2821 | { |
| 2822 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2823 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2824 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2825 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2826 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2827 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2828 | return NULL; |
| 2829 | |
| 2830 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 2831 | length = PyInt_AsLong(lenobj); |
| 2832 | #else |
| 2833 | length = PyLong_Check(lenobj) ? |
| 2834 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 2835 | #endif |
| 2836 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2837 | return NULL; |
| 2838 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2839 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2840 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2841 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2842 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2843 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2844 | return NULL; |
| 2845 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2846 | Py_INCREF(Py_None); |
| 2847 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2848 | } |
| 2849 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 2850 | |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2851 | #ifdef NeXT |
| 2852 | #define HAVE_PUTENV |
| 2853 | /* Steve Spicklemire got this putenv from NeXTAnswers */ |
| 2854 | static int |
| 2855 | putenv(char *newval) |
| 2856 | { |
| 2857 | extern char **environ; |
| 2858 | |
| 2859 | static int firstTime = 1; |
| 2860 | char **ep; |
| 2861 | char *cp; |
| 2862 | int esiz; |
| 2863 | char *np; |
| 2864 | |
| 2865 | if (!(np = strchr(newval, '='))) |
| 2866 | return 1; |
| 2867 | *np = '\0'; |
| 2868 | |
| 2869 | /* look it up */ |
| 2870 | for (ep=environ ; *ep ; ep++) |
| 2871 | { |
| 2872 | /* this should always be true... */ |
| 2873 | if (cp = strchr(*ep, '=')) |
| 2874 | { |
| 2875 | *cp = '\0'; |
| 2876 | if (!strcmp(*ep, newval)) |
| 2877 | { |
| 2878 | /* got it! */ |
| 2879 | *cp = '='; |
| 2880 | break; |
| 2881 | } |
| 2882 | *cp = '='; |
| 2883 | } |
| 2884 | else |
| 2885 | { |
| 2886 | *np = '='; |
| 2887 | return 1; |
| 2888 | } |
| 2889 | } |
| 2890 | |
| 2891 | *np = '='; |
| 2892 | if (*ep) |
| 2893 | { |
| 2894 | /* the string was already there: |
| 2895 | just replace it with the new one */ |
| 2896 | *ep = newval; |
| 2897 | return 0; |
| 2898 | } |
| 2899 | |
| 2900 | /* expand environ by one */ |
| 2901 | for (esiz=2, ep=environ ; *ep ; ep++) |
| 2902 | esiz++; |
| 2903 | if (firstTime) |
| 2904 | { |
| 2905 | char **epp; |
| 2906 | char **newenv; |
| 2907 | if (!(newenv = malloc(esiz * sizeof(char *)))) |
| 2908 | return 1; |
| 2909 | |
| 2910 | for (ep=environ, epp=newenv ; *ep ;) |
| 2911 | *epp++ = *ep++; |
| 2912 | *epp++ = newval; |
| 2913 | *epp = (char *) 0; |
| 2914 | environ = newenv; |
| 2915 | } |
| 2916 | else |
| 2917 | { |
| 2918 | if (!(environ = realloc(environ, esiz * sizeof(char *)))) |
| 2919 | return 1; |
| 2920 | environ[esiz - 2] = newval; |
| 2921 | environ[esiz - 1] = (char *) 0; |
| 2922 | firstTime = 0; |
| 2923 | } |
| 2924 | |
| 2925 | return 0; |
| 2926 | } |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 2927 | #endif /* NeXT */ |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2928 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2929 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2930 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2931 | static char posix_putenv__doc__[] = |
| 2932 | "putenv(key, value) -> None\n\ |
| 2933 | Change or add an environment variable."; |
| 2934 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2935 | #ifdef __BEOS__ |
| 2936 | /* We have putenv(), but not in the headers (as of PR2). - [cjh] */ |
| 2937 | int putenv( const char *str ); |
| 2938 | #endif |
| 2939 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 2940 | /* Save putenv() parameters as values here, so we can collect them when they |
| 2941 | * get re-set with another call for the same key. */ |
| 2942 | static PyObject *posix_putenv_garbage; |
| 2943 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2944 | static PyObject * |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 2945 | posix_putenv(self, args) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2946 | PyObject *self; |
| 2947 | PyObject *args; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2948 | { |
| 2949 | char *s1, *s2; |
| 2950 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 2951 | PyObject *newstr; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2952 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2953 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2954 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2955 | |
| 2956 | #if defined(PYOS_OS2) |
| 2957 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 2958 | APIRET rc; |
| 2959 | |
| 2960 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 2961 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 2962 | |
| 2963 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 2964 | if (rc != NO_ERROR) |
| 2965 | return os2_error(rc); |
| 2966 | |
| 2967 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 2968 | APIRET rc; |
| 2969 | |
| 2970 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 2971 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 2972 | |
| 2973 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 2974 | if (rc != NO_ERROR) |
| 2975 | return os2_error(rc); |
| 2976 | } else { |
| 2977 | #endif |
| 2978 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 2979 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 2980 | newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2); |
| 2981 | if (newstr == NULL) |
| 2982 | return PyErr_NoMemory(); |
| 2983 | new = PyString_AS_STRING(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 2984 | (void) sprintf(new, "%s=%s", s1, s2); |
| 2985 | if (putenv(new)) { |
| 2986 | posix_error(); |
| 2987 | return NULL; |
| 2988 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 2989 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 2990 | * this will cause previous value to be collected. This has to |
| 2991 | * happen after the real putenv() call because the old value |
| 2992 | * was still accessible until then. */ |
| 2993 | if (PyDict_SetItem(posix_putenv_garbage, |
| 2994 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 2995 | /* really not much we can do; just leak */ |
| 2996 | PyErr_Clear(); |
| 2997 | } |
| 2998 | else { |
| 2999 | Py_DECREF(newstr); |
| 3000 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3001 | |
| 3002 | #if defined(PYOS_OS2) |
| 3003 | } |
| 3004 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3005 | Py_INCREF(Py_None); |
| 3006 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3007 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3008 | #endif /* putenv */ |
| 3009 | |
| 3010 | #ifdef HAVE_STRERROR |
| 3011 | static char posix_strerror__doc__[] = |
| 3012 | "strerror(code) -> string\n\ |
| 3013 | Translate an error code to a message string."; |
| 3014 | |
| 3015 | PyObject * |
| 3016 | posix_strerror(self, args) |
| 3017 | PyObject *self; |
| 3018 | PyObject *args; |
| 3019 | { |
| 3020 | int code; |
| 3021 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3022 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3023 | return NULL; |
| 3024 | message = strerror(code); |
| 3025 | if (message == NULL) { |
| 3026 | PyErr_SetString(PyExc_ValueError, |
| 3027 | "strerror code out of range"); |
| 3028 | return NULL; |
| 3029 | } |
| 3030 | return PyString_FromString(message); |
| 3031 | } |
| 3032 | #endif /* strerror */ |
| 3033 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3034 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3035 | #ifdef HAVE_SYS_WAIT_H |
| 3036 | |
| 3037 | #ifdef WIFSTOPPED |
| 3038 | static char posix_WIFSTOPPED__doc__[] = |
| 3039 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3040 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3041 | |
| 3042 | static PyObject * |
| 3043 | posix_WIFSTOPPED(self, args) |
| 3044 | PyObject *self; |
| 3045 | PyObject *args; |
| 3046 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3047 | #ifdef UNION_WAIT |
| 3048 | union wait status; |
| 3049 | #define status_i (status.w_status) |
| 3050 | #else |
| 3051 | int status; |
| 3052 | #define status_i status |
| 3053 | #endif |
| 3054 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3055 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3056 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3057 | { |
| 3058 | return NULL; |
| 3059 | } |
| 3060 | |
| 3061 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3062 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3063 | } |
| 3064 | #endif /* WIFSTOPPED */ |
| 3065 | |
| 3066 | #ifdef WIFSIGNALED |
| 3067 | static char posix_WIFSIGNALED__doc__[] = |
| 3068 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 3069 | Return true if the process returning 'status' was terminated by a signal."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3070 | |
| 3071 | static PyObject * |
| 3072 | posix_WIFSIGNALED(self, args) |
| 3073 | PyObject *self; |
| 3074 | PyObject *args; |
| 3075 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3076 | #ifdef UNION_WAIT |
| 3077 | union wait status; |
| 3078 | #define status_i (status.w_status) |
| 3079 | #else |
| 3080 | int status; |
| 3081 | #define status_i status |
| 3082 | #endif |
| 3083 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3084 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3085 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3086 | { |
| 3087 | return NULL; |
| 3088 | } |
| 3089 | |
| 3090 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3091 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3092 | } |
| 3093 | #endif /* WIFSIGNALED */ |
| 3094 | |
| 3095 | #ifdef WIFEXITED |
| 3096 | static char posix_WIFEXITED__doc__[] = |
| 3097 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3098 | Return true if the process returning 'status' exited using the exit()\n\ |
| 3099 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3100 | |
| 3101 | static PyObject * |
| 3102 | posix_WIFEXITED(self, args) |
| 3103 | PyObject *self; |
| 3104 | PyObject *args; |
| 3105 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3106 | #ifdef UNION_WAIT |
| 3107 | union wait status; |
| 3108 | #define status_i (status.w_status) |
| 3109 | #else |
| 3110 | int status; |
| 3111 | #define status_i status |
| 3112 | #endif |
| 3113 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3114 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3115 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3116 | { |
| 3117 | return NULL; |
| 3118 | } |
| 3119 | |
| 3120 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3121 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3122 | } |
| 3123 | #endif /* WIFEXITED */ |
| 3124 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3125 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3126 | static char posix_WEXITSTATUS__doc__[] = |
| 3127 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3128 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3129 | |
| 3130 | static PyObject * |
| 3131 | posix_WEXITSTATUS(self, args) |
| 3132 | PyObject *self; |
| 3133 | PyObject *args; |
| 3134 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3135 | #ifdef UNION_WAIT |
| 3136 | union wait status; |
| 3137 | #define status_i (status.w_status) |
| 3138 | #else |
| 3139 | int status; |
| 3140 | #define status_i status |
| 3141 | #endif |
| 3142 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3143 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3144 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3145 | { |
| 3146 | return NULL; |
| 3147 | } |
| 3148 | |
| 3149 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3150 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3151 | } |
| 3152 | #endif /* WEXITSTATUS */ |
| 3153 | |
| 3154 | #ifdef WTERMSIG |
| 3155 | static char posix_WTERMSIG__doc__[] = |
| 3156 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3157 | Return the signal that terminated the process that provided the 'status'\n\ |
| 3158 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3159 | |
| 3160 | static PyObject * |
| 3161 | posix_WTERMSIG(self, args) |
| 3162 | PyObject *self; |
| 3163 | PyObject *args; |
| 3164 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3165 | #ifdef UNION_WAIT |
| 3166 | union wait status; |
| 3167 | #define status_i (status.w_status) |
| 3168 | #else |
| 3169 | int status; |
| 3170 | #define status_i status |
| 3171 | #endif |
| 3172 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3173 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3174 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3175 | { |
| 3176 | return NULL; |
| 3177 | } |
| 3178 | |
| 3179 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3180 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3181 | } |
| 3182 | #endif /* WTERMSIG */ |
| 3183 | |
| 3184 | #ifdef WSTOPSIG |
| 3185 | static char posix_WSTOPSIG__doc__[] = |
| 3186 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3187 | Return the signal that stopped the process that provided the 'status' value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3188 | |
| 3189 | static PyObject * |
| 3190 | posix_WSTOPSIG(self, args) |
| 3191 | PyObject *self; |
| 3192 | PyObject *args; |
| 3193 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3194 | #ifdef UNION_WAIT |
| 3195 | union wait status; |
| 3196 | #define status_i (status.w_status) |
| 3197 | #else |
| 3198 | int status; |
| 3199 | #define status_i status |
| 3200 | #endif |
| 3201 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3202 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3203 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3204 | { |
| 3205 | return NULL; |
| 3206 | } |
| 3207 | |
| 3208 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3209 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3210 | } |
| 3211 | #endif /* WSTOPSIG */ |
| 3212 | |
| 3213 | #endif /* HAVE_SYS_WAIT_H */ |
| 3214 | |
| 3215 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3216 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 3217 | #ifdef _SCO_DS |
| 3218 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 3219 | needed definitions in sys/statvfs.h */ |
| 3220 | #define _SVID3 |
| 3221 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3222 | #include <sys/statvfs.h> |
| 3223 | |
| 3224 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3225 | "fstatvfs(fd) -> \n\ |
| 3226 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3227 | Perform an fstatvfs system call on the given fd."; |
| 3228 | |
| 3229 | static PyObject * |
| 3230 | posix_fstatvfs(self, args) |
| 3231 | PyObject *self; |
| 3232 | PyObject *args; |
| 3233 | { |
| 3234 | int fd, res; |
| 3235 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3236 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3237 | return NULL; |
| 3238 | Py_BEGIN_ALLOW_THREADS |
| 3239 | res = fstatvfs(fd, &st); |
| 3240 | Py_END_ALLOW_THREADS |
| 3241 | if (res != 0) |
| 3242 | return posix_error(); |
| 3243 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3244 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3245 | (long) st.f_bsize, |
| 3246 | (long) st.f_frsize, |
| 3247 | (long) st.f_blocks, |
| 3248 | (long) st.f_bfree, |
| 3249 | (long) st.f_bavail, |
| 3250 | (long) st.f_files, |
| 3251 | (long) st.f_ffree, |
| 3252 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3253 | (long) st.f_flag, |
| 3254 | (long) st.f_namemax); |
| 3255 | #else |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3256 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3257 | (long) st.f_bsize, |
| 3258 | (long) st.f_frsize, |
| 3259 | (LONG_LONG) st.f_blocks, |
| 3260 | (LONG_LONG) st.f_bfree, |
| 3261 | (LONG_LONG) st.f_bavail, |
| 3262 | (LONG_LONG) st.f_files, |
| 3263 | (LONG_LONG) st.f_ffree, |
| 3264 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3265 | (long) st.f_flag, |
| 3266 | (long) st.f_namemax); |
| 3267 | #endif |
| 3268 | } |
| 3269 | #endif /* HAVE_FSTATVFS */ |
| 3270 | |
| 3271 | |
| 3272 | #if defined(HAVE_STATVFS) |
| 3273 | #include <sys/statvfs.h> |
| 3274 | |
| 3275 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3276 | "statvfs(path) -> \n\ |
| 3277 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3278 | Perform a statvfs system call on the given path."; |
| 3279 | |
| 3280 | static PyObject * |
| 3281 | posix_statvfs(self, args) |
| 3282 | PyObject *self; |
| 3283 | PyObject *args; |
| 3284 | { |
| 3285 | char *path; |
| 3286 | int res; |
| 3287 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3288 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3289 | return NULL; |
| 3290 | Py_BEGIN_ALLOW_THREADS |
| 3291 | res = statvfs(path, &st); |
| 3292 | Py_END_ALLOW_THREADS |
| 3293 | if (res != 0) |
| 3294 | return posix_error_with_filename(path); |
| 3295 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3296 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3297 | (long) st.f_bsize, |
| 3298 | (long) st.f_frsize, |
| 3299 | (long) st.f_blocks, |
| 3300 | (long) st.f_bfree, |
| 3301 | (long) st.f_bavail, |
| 3302 | (long) st.f_files, |
| 3303 | (long) st.f_ffree, |
| 3304 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3305 | (long) st.f_flag, |
| 3306 | (long) st.f_namemax); |
| 3307 | #else /* HAVE_LARGEFILE_SUPPORT */ |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3308 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3309 | (long) st.f_bsize, |
| 3310 | (long) st.f_frsize, |
| 3311 | (LONG_LONG) st.f_blocks, |
| 3312 | (LONG_LONG) st.f_bfree, |
| 3313 | (LONG_LONG) st.f_bavail, |
| 3314 | (LONG_LONG) st.f_files, |
| 3315 | (LONG_LONG) st.f_ffree, |
| 3316 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3317 | (long) st.f_flag, |
| 3318 | (long) st.f_namemax); |
| 3319 | #endif |
| 3320 | } |
| 3321 | #endif /* HAVE_STATVFS */ |
| 3322 | |
| 3323 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3324 | #ifdef HAVE_TEMPNAM |
| 3325 | static char posix_tempnam__doc__[] = "\ |
| 3326 | tempnam([dir[, prefix]]) -> string\n\ |
| 3327 | Return a unique name for a temporary file.\n\ |
| 3328 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 3329 | or None if not needed."; |
| 3330 | |
| 3331 | static PyObject * |
| 3332 | posix_tempnam(self, args) |
| 3333 | PyObject *self; |
| 3334 | PyObject *args; |
| 3335 | { |
| 3336 | PyObject *result = NULL; |
| 3337 | char *dir = NULL; |
| 3338 | char *pfx = NULL; |
| 3339 | char *name; |
| 3340 | |
| 3341 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 3342 | return NULL; |
| 3343 | name = tempnam(dir, pfx); |
| 3344 | if (name == NULL) |
| 3345 | return PyErr_NoMemory(); |
| 3346 | result = PyString_FromString(name); |
| 3347 | free(name); |
| 3348 | return result; |
| 3349 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 3350 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3351 | |
| 3352 | |
| 3353 | #ifdef HAVE_TMPFILE |
| 3354 | static char posix_tmpfile__doc__[] = "\ |
| 3355 | tmpfile() -> file object\n\ |
| 3356 | Create a temporary file with no directory entries."; |
| 3357 | |
| 3358 | static PyObject * |
| 3359 | posix_tmpfile(self, args) |
| 3360 | PyObject *self; |
| 3361 | PyObject *args; |
| 3362 | { |
| 3363 | FILE *fp; |
| 3364 | |
| 3365 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 3366 | return NULL; |
| 3367 | fp = tmpfile(); |
| 3368 | if (fp == NULL) |
| 3369 | return posix_error(); |
| 3370 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 3371 | } |
| 3372 | #endif |
| 3373 | |
| 3374 | |
| 3375 | #ifdef HAVE_TMPNAM |
| 3376 | static char posix_tmpnam__doc__[] = "\ |
| 3377 | tmpnam() -> string\n\ |
| 3378 | Return a unique name for a temporary file."; |
| 3379 | |
| 3380 | static PyObject * |
| 3381 | posix_tmpnam(self, args) |
| 3382 | PyObject *self; |
| 3383 | PyObject *args; |
| 3384 | { |
| 3385 | char buffer[L_tmpnam]; |
| 3386 | char *name; |
| 3387 | |
| 3388 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 3389 | return NULL; |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 3390 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3391 | name = tmpnam_r(buffer); |
| 3392 | #else |
| 3393 | name = tmpnam(buffer); |
| 3394 | #endif |
| 3395 | if (name == NULL) { |
| 3396 | PyErr_SetObject(PyExc_OSError, |
| 3397 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 3398 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3399 | "unexpected NULL from tmpnam_r" |
| 3400 | #else |
| 3401 | "unexpected NULL from tmpnam" |
| 3402 | #endif |
| 3403 | )); |
| 3404 | return NULL; |
| 3405 | } |
| 3406 | return PyString_FromString(buffer); |
| 3407 | } |
| 3408 | #endif |
| 3409 | |
| 3410 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3411 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 3412 | * It maps strings representing configuration variable names to |
| 3413 | * integer values, allowing those functions to be called with the |
| 3414 | * magic names instead of poluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3415 | * rarely-used constants. There are three separate tables that use |
| 3416 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 3417 | * |
| 3418 | * This code is always included, even if none of the interfaces that |
| 3419 | * need it are included. The #if hackery needed to avoid it would be |
| 3420 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3421 | */ |
| 3422 | struct constdef { |
| 3423 | char *name; |
| 3424 | long value; |
| 3425 | }; |
| 3426 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3427 | static int |
| 3428 | conv_confname(arg, valuep, table, tablesize) |
| 3429 | PyObject *arg; |
| 3430 | int *valuep; |
| 3431 | struct constdef *table; |
| 3432 | size_t tablesize; |
| 3433 | { |
| 3434 | if (PyInt_Check(arg)) { |
| 3435 | *valuep = PyInt_AS_LONG(arg); |
| 3436 | return 1; |
| 3437 | } |
| 3438 | if (PyString_Check(arg)) { |
| 3439 | /* look up the value in the table using a binary search */ |
| 3440 | int lo = 0; |
| 3441 | int hi = tablesize; |
| 3442 | int cmp, mid; |
| 3443 | char *confname = PyString_AS_STRING(arg); |
| 3444 | while (lo < hi) { |
| 3445 | mid = (lo + hi) / 2; |
| 3446 | cmp = strcmp(confname, table[mid].name); |
| 3447 | if (cmp < 0) |
| 3448 | hi = mid; |
| 3449 | else if (cmp > 0) |
| 3450 | lo = mid + 1; |
| 3451 | else { |
| 3452 | *valuep = table[mid].value; |
| 3453 | return 1; |
| 3454 | } |
| 3455 | } |
| 3456 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 3457 | } |
| 3458 | else |
| 3459 | PyErr_SetString(PyExc_TypeError, |
| 3460 | "configuration names must be strings or integers"); |
| 3461 | return 0; |
| 3462 | } |
| 3463 | |
| 3464 | |
| 3465 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 3466 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3467 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 3468 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 3469 | #endif |
| 3470 | #ifdef _PC_ABI_ASYNC_IO |
| 3471 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 3472 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3473 | #ifdef _PC_ASYNC_IO |
| 3474 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 3475 | #endif |
| 3476 | #ifdef _PC_CHOWN_RESTRICTED |
| 3477 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 3478 | #endif |
| 3479 | #ifdef _PC_FILESIZEBITS |
| 3480 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 3481 | #endif |
| 3482 | #ifdef _PC_LAST |
| 3483 | {"PC_LAST", _PC_LAST}, |
| 3484 | #endif |
| 3485 | #ifdef _PC_LINK_MAX |
| 3486 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 3487 | #endif |
| 3488 | #ifdef _PC_MAX_CANON |
| 3489 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 3490 | #endif |
| 3491 | #ifdef _PC_MAX_INPUT |
| 3492 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 3493 | #endif |
| 3494 | #ifdef _PC_NAME_MAX |
| 3495 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 3496 | #endif |
| 3497 | #ifdef _PC_NO_TRUNC |
| 3498 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 3499 | #endif |
| 3500 | #ifdef _PC_PATH_MAX |
| 3501 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 3502 | #endif |
| 3503 | #ifdef _PC_PIPE_BUF |
| 3504 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 3505 | #endif |
| 3506 | #ifdef _PC_PRIO_IO |
| 3507 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 3508 | #endif |
| 3509 | #ifdef _PC_SOCK_MAXBUF |
| 3510 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 3511 | #endif |
| 3512 | #ifdef _PC_SYNC_IO |
| 3513 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 3514 | #endif |
| 3515 | #ifdef _PC_VDISABLE |
| 3516 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 3517 | #endif |
| 3518 | }; |
| 3519 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3520 | static int |
| 3521 | conv_path_confname(arg, valuep) |
| 3522 | PyObject *arg; |
| 3523 | int *valuep; |
| 3524 | { |
| 3525 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 3526 | sizeof(posix_constants_pathconf) |
| 3527 | / sizeof(struct constdef)); |
| 3528 | } |
| 3529 | #endif |
| 3530 | |
| 3531 | #ifdef HAVE_FPATHCONF |
| 3532 | static char posix_fpathconf__doc__[] = "\ |
| 3533 | fpathconf(fd, name) -> integer\n\ |
| 3534 | Return the configuration limit name for the file descriptor fd.\n\ |
| 3535 | If there is no limit, return -1."; |
| 3536 | |
| 3537 | static PyObject * |
| 3538 | posix_fpathconf(self, args) |
| 3539 | PyObject *self; |
| 3540 | PyObject *args; |
| 3541 | { |
| 3542 | PyObject *result = NULL; |
| 3543 | int name, fd; |
| 3544 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3545 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 3546 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3547 | long limit; |
| 3548 | |
| 3549 | errno = 0; |
| 3550 | limit = fpathconf(fd, name); |
| 3551 | if (limit == -1 && errno != 0) |
| 3552 | posix_error(); |
| 3553 | else |
| 3554 | result = PyInt_FromLong(limit); |
| 3555 | } |
| 3556 | return result; |
| 3557 | } |
| 3558 | #endif |
| 3559 | |
| 3560 | |
| 3561 | #ifdef HAVE_PATHCONF |
| 3562 | static char posix_pathconf__doc__[] = "\ |
| 3563 | pathconf(path, name) -> integer\n\ |
| 3564 | Return the configuration limit name for the file or directory path.\n\ |
| 3565 | If there is no limit, return -1."; |
| 3566 | |
| 3567 | static PyObject * |
| 3568 | posix_pathconf(self, args) |
| 3569 | PyObject *self; |
| 3570 | PyObject *args; |
| 3571 | { |
| 3572 | PyObject *result = NULL; |
| 3573 | int name; |
| 3574 | char *path; |
| 3575 | |
| 3576 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 3577 | conv_path_confname, &name)) { |
| 3578 | long limit; |
| 3579 | |
| 3580 | errno = 0; |
| 3581 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3582 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3583 | if (errno == EINVAL) |
| 3584 | /* could be a path or name problem */ |
| 3585 | posix_error(); |
| 3586 | else |
| 3587 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3588 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3589 | else |
| 3590 | result = PyInt_FromLong(limit); |
| 3591 | } |
| 3592 | return result; |
| 3593 | } |
| 3594 | #endif |
| 3595 | |
| 3596 | #ifdef HAVE_CONFSTR |
| 3597 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3598 | #ifdef _CS_ARCHITECTURE |
| 3599 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 3600 | #endif |
| 3601 | #ifdef _CS_HOSTNAME |
| 3602 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 3603 | #endif |
| 3604 | #ifdef _CS_HW_PROVIDER |
| 3605 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 3606 | #endif |
| 3607 | #ifdef _CS_HW_SERIAL |
| 3608 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 3609 | #endif |
| 3610 | #ifdef _CS_INITTAB_NAME |
| 3611 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 3612 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3613 | #ifdef _CS_LFS64_CFLAGS |
| 3614 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 3615 | #endif |
| 3616 | #ifdef _CS_LFS64_LDFLAGS |
| 3617 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 3618 | #endif |
| 3619 | #ifdef _CS_LFS64_LIBS |
| 3620 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 3621 | #endif |
| 3622 | #ifdef _CS_LFS64_LINTFLAGS |
| 3623 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 3624 | #endif |
| 3625 | #ifdef _CS_LFS_CFLAGS |
| 3626 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 3627 | #endif |
| 3628 | #ifdef _CS_LFS_LDFLAGS |
| 3629 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 3630 | #endif |
| 3631 | #ifdef _CS_LFS_LIBS |
| 3632 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 3633 | #endif |
| 3634 | #ifdef _CS_LFS_LINTFLAGS |
| 3635 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 3636 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3637 | #ifdef _CS_MACHINE |
| 3638 | {"CS_MACHINE", _CS_MACHINE}, |
| 3639 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3640 | #ifdef _CS_PATH |
| 3641 | {"CS_PATH", _CS_PATH}, |
| 3642 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3643 | #ifdef _CS_RELEASE |
| 3644 | {"CS_RELEASE", _CS_RELEASE}, |
| 3645 | #endif |
| 3646 | #ifdef _CS_SRPC_DOMAIN |
| 3647 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 3648 | #endif |
| 3649 | #ifdef _CS_SYSNAME |
| 3650 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 3651 | #endif |
| 3652 | #ifdef _CS_VERSION |
| 3653 | {"CS_VERSION", _CS_VERSION}, |
| 3654 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3655 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 3656 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 3657 | #endif |
| 3658 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 3659 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 3660 | #endif |
| 3661 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 3662 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 3663 | #endif |
| 3664 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 3665 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 3666 | #endif |
| 3667 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 3668 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 3669 | #endif |
| 3670 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 3671 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 3672 | #endif |
| 3673 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 3674 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 3675 | #endif |
| 3676 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 3677 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 3678 | #endif |
| 3679 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 3680 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 3681 | #endif |
| 3682 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 3683 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 3684 | #endif |
| 3685 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 3686 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 3687 | #endif |
| 3688 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 3689 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 3690 | #endif |
| 3691 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 3692 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 3693 | #endif |
| 3694 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 3695 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 3696 | #endif |
| 3697 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 3698 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 3699 | #endif |
| 3700 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 3701 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 3702 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3703 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 3704 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 3705 | #endif |
| 3706 | #ifdef _MIPS_CS_BASE |
| 3707 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 3708 | #endif |
| 3709 | #ifdef _MIPS_CS_HOSTID |
| 3710 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 3711 | #endif |
| 3712 | #ifdef _MIPS_CS_HW_NAME |
| 3713 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 3714 | #endif |
| 3715 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 3716 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 3717 | #endif |
| 3718 | #ifdef _MIPS_CS_OSREL_MAJ |
| 3719 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 3720 | #endif |
| 3721 | #ifdef _MIPS_CS_OSREL_MIN |
| 3722 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 3723 | #endif |
| 3724 | #ifdef _MIPS_CS_OSREL_PATCH |
| 3725 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 3726 | #endif |
| 3727 | #ifdef _MIPS_CS_OS_NAME |
| 3728 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 3729 | #endif |
| 3730 | #ifdef _MIPS_CS_OS_PROVIDER |
| 3731 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 3732 | #endif |
| 3733 | #ifdef _MIPS_CS_PROCESSORS |
| 3734 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 3735 | #endif |
| 3736 | #ifdef _MIPS_CS_SERIAL |
| 3737 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 3738 | #endif |
| 3739 | #ifdef _MIPS_CS_VENDOR |
| 3740 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 3741 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3742 | }; |
| 3743 | |
| 3744 | static int |
| 3745 | conv_confstr_confname(arg, valuep) |
| 3746 | PyObject *arg; |
| 3747 | int *valuep; |
| 3748 | { |
| 3749 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 3750 | sizeof(posix_constants_confstr) |
| 3751 | / sizeof(struct constdef)); |
| 3752 | } |
| 3753 | |
| 3754 | static char posix_confstr__doc__[] = "\ |
| 3755 | confstr(name) -> string\n\ |
| 3756 | Return a string-valued system configuration variable."; |
| 3757 | |
| 3758 | static PyObject * |
| 3759 | posix_confstr(self, args) |
| 3760 | PyObject *self; |
| 3761 | PyObject *args; |
| 3762 | { |
| 3763 | PyObject *result = NULL; |
| 3764 | int name; |
| 3765 | char buffer[64]; |
| 3766 | |
| 3767 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 3768 | int len = confstr(name, buffer, sizeof(buffer)); |
| 3769 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3770 | errno = 0; |
| 3771 | if (len == 0) { |
| 3772 | if (errno != 0) |
| 3773 | posix_error(); |
| 3774 | else |
| 3775 | result = PyString_FromString(""); |
| 3776 | } |
| 3777 | else { |
| 3778 | if (len >= sizeof(buffer)) { |
| 3779 | result = PyString_FromStringAndSize(NULL, len); |
| 3780 | if (result != NULL) |
| 3781 | confstr(name, PyString_AS_STRING(result), len+1); |
| 3782 | } |
| 3783 | else |
| 3784 | result = PyString_FromString(buffer); |
| 3785 | } |
| 3786 | } |
| 3787 | return result; |
| 3788 | } |
| 3789 | #endif |
| 3790 | |
| 3791 | |
| 3792 | #ifdef HAVE_SYSCONF |
| 3793 | static struct constdef posix_constants_sysconf[] = { |
| 3794 | #ifdef _SC_2_CHAR_TERM |
| 3795 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 3796 | #endif |
| 3797 | #ifdef _SC_2_C_BIND |
| 3798 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 3799 | #endif |
| 3800 | #ifdef _SC_2_C_DEV |
| 3801 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 3802 | #endif |
| 3803 | #ifdef _SC_2_C_VERSION |
| 3804 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 3805 | #endif |
| 3806 | #ifdef _SC_2_FORT_DEV |
| 3807 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 3808 | #endif |
| 3809 | #ifdef _SC_2_FORT_RUN |
| 3810 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 3811 | #endif |
| 3812 | #ifdef _SC_2_LOCALEDEF |
| 3813 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 3814 | #endif |
| 3815 | #ifdef _SC_2_SW_DEV |
| 3816 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 3817 | #endif |
| 3818 | #ifdef _SC_2_UPE |
| 3819 | {"SC_2_UPE", _SC_2_UPE}, |
| 3820 | #endif |
| 3821 | #ifdef _SC_2_VERSION |
| 3822 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 3823 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3824 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 3825 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 3826 | #endif |
| 3827 | #ifdef _SC_ACL |
| 3828 | {"SC_ACL", _SC_ACL}, |
| 3829 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3830 | #ifdef _SC_AIO_LISTIO_MAX |
| 3831 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 3832 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3833 | #ifdef _SC_AIO_MAX |
| 3834 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 3835 | #endif |
| 3836 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 3837 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 3838 | #endif |
| 3839 | #ifdef _SC_ARG_MAX |
| 3840 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 3841 | #endif |
| 3842 | #ifdef _SC_ASYNCHRONOUS_IO |
| 3843 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 3844 | #endif |
| 3845 | #ifdef _SC_ATEXIT_MAX |
| 3846 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 3847 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3848 | #ifdef _SC_AUDIT |
| 3849 | {"SC_AUDIT", _SC_AUDIT}, |
| 3850 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3851 | #ifdef _SC_AVPHYS_PAGES |
| 3852 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 3853 | #endif |
| 3854 | #ifdef _SC_BC_BASE_MAX |
| 3855 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 3856 | #endif |
| 3857 | #ifdef _SC_BC_DIM_MAX |
| 3858 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 3859 | #endif |
| 3860 | #ifdef _SC_BC_SCALE_MAX |
| 3861 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 3862 | #endif |
| 3863 | #ifdef _SC_BC_STRING_MAX |
| 3864 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 3865 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3866 | #ifdef _SC_CAP |
| 3867 | {"SC_CAP", _SC_CAP}, |
| 3868 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3869 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 3870 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 3871 | #endif |
| 3872 | #ifdef _SC_CHAR_BIT |
| 3873 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 3874 | #endif |
| 3875 | #ifdef _SC_CHAR_MAX |
| 3876 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 3877 | #endif |
| 3878 | #ifdef _SC_CHAR_MIN |
| 3879 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 3880 | #endif |
| 3881 | #ifdef _SC_CHILD_MAX |
| 3882 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 3883 | #endif |
| 3884 | #ifdef _SC_CLK_TCK |
| 3885 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 3886 | #endif |
| 3887 | #ifdef _SC_COHER_BLKSZ |
| 3888 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 3889 | #endif |
| 3890 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 3891 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 3892 | #endif |
| 3893 | #ifdef _SC_DCACHE_ASSOC |
| 3894 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 3895 | #endif |
| 3896 | #ifdef _SC_DCACHE_BLKSZ |
| 3897 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 3898 | #endif |
| 3899 | #ifdef _SC_DCACHE_LINESZ |
| 3900 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 3901 | #endif |
| 3902 | #ifdef _SC_DCACHE_SZ |
| 3903 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 3904 | #endif |
| 3905 | #ifdef _SC_DCACHE_TBLKSZ |
| 3906 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 3907 | #endif |
| 3908 | #ifdef _SC_DELAYTIMER_MAX |
| 3909 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 3910 | #endif |
| 3911 | #ifdef _SC_EQUIV_CLASS_MAX |
| 3912 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 3913 | #endif |
| 3914 | #ifdef _SC_EXPR_NEST_MAX |
| 3915 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 3916 | #endif |
| 3917 | #ifdef _SC_FSYNC |
| 3918 | {"SC_FSYNC", _SC_FSYNC}, |
| 3919 | #endif |
| 3920 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 3921 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 3922 | #endif |
| 3923 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 3924 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 3925 | #endif |
| 3926 | #ifdef _SC_ICACHE_ASSOC |
| 3927 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 3928 | #endif |
| 3929 | #ifdef _SC_ICACHE_BLKSZ |
| 3930 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 3931 | #endif |
| 3932 | #ifdef _SC_ICACHE_LINESZ |
| 3933 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 3934 | #endif |
| 3935 | #ifdef _SC_ICACHE_SZ |
| 3936 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 3937 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3938 | #ifdef _SC_INF |
| 3939 | {"SC_INF", _SC_INF}, |
| 3940 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3941 | #ifdef _SC_INT_MAX |
| 3942 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 3943 | #endif |
| 3944 | #ifdef _SC_INT_MIN |
| 3945 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 3946 | #endif |
| 3947 | #ifdef _SC_IOV_MAX |
| 3948 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 3949 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3950 | #ifdef _SC_IP_SECOPTS |
| 3951 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 3952 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3953 | #ifdef _SC_JOB_CONTROL |
| 3954 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 3955 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3956 | #ifdef _SC_KERN_POINTERS |
| 3957 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 3958 | #endif |
| 3959 | #ifdef _SC_KERN_SIM |
| 3960 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 3961 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3962 | #ifdef _SC_LINE_MAX |
| 3963 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 3964 | #endif |
| 3965 | #ifdef _SC_LOGIN_NAME_MAX |
| 3966 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 3967 | #endif |
| 3968 | #ifdef _SC_LOGNAME_MAX |
| 3969 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 3970 | #endif |
| 3971 | #ifdef _SC_LONG_BIT |
| 3972 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 3973 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3974 | #ifdef _SC_MAC |
| 3975 | {"SC_MAC", _SC_MAC}, |
| 3976 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3977 | #ifdef _SC_MAPPED_FILES |
| 3978 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 3979 | #endif |
| 3980 | #ifdef _SC_MAXPID |
| 3981 | {"SC_MAXPID", _SC_MAXPID}, |
| 3982 | #endif |
| 3983 | #ifdef _SC_MB_LEN_MAX |
| 3984 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 3985 | #endif |
| 3986 | #ifdef _SC_MEMLOCK |
| 3987 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 3988 | #endif |
| 3989 | #ifdef _SC_MEMLOCK_RANGE |
| 3990 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 3991 | #endif |
| 3992 | #ifdef _SC_MEMORY_PROTECTION |
| 3993 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 3994 | #endif |
| 3995 | #ifdef _SC_MESSAGE_PASSING |
| 3996 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 3997 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 3998 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 3999 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 4000 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4001 | #ifdef _SC_MQ_OPEN_MAX |
| 4002 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 4003 | #endif |
| 4004 | #ifdef _SC_MQ_PRIO_MAX |
| 4005 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 4006 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4007 | #ifdef _SC_NACLS_MAX |
| 4008 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 4009 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4010 | #ifdef _SC_NGROUPS_MAX |
| 4011 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 4012 | #endif |
| 4013 | #ifdef _SC_NL_ARGMAX |
| 4014 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 4015 | #endif |
| 4016 | #ifdef _SC_NL_LANGMAX |
| 4017 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 4018 | #endif |
| 4019 | #ifdef _SC_NL_MSGMAX |
| 4020 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 4021 | #endif |
| 4022 | #ifdef _SC_NL_NMAX |
| 4023 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 4024 | #endif |
| 4025 | #ifdef _SC_NL_SETMAX |
| 4026 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 4027 | #endif |
| 4028 | #ifdef _SC_NL_TEXTMAX |
| 4029 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 4030 | #endif |
| 4031 | #ifdef _SC_NPROCESSORS_CONF |
| 4032 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 4033 | #endif |
| 4034 | #ifdef _SC_NPROCESSORS_ONLN |
| 4035 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 4036 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4037 | #ifdef _SC_NPROC_CONF |
| 4038 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 4039 | #endif |
| 4040 | #ifdef _SC_NPROC_ONLN |
| 4041 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 4042 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4043 | #ifdef _SC_NZERO |
| 4044 | {"SC_NZERO", _SC_NZERO}, |
| 4045 | #endif |
| 4046 | #ifdef _SC_OPEN_MAX |
| 4047 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 4048 | #endif |
| 4049 | #ifdef _SC_PAGESIZE |
| 4050 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 4051 | #endif |
| 4052 | #ifdef _SC_PAGE_SIZE |
| 4053 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 4054 | #endif |
| 4055 | #ifdef _SC_PASS_MAX |
| 4056 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 4057 | #endif |
| 4058 | #ifdef _SC_PHYS_PAGES |
| 4059 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 4060 | #endif |
| 4061 | #ifdef _SC_PII |
| 4062 | {"SC_PII", _SC_PII}, |
| 4063 | #endif |
| 4064 | #ifdef _SC_PII_INTERNET |
| 4065 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 4066 | #endif |
| 4067 | #ifdef _SC_PII_INTERNET_DGRAM |
| 4068 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 4069 | #endif |
| 4070 | #ifdef _SC_PII_INTERNET_STREAM |
| 4071 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 4072 | #endif |
| 4073 | #ifdef _SC_PII_OSI |
| 4074 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 4075 | #endif |
| 4076 | #ifdef _SC_PII_OSI_CLTS |
| 4077 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 4078 | #endif |
| 4079 | #ifdef _SC_PII_OSI_COTS |
| 4080 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 4081 | #endif |
| 4082 | #ifdef _SC_PII_OSI_M |
| 4083 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 4084 | #endif |
| 4085 | #ifdef _SC_PII_SOCKET |
| 4086 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 4087 | #endif |
| 4088 | #ifdef _SC_PII_XTI |
| 4089 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 4090 | #endif |
| 4091 | #ifdef _SC_POLL |
| 4092 | {"SC_POLL", _SC_POLL}, |
| 4093 | #endif |
| 4094 | #ifdef _SC_PRIORITIZED_IO |
| 4095 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 4096 | #endif |
| 4097 | #ifdef _SC_PRIORITY_SCHEDULING |
| 4098 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 4099 | #endif |
| 4100 | #ifdef _SC_REALTIME_SIGNALS |
| 4101 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 4102 | #endif |
| 4103 | #ifdef _SC_RE_DUP_MAX |
| 4104 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 4105 | #endif |
| 4106 | #ifdef _SC_RTSIG_MAX |
| 4107 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 4108 | #endif |
| 4109 | #ifdef _SC_SAVED_IDS |
| 4110 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 4111 | #endif |
| 4112 | #ifdef _SC_SCHAR_MAX |
| 4113 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 4114 | #endif |
| 4115 | #ifdef _SC_SCHAR_MIN |
| 4116 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 4117 | #endif |
| 4118 | #ifdef _SC_SELECT |
| 4119 | {"SC_SELECT", _SC_SELECT}, |
| 4120 | #endif |
| 4121 | #ifdef _SC_SEMAPHORES |
| 4122 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 4123 | #endif |
| 4124 | #ifdef _SC_SEM_NSEMS_MAX |
| 4125 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 4126 | #endif |
| 4127 | #ifdef _SC_SEM_VALUE_MAX |
| 4128 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 4129 | #endif |
| 4130 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 4131 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 4132 | #endif |
| 4133 | #ifdef _SC_SHRT_MAX |
| 4134 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 4135 | #endif |
| 4136 | #ifdef _SC_SHRT_MIN |
| 4137 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 4138 | #endif |
| 4139 | #ifdef _SC_SIGQUEUE_MAX |
| 4140 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 4141 | #endif |
| 4142 | #ifdef _SC_SIGRT_MAX |
| 4143 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 4144 | #endif |
| 4145 | #ifdef _SC_SIGRT_MIN |
| 4146 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 4147 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4148 | #ifdef _SC_SOFTPOWER |
| 4149 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 4150 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4151 | #ifdef _SC_SPLIT_CACHE |
| 4152 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 4153 | #endif |
| 4154 | #ifdef _SC_SSIZE_MAX |
| 4155 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 4156 | #endif |
| 4157 | #ifdef _SC_STACK_PROT |
| 4158 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 4159 | #endif |
| 4160 | #ifdef _SC_STREAM_MAX |
| 4161 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 4162 | #endif |
| 4163 | #ifdef _SC_SYNCHRONIZED_IO |
| 4164 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 4165 | #endif |
| 4166 | #ifdef _SC_THREADS |
| 4167 | {"SC_THREADS", _SC_THREADS}, |
| 4168 | #endif |
| 4169 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 4170 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 4171 | #endif |
| 4172 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 4173 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 4174 | #endif |
| 4175 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 4176 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 4177 | #endif |
| 4178 | #ifdef _SC_THREAD_KEYS_MAX |
| 4179 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 4180 | #endif |
| 4181 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 4182 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 4183 | #endif |
| 4184 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 4185 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 4186 | #endif |
| 4187 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 4188 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 4189 | #endif |
| 4190 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 4191 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 4192 | #endif |
| 4193 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 4194 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 4195 | #endif |
| 4196 | #ifdef _SC_THREAD_STACK_MIN |
| 4197 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 4198 | #endif |
| 4199 | #ifdef _SC_THREAD_THREADS_MAX |
| 4200 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 4201 | #endif |
| 4202 | #ifdef _SC_TIMERS |
| 4203 | {"SC_TIMERS", _SC_TIMERS}, |
| 4204 | #endif |
| 4205 | #ifdef _SC_TIMER_MAX |
| 4206 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 4207 | #endif |
| 4208 | #ifdef _SC_TTY_NAME_MAX |
| 4209 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 4210 | #endif |
| 4211 | #ifdef _SC_TZNAME_MAX |
| 4212 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 4213 | #endif |
| 4214 | #ifdef _SC_T_IOV_MAX |
| 4215 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 4216 | #endif |
| 4217 | #ifdef _SC_UCHAR_MAX |
| 4218 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 4219 | #endif |
| 4220 | #ifdef _SC_UINT_MAX |
| 4221 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 4222 | #endif |
| 4223 | #ifdef _SC_UIO_MAXIOV |
| 4224 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 4225 | #endif |
| 4226 | #ifdef _SC_ULONG_MAX |
| 4227 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 4228 | #endif |
| 4229 | #ifdef _SC_USHRT_MAX |
| 4230 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 4231 | #endif |
| 4232 | #ifdef _SC_VERSION |
| 4233 | {"SC_VERSION", _SC_VERSION}, |
| 4234 | #endif |
| 4235 | #ifdef _SC_WORD_BIT |
| 4236 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 4237 | #endif |
| 4238 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 4239 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 4240 | #endif |
| 4241 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 4242 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 4243 | #endif |
| 4244 | #ifdef _SC_XBS5_LP64_OFF64 |
| 4245 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 4246 | #endif |
| 4247 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 4248 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 4249 | #endif |
| 4250 | #ifdef _SC_XOPEN_CRYPT |
| 4251 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 4252 | #endif |
| 4253 | #ifdef _SC_XOPEN_ENH_I18N |
| 4254 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 4255 | #endif |
| 4256 | #ifdef _SC_XOPEN_LEGACY |
| 4257 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 4258 | #endif |
| 4259 | #ifdef _SC_XOPEN_REALTIME |
| 4260 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 4261 | #endif |
| 4262 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 4263 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 4264 | #endif |
| 4265 | #ifdef _SC_XOPEN_SHM |
| 4266 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 4267 | #endif |
| 4268 | #ifdef _SC_XOPEN_UNIX |
| 4269 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 4270 | #endif |
| 4271 | #ifdef _SC_XOPEN_VERSION |
| 4272 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 4273 | #endif |
| 4274 | #ifdef _SC_XOPEN_XCU_VERSION |
| 4275 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 4276 | #endif |
| 4277 | #ifdef _SC_XOPEN_XPG2 |
| 4278 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 4279 | #endif |
| 4280 | #ifdef _SC_XOPEN_XPG3 |
| 4281 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 4282 | #endif |
| 4283 | #ifdef _SC_XOPEN_XPG4 |
| 4284 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 4285 | #endif |
| 4286 | }; |
| 4287 | |
| 4288 | static int |
| 4289 | conv_sysconf_confname(arg, valuep) |
| 4290 | PyObject *arg; |
| 4291 | int *valuep; |
| 4292 | { |
| 4293 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 4294 | sizeof(posix_constants_sysconf) |
| 4295 | / sizeof(struct constdef)); |
| 4296 | } |
| 4297 | |
| 4298 | static char posix_sysconf__doc__[] = "\ |
| 4299 | sysconf(name) -> integer\n\ |
| 4300 | Return an integer-valued system configuration variable."; |
| 4301 | |
| 4302 | static PyObject * |
| 4303 | posix_sysconf(self, args) |
| 4304 | PyObject *self; |
| 4305 | PyObject *args; |
| 4306 | { |
| 4307 | PyObject *result = NULL; |
| 4308 | int name; |
| 4309 | |
| 4310 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 4311 | int value; |
| 4312 | |
| 4313 | errno = 0; |
| 4314 | value = sysconf(name); |
| 4315 | if (value == -1 && errno != 0) |
| 4316 | posix_error(); |
| 4317 | else |
| 4318 | result = PyInt_FromLong(value); |
| 4319 | } |
| 4320 | return result; |
| 4321 | } |
| 4322 | #endif |
| 4323 | |
| 4324 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4325 | /* This code is used to ensure that the tables of configuration value names |
| 4326 | * are in sorted order as required by conv_confname(), and also to build the |
| 4327 | * the exported dictionaries that are used to publish information about the |
| 4328 | * names available on the host platform. |
| 4329 | * |
| 4330 | * Sorting the table at runtime ensures that the table is properly ordered |
| 4331 | * when used, even for platforms we're not able to test on. It also makes |
| 4332 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4333 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4334 | |
| 4335 | static int |
| 4336 | cmp_constdefs(v1, v2) |
| 4337 | const void *v1; |
| 4338 | const void *v2; |
| 4339 | { |
| 4340 | const struct constdef *c1 = |
| 4341 | (const struct constdef *) v1; |
| 4342 | const struct constdef *c2 = |
| 4343 | (const struct constdef *) v2; |
| 4344 | |
| 4345 | return strcmp(c1->name, c2->name); |
| 4346 | } |
| 4347 | |
| 4348 | static int |
| 4349 | setup_confname_table(table, tablesize, tablename, moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4350 | struct constdef *table; |
| 4351 | size_t tablesize; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4352 | char * tablename; |
| 4353 | PyObject *moddict; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4354 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4355 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 4356 | size_t i; |
| 4357 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4358 | |
| 4359 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 4360 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 4361 | if (d == NULL) |
| 4362 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4363 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 4364 | for (i=0; i < tablesize; ++i) { |
| 4365 | PyObject *o = PyInt_FromLong(table[i].value); |
| 4366 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 4367 | Py_XDECREF(o); |
| 4368 | Py_DECREF(d); |
| 4369 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4370 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 4371 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4372 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 4373 | status = PyDict_SetItemString(moddict, tablename, d); |
| 4374 | Py_DECREF(d); |
| 4375 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4378 | /* Return -1 on failure, 0 on success. */ |
| 4379 | static int |
| 4380 | setup_confname_tables(moddict) |
| 4381 | PyObject *moddict; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4382 | { |
| 4383 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4384 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4385 | sizeof(posix_constants_pathconf) |
| 4386 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4387 | "pathconf_names", moddict)) |
| 4388 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4389 | #endif |
| 4390 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4391 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4392 | sizeof(posix_constants_confstr) |
| 4393 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4394 | "confstr_names", moddict)) |
| 4395 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4396 | #endif |
| 4397 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4398 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4399 | sizeof(posix_constants_sysconf) |
| 4400 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4401 | "sysconf_names", moddict)) |
| 4402 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4403 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4404 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4405 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4406 | |
| 4407 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4408 | static char posix_abort__doc__[] = "\ |
| 4409 | abort() -> does not return!\n\ |
| 4410 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 4411 | in the hardest way possible on the hosting operating system."; |
| 4412 | |
| 4413 | static PyObject * |
| 4414 | posix_abort(self, args) |
| 4415 | PyObject *self; |
| 4416 | PyObject *args; |
| 4417 | { |
| 4418 | if (!PyArg_ParseTuple(args, ":abort")) |
| 4419 | return NULL; |
| 4420 | abort(); |
| 4421 | /*NOTREACHED*/ |
| 4422 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 4423 | return NULL; |
| 4424 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4425 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4426 | |
| 4427 | static PyMethodDef posix_methods[] = { |
| 4428 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 4429 | #ifdef HAVE_TTYNAME |
| 4430 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 4431 | #endif |
| 4432 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 4433 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4434 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4435 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4436 | #endif /* HAVE_CHOWN */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4437 | #ifdef HAVE_CTERMID |
| 4438 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 4439 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 4440 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4441 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 4442 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4443 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4444 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4445 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4446 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 4447 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 4448 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4449 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4450 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4451 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4452 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4453 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4454 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4455 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 4456 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 4457 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4458 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4459 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4460 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4461 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4462 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4463 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4464 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4465 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4466 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4467 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4468 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 4469 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 4470 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4471 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4472 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4473 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4474 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4475 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4476 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 4477 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4478 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4479 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4480 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 4481 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4482 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4483 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4484 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4485 | #endif /* HAVE_FORK */ |
| 4486 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4487 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4488 | #endif /* HAVE_GETEGID */ |
| 4489 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4490 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4491 | #endif /* HAVE_GETEUID */ |
| 4492 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4493 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4494 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4495 | #ifdef HAVE_GETGROUPS |
| 4496 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 4497 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4498 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4499 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4500 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4501 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4502 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4503 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4504 | #endif /* HAVE_GETPPID */ |
| 4505 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4506 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4507 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4508 | #ifdef HAVE_GETLOGIN |
| 4509 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 4510 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4511 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4512 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4513 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4514 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4515 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4516 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4517 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4518 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4519 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4520 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4521 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4522 | #endif /* HAVE_SETUID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4523 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4524 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4525 | #endif /* HAVE_SETGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4526 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4527 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4528 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4529 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4530 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4531 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4532 | #ifdef HAVE_WAITPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4533 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4534 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4535 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4536 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4537 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4538 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4539 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4540 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4541 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4542 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4543 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4544 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4545 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4546 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4547 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 4548 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 4549 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 4550 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 4551 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 4552 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 4553 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 4554 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 4555 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4556 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4557 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4558 | #endif |
| 4559 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4560 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4561 | #endif |
| 4562 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4563 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4564 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4565 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4566 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4567 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4568 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4569 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4570 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 4571 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4572 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 4573 | #endif |
| 4574 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4575 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 4576 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4577 | #ifdef HAVE_SYS_WAIT_H |
| 4578 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4579 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4580 | #endif /* WIFSTOPPED */ |
| 4581 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4582 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4583 | #endif /* WIFSIGNALED */ |
| 4584 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4585 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4586 | #endif /* WIFEXITED */ |
| 4587 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4588 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4589 | #endif /* WEXITSTATUS */ |
| 4590 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4591 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4592 | #endif /* WTERMSIG */ |
| 4593 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4594 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4595 | #endif /* WSTOPSIG */ |
| 4596 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4597 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4598 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4599 | #endif |
| 4600 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4601 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4602 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4603 | #ifdef HAVE_TMPNAM |
| 4604 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 4605 | #endif |
| 4606 | #ifdef HAVE_TEMPNAM |
| 4607 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 4608 | #endif |
| 4609 | #ifdef HAVE_TMPNAM |
| 4610 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 4611 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4612 | #ifdef HAVE_CONFSTR |
| 4613 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 4614 | #endif |
| 4615 | #ifdef HAVE_SYSCONF |
| 4616 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 4617 | #endif |
| 4618 | #ifdef HAVE_FPATHCONF |
| 4619 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 4620 | #endif |
| 4621 | #ifdef HAVE_PATHCONF |
| 4622 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 4623 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4624 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4625 | {NULL, NULL} /* Sentinel */ |
| 4626 | }; |
| 4627 | |
| 4628 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 4629 | static int |
| 4630 | ins(d, symbol, value) |
| 4631 | PyObject* d; |
| 4632 | char* symbol; |
| 4633 | long value; |
| 4634 | { |
| 4635 | PyObject* v = PyInt_FromLong(value); |
| 4636 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 4637 | return -1; /* triggers fatal error */ |
| 4638 | |
| 4639 | Py_DECREF(v); |
| 4640 | return 0; |
| 4641 | } |
| 4642 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4643 | #if defined(PYOS_OS2) |
| 4644 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 4645 | static int insertvalues(PyObject *d) |
| 4646 | { |
| 4647 | APIRET rc; |
| 4648 | ULONG values[QSV_MAX+1]; |
| 4649 | PyObject *v; |
| 4650 | char *ver, tmp[10]; |
| 4651 | |
| 4652 | Py_BEGIN_ALLOW_THREADS |
| 4653 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 4654 | Py_END_ALLOW_THREADS |
| 4655 | |
| 4656 | if (rc != NO_ERROR) { |
| 4657 | os2_error(rc); |
| 4658 | return -1; |
| 4659 | } |
| 4660 | |
| 4661 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 4662 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 4663 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 4664 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 4665 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 4666 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 4667 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 4668 | |
| 4669 | switch (values[QSV_VERSION_MINOR]) { |
| 4670 | case 0: ver = "2.00"; break; |
| 4671 | case 10: ver = "2.10"; break; |
| 4672 | case 11: ver = "2.11"; break; |
| 4673 | case 30: ver = "3.00"; break; |
| 4674 | case 40: ver = "4.00"; break; |
| 4675 | case 50: ver = "5.00"; break; |
| 4676 | default: |
| 4677 | sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR], |
| 4678 | values[QSV_VERSION_MINOR]); |
| 4679 | ver = &tmp[0]; |
| 4680 | } |
| 4681 | |
| 4682 | /* Add Indicator of the Version of the Operating System */ |
| 4683 | v = PyString_FromString(ver); |
| 4684 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 4685 | return -1; |
| 4686 | Py_DECREF(v); |
| 4687 | |
| 4688 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 4689 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 4690 | tmp[1] = ':'; |
| 4691 | tmp[2] = '\0'; |
| 4692 | |
| 4693 | v = PyString_FromString(tmp); |
| 4694 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 4695 | return -1; |
| 4696 | Py_DECREF(v); |
| 4697 | |
| 4698 | return 0; |
| 4699 | } |
| 4700 | #endif |
| 4701 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 4702 | static int |
| 4703 | all_ins(d) |
| 4704 | PyObject* d; |
| 4705 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4706 | #ifdef F_OK |
| 4707 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
| 4708 | #endif |
| 4709 | #ifdef R_OK |
| 4710 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
| 4711 | #endif |
| 4712 | #ifdef W_OK |
| 4713 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
| 4714 | #endif |
| 4715 | #ifdef X_OK |
| 4716 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
| 4717 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4718 | #ifdef NGROUPS_MAX |
| 4719 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 4720 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4721 | #ifdef TMP_MAX |
| 4722 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 4723 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 4724 | #ifdef WNOHANG |
| 4725 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
| 4726 | #endif |
| 4727 | #ifdef O_RDONLY |
| 4728 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 4729 | #endif |
| 4730 | #ifdef O_WRONLY |
| 4731 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 4732 | #endif |
| 4733 | #ifdef O_RDWR |
| 4734 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 4735 | #endif |
| 4736 | #ifdef O_NDELAY |
| 4737 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 4738 | #endif |
| 4739 | #ifdef O_NONBLOCK |
| 4740 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 4741 | #endif |
| 4742 | #ifdef O_APPEND |
| 4743 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 4744 | #endif |
| 4745 | #ifdef O_DSYNC |
| 4746 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 4747 | #endif |
| 4748 | #ifdef O_RSYNC |
| 4749 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 4750 | #endif |
| 4751 | #ifdef O_SYNC |
| 4752 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 4753 | #endif |
| 4754 | #ifdef O_NOCTTY |
| 4755 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 4756 | #endif |
| 4757 | #ifdef O_CREAT |
| 4758 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 4759 | #endif |
| 4760 | #ifdef O_EXCL |
| 4761 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 4762 | #endif |
| 4763 | #ifdef O_TRUNC |
| 4764 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 4765 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 4766 | #ifdef O_BINARY |
| 4767 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 4768 | #endif |
| 4769 | #ifdef O_TEXT |
| 4770 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 4771 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4772 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 4773 | #ifdef HAVE_SPAWNV |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 4774 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 4775 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 4776 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 4777 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 4778 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 4779 | #endif |
| 4780 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4781 | #if defined(PYOS_OS2) |
| 4782 | if (insertvalues(d)) return -1; |
| 4783 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 4784 | return 0; |
| 4785 | } |
| 4786 | |
| 4787 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4788 | #if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4789 | #define INITFUNC initnt |
| 4790 | #define MODNAME "nt" |
| 4791 | #else |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4792 | #if defined(PYOS_OS2) |
| 4793 | #define INITFUNC initos2 |
| 4794 | #define MODNAME "os2" |
| 4795 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4796 | #define INITFUNC initposix |
| 4797 | #define MODNAME "posix" |
| 4798 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4799 | #endif |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4800 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 4801 | DL_EXPORT(void) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4802 | INITFUNC() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4803 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4804 | PyObject *m, *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4805 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4806 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4807 | posix_methods, |
| 4808 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4809 | (PyObject *)NULL, |
| 4810 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4811 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4812 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4813 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4814 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4815 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4816 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4817 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4818 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 4819 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 4820 | return; |
| 4821 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4822 | if (setup_confname_tables(d)) |
| 4823 | return; |
| 4824 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 4825 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4826 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 4827 | #ifdef HAVE_PUTENV |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4828 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 4829 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4830 | } |