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