Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* POSIX module implementation */ |
| 33 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 34 | /* This file is also used for Windows NT and MS-Win. In that case the module |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 35 | actually calls itself 'nt', not 'posix', and a few functions are |
| 36 | either unimplemented or implemented differently. The source |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 37 | assumes that for Windows NT, the macro 'MS_WIN32' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 38 | of the compiler used. Different compilers define their own feature |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 39 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 41 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 43 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | #include "modsupport.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 45 | #include "ceval.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 47 | #include <string.h> |
| 48 | #include <errno.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/stat.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 51 | #ifdef HAVE_SYS_WAIT_H |
| 52 | #include <sys/wait.h> /* For WNOHANG */ |
| 53 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 54 | |
| 55 | #include "mytime.h" /* For clock_t on some systems */ |
| 56 | |
| 57 | #ifdef HAVE_FCNTL_H |
| 58 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 59 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 61 | /* Various compilers have only certain posix functions */ |
| 62 | #ifdef __WATCOMC__ /* Watcom compiler */ |
| 63 | #define HAVE_GETCWD 1 |
| 64 | #define HAVE_OPENDIR 1 |
| 65 | #define HAVE_SYSTEM 1 |
| 66 | #if defined(__OS2__) |
| 67 | #define HAVE_EXECV 1 |
| 68 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 69 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 70 | #include <process.h> |
| 71 | #else |
| 72 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 73 | #define HAVE_EXECV 1 |
| 74 | #define HAVE_GETCWD 1 |
| 75 | #define HAVE_GETEGID 1 |
| 76 | #define HAVE_GETEUID 1 |
| 77 | #define HAVE_GETGID 1 |
| 78 | #define HAVE_GETPPID 1 |
| 79 | #define HAVE_GETUID 1 |
| 80 | #define HAVE_KILL 1 |
| 81 | #define HAVE_OPENDIR 1 |
| 82 | #define HAVE_PIPE 1 |
| 83 | #define HAVE_POPEN 1 |
| 84 | #define HAVE_SYSTEM 1 |
| 85 | #define HAVE_WAIT 1 |
| 86 | #else |
| 87 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 88 | #define HAVE_GETCWD 1 |
| 89 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 90 | #define HAVE_EXECV 1 |
| 91 | #define HAVE_PIPE 1 |
| 92 | #define HAVE_POPEN 1 |
| 93 | #define HAVE_SYSTEM 1 |
| 94 | #else /* 16-bit Windows */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 95 | #endif /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 96 | #else /* all other compilers */ |
| 97 | /* Unix functions that the configure script doesn't check for */ |
| 98 | #define HAVE_EXECV 1 |
| 99 | #define HAVE_FORK 1 |
| 100 | #define HAVE_GETCWD 1 |
| 101 | #define HAVE_GETEGID 1 |
| 102 | #define HAVE_GETEUID 1 |
| 103 | #define HAVE_GETGID 1 |
| 104 | #define HAVE_GETPPID 1 |
| 105 | #define HAVE_GETUID 1 |
| 106 | #define HAVE_KILL 1 |
| 107 | #define HAVE_OPENDIR 1 |
| 108 | #define HAVE_PIPE 1 |
| 109 | #define HAVE_POPEN 1 |
| 110 | #define HAVE_SYSTEM 1 |
| 111 | #define HAVE_WAIT 1 |
| 112 | #endif /* _MSC_VER */ |
| 113 | #endif /* __BORLANDC__ */ |
| 114 | #endif /* ! __WATCOMC__ */ |
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 |
| 126 | /* #undef HAVE_GETCWD */ |
| 127 | #endif |
| 128 | |
| 129 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 130 | /* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */ |
| 131 | extern int rename(); |
| 132 | extern int pclose(); |
| 133 | extern int lstat(); |
| 134 | extern int symlink(); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 135 | #else /* !HAVE_UNISTD_H */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 136 | #if defined(__WATCOMC__) || defined(_MSC_VER) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | extern int mkdir PROTO((const char *)); |
| 138 | #else |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 139 | extern int mkdir PROTO((const char *, mode_t)); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | #endif |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 141 | extern int chdir PROTO((const char *)); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 142 | extern int rmdir PROTO((const char *)); |
| 143 | extern int chmod PROTO((const char *, mode_t)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 144 | extern int chown PROTO((const char *, uid_t, gid_t)); |
| 145 | extern char *getcwd PROTO((char *, int)); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 146 | extern char *strerror PROTO((int)); |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 147 | extern int link PROTO((const char *, const char *)); |
| 148 | extern int rename PROTO((const char *, const char *)); |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 149 | extern int stat PROTO((const char *, struct stat *)); |
| 150 | extern int unlink PROTO((const char *)); |
| 151 | extern int pclose PROTO((FILE *)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 152 | #ifdef HAVE_SYMLINK |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 153 | extern int symlink PROTO((const char *, const char *)); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 154 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 155 | #ifdef HAVE_LSTAT |
| 156 | extern int lstat PROTO((const char *, struct stat *)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 157 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 158 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 160 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 162 | #ifdef HAVE_UTIME_H |
| 163 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 164 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 166 | #ifdef HAVE_SYS_UTIME_H |
| 167 | #include <sys/utime.h> |
| 168 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 169 | #endif /* HAVE_SYS_UTIME_H */ |
| 170 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 171 | #ifdef HAVE_SYS_TIMES_H |
| 172 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 173 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 174 | |
| 175 | #ifdef HAVE_SYS_PARAM_H |
| 176 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 177 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 178 | |
| 179 | #ifdef HAVE_SYS_UTSNAME_H |
| 180 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 181 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 182 | |
| 183 | #ifndef MAXPATHLEN |
| 184 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 185 | #endif /* MAXPATHLEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 187 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 188 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 189 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 190 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 191 | #ifdef __WATCOMC__ |
| 192 | #include <direct.h> |
| 193 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 194 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 196 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 197 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 198 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 200 | #endif |
| 201 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 203 | #endif |
| 204 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 206 | #endif |
| 207 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 209 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 210 | #include <direct.h> |
| 211 | #include <io.h> |
| 212 | #include <process.h> |
| 213 | #include <windows.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 214 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 215 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 216 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 217 | #else /* 16-bit Windows */ |
| 218 | #include <dos.h> |
| 219 | #include <ctype.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 220 | #endif /* MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 221 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
| 223 | #ifdef OS2 |
| 224 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 225 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | |
| 227 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 228 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 229 | #if !defined(_MSC_VER) && !defined(__WATCOMC__) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 230 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 231 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 232 | |
| 233 | static object * |
| 234 | convertenviron() |
| 235 | { |
| 236 | object *d; |
| 237 | char **e; |
| 238 | d = newdictobject(); |
| 239 | if (d == NULL) |
| 240 | return NULL; |
| 241 | if (environ == NULL) |
| 242 | return d; |
| 243 | /* XXX This part ignores errors */ |
| 244 | for (e = environ; *e != NULL; e++) { |
| 245 | object *v; |
| 246 | char *p = strchr(*e, '='); |
| 247 | if (p == NULL) |
| 248 | continue; |
| 249 | v = newstringobject(p+1); |
| 250 | if (v == NULL) |
| 251 | continue; |
| 252 | *p = '\0'; |
| 253 | (void) dictinsert(d, *e, v); |
| 254 | *p = '='; |
| 255 | DECREF(v); |
| 256 | } |
| 257 | return d; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | static object *PosixError; /* Exception posix.error */ |
| 262 | |
| 263 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 264 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 265 | static object * posix_error() |
| 266 | { |
| 267 | return err_errno(PosixError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
| 271 | /* POSIX generic methods */ |
| 272 | |
| 273 | static object * |
| 274 | posix_1str(args, func) |
| 275 | object *args; |
| 276 | int (*func) FPROTO((const char *)); |
| 277 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 278 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 279 | int res; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 280 | if (!getargs(args, "s", &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 282 | BGN_SAVE |
| 283 | res = (*func)(path1); |
| 284 | END_SAVE |
| 285 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | return posix_error(); |
| 287 | INCREF(None); |
| 288 | return None; |
| 289 | } |
| 290 | |
| 291 | static object * |
| 292 | posix_2str(args, func) |
| 293 | object *args; |
| 294 | int (*func) FPROTO((const char *, const char *)); |
| 295 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 296 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 297 | int res; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 298 | if (!getargs(args, "(ss)", &path1, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 299 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 300 | BGN_SAVE |
| 301 | res = (*func)(path1, path2); |
| 302 | END_SAVE |
| 303 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | return posix_error(); |
| 305 | INCREF(None); |
| 306 | return None; |
| 307 | } |
| 308 | |
| 309 | static object * |
| 310 | posix_strint(args, func) |
| 311 | object *args; |
| 312 | int (*func) FPROTO((const char *, int)); |
| 313 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 314 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | int i; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 316 | int res; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 317 | if (!getargs(args, "(si)", &path, &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 319 | BGN_SAVE |
| 320 | res = (*func)(path, i); |
| 321 | END_SAVE |
| 322 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | return posix_error(); |
| 324 | INCREF(None); |
| 325 | return None; |
| 326 | } |
| 327 | |
| 328 | static object * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 329 | posix_strintint(args, func) |
| 330 | object *args; |
| 331 | int (*func) FPROTO((const char *, int, int)); |
| 332 | { |
| 333 | char *path; |
| 334 | int i,i2; |
| 335 | int res; |
| 336 | if (!getargs(args, "(sii)", &path, &i, &i2)) |
| 337 | return NULL; |
| 338 | BGN_SAVE |
| 339 | res = (*func)(path, i, i2); |
| 340 | END_SAVE |
| 341 | if (res < 0) |
| 342 | return posix_error(); |
| 343 | INCREF(None); |
| 344 | return None; |
| 345 | } |
| 346 | |
| 347 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | posix_do_stat(self, args, statfunc) |
| 349 | object *self; |
| 350 | object *args; |
| 351 | int (*statfunc) FPROTO((const char *, struct stat *)); |
| 352 | { |
| 353 | struct stat st; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 354 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 355 | int res; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 356 | if (!getargs(args, "s", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 358 | BGN_SAVE |
| 359 | res = (*statfunc)(path, &st); |
| 360 | END_SAVE |
| 361 | if (res != 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | return posix_error(); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 363 | return mkvalue("(llllllllll)", |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 364 | (long)st.st_mode, |
| 365 | (long)st.st_ino, |
| 366 | (long)st.st_dev, |
| 367 | (long)st.st_nlink, |
| 368 | (long)st.st_uid, |
| 369 | (long)st.st_gid, |
| 370 | (long)st.st_size, |
| 371 | (long)st.st_atime, |
| 372 | (long)st.st_mtime, |
| 373 | (long)st.st_ctime); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | |
| 377 | /* POSIX methods */ |
| 378 | |
| 379 | static object * |
| 380 | posix_chdir(self, args) |
| 381 | object *self; |
| 382 | object *args; |
| 383 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 384 | return posix_1str(args, chdir); |
| 385 | } |
| 386 | |
| 387 | static object * |
| 388 | posix_chmod(self, args) |
| 389 | object *self; |
| 390 | object *args; |
| 391 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 392 | return posix_strint(args, chmod); |
| 393 | } |
| 394 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 395 | #ifdef HAVE_CHOWN |
| 396 | static object * |
| 397 | posix_chown(self, args) |
| 398 | object *self; |
| 399 | object *args; |
| 400 | { |
| 401 | return posix_strintint(args, chown); |
| 402 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 403 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 404 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 405 | #ifdef HAVE_GETCWD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 406 | static object * |
| 407 | posix_getcwd(self, args) |
| 408 | object *self; |
| 409 | object *args; |
| 410 | { |
| 411 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 412 | char *res; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 413 | if (!getnoarg(args)) |
| 414 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 415 | BGN_SAVE |
| 416 | res = getcwd(buf, sizeof buf); |
| 417 | END_SAVE |
| 418 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | return posix_error(); |
| 420 | return newstringobject(buf); |
| 421 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 422 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 423 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 424 | #ifdef HAVE_LINK |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 425 | static object * |
| 426 | posix_link(self, args) |
| 427 | object *self; |
| 428 | object *args; |
| 429 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 430 | return posix_2str(args, link); |
| 431 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 432 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 434 | static object * |
| 435 | posix_listdir(self, args) |
| 436 | object *self; |
| 437 | object *args; |
| 438 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 439 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 440 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 441 | char *name; |
| 442 | int len; |
| 443 | object *d, *v; |
| 444 | HANDLE hFindFile; |
| 445 | WIN32_FIND_DATA FileData; |
| 446 | char namebuf[MAX_PATH+5]; |
| 447 | |
| 448 | if (!getargs(args, "s#", &name, &len)) |
| 449 | return NULL; |
| 450 | if (len >= MAX_PATH) { |
| 451 | err_setstr(ValueError, "path too long"); |
| 452 | return NULL; |
| 453 | } |
| 454 | strcpy(namebuf, name); |
| 455 | if (namebuf[len-1] != '/' && namebuf[len-1] != '\\') |
| 456 | namebuf[len++] = '/'; |
| 457 | strcpy(namebuf + len, "*.*"); |
| 458 | |
| 459 | if ((d = newlistobject(0)) == NULL) |
| 460 | return NULL; |
| 461 | |
| 462 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 463 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 464 | errno = GetLastError(); |
| 465 | return posix_error(); |
| 466 | } |
| 467 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 468 | if (FileData.cFileName[0] == '.' && |
| 469 | (FileData.cFileName[1] == '\0' || |
| 470 | FileData.cFileName[1] == '.' && |
| 471 | FileData.cFileName[2] == '\0')) |
| 472 | continue; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 473 | v = newstringobject(FileData.cFileName); |
| 474 | if (v == NULL) { |
| 475 | DECREF(d); |
| 476 | d = NULL; |
| 477 | break; |
| 478 | } |
| 479 | if (addlistitem(d, v) != 0) { |
| 480 | DECREF(v); |
| 481 | DECREF(d); |
| 482 | d = NULL; |
| 483 | break; |
| 484 | } |
| 485 | DECREF(v); |
| 486 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 487 | |
| 488 | if (FindClose(hFindFile) == FALSE) { |
| 489 | errno = GetLastError(); |
| 490 | return posix_error(); |
| 491 | } |
| 492 | |
| 493 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 494 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 495 | #else /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 496 | #ifdef _MSC_VER /* 16-bit Windows */ |
| 497 | |
| 498 | #ifndef MAX_PATH |
| 499 | #define MAX_PATH 250 |
| 500 | #endif |
| 501 | char *name, *pt; |
| 502 | int len; |
| 503 | object *d, *v; |
| 504 | char namebuf[MAX_PATH+5]; |
| 505 | struct _find_t ep; |
| 506 | |
| 507 | if (!getargs(args, "s#", &name, &len)) |
| 508 | return NULL; |
| 509 | if (len >= MAX_PATH) { |
| 510 | err_setstr(ValueError, "path too long"); |
| 511 | return NULL; |
| 512 | } |
| 513 | strcpy(namebuf, name); |
| 514 | for (pt = namebuf; *pt; pt++) |
| 515 | if (*pt == '/') |
| 516 | *pt = '\\'; |
| 517 | if (namebuf[len-1] != '\\') |
| 518 | namebuf[len++] = '\\'; |
| 519 | strcpy(namebuf + len, "*.*"); |
| 520 | |
| 521 | if ((d = newlistobject(0)) == NULL) |
| 522 | return NULL; |
| 523 | |
| 524 | if (_dos_findfirst(namebuf, _A_RDONLY | |
| 525 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0){ |
| 526 | errno = ENOENT; |
| 527 | return posix_error(); |
| 528 | } |
| 529 | do { |
| 530 | if (ep.name[0] == '.' && |
| 531 | (ep.name[1] == '\0' || |
| 532 | ep.name[1] == '.' && |
| 533 | ep.name[2] == '\0')) |
| 534 | continue; |
| 535 | strcpy(namebuf, ep.name); |
| 536 | for (pt = namebuf; *pt; pt++) |
| 537 | if (isupper(*pt)) |
| 538 | *pt = tolower(*pt); |
| 539 | v = newstringobject(namebuf); |
| 540 | if (v == NULL) { |
| 541 | DECREF(d); |
| 542 | d = NULL; |
| 543 | break; |
| 544 | } |
| 545 | if (addlistitem(d, v) != 0) { |
| 546 | DECREF(v); |
| 547 | DECREF(d); |
| 548 | d = NULL; |
| 549 | break; |
| 550 | } |
| 551 | DECREF(v); |
| 552 | } while (_dos_findnext(&ep) == 0); |
| 553 | |
| 554 | return d; |
| 555 | |
| 556 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 557 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 558 | char *name; |
| 559 | object *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 560 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 561 | struct dirent *ep; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 562 | if (!getargs(args, "s", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 563 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 564 | BGN_SAVE |
| 565 | if ((dirp = opendir(name)) == NULL) { |
| 566 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 567 | return posix_error(); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 568 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 569 | if ((d = newlistobject(0)) == NULL) { |
| 570 | closedir(dirp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 571 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 572 | return NULL; |
| 573 | } |
| 574 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 575 | if (ep->d_name[0] == '.' && |
| 576 | (NAMLEN(ep) == 1 || |
| 577 | ep->d_name[1] == '.' && NAMLEN(ep) == 2)) |
| 578 | continue; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 579 | v = newsizedstringobject(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 580 | if (v == NULL) { |
| 581 | DECREF(d); |
| 582 | d = NULL; |
| 583 | break; |
| 584 | } |
| 585 | if (addlistitem(d, v) != 0) { |
| 586 | DECREF(v); |
| 587 | DECREF(d); |
| 588 | d = NULL; |
| 589 | break; |
| 590 | } |
| 591 | DECREF(v); |
| 592 | } |
| 593 | closedir(dirp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 594 | END_SAVE |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 595 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 596 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 597 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 598 | #endif /* !_MSC_VER */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 599 | #endif /* !MS_WIN32 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static object * |
| 603 | posix_mkdir(self, args) |
| 604 | object *self; |
| 605 | object *args; |
| 606 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 607 | int res; |
| 608 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 609 | int mode = 0777; |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 610 | if (!newgetargs(args, "s|i", &path, &mode)) |
| 611 | return NULL; |
| 612 | BGN_SAVE |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 613 | #if defined(__WATCOMC__) || defined(_MSC_VER) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 614 | res = mkdir(path); |
| 615 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 616 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 617 | #endif |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 618 | END_SAVE |
| 619 | if (res < 0) |
| 620 | return posix_error(); |
| 621 | INCREF(None); |
| 622 | return None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 625 | #ifdef HAVE_NICE |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 626 | static object * |
| 627 | posix_nice(self, args) |
| 628 | object *self; |
| 629 | object *args; |
| 630 | { |
| 631 | int increment, value; |
| 632 | |
| 633 | if (!getargs(args, "i", &increment)) |
| 634 | return NULL; |
| 635 | value = nice(increment); |
| 636 | if (value == -1) |
| 637 | return posix_error(); |
| 638 | return newintobject((long) value); |
| 639 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 640 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 641 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 642 | static object * |
| 643 | posix_rename(self, args) |
| 644 | object *self; |
| 645 | object *args; |
| 646 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 647 | return posix_2str(args, rename); |
| 648 | } |
| 649 | |
| 650 | static object * |
| 651 | posix_rmdir(self, args) |
| 652 | object *self; |
| 653 | object *args; |
| 654 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 655 | return posix_1str(args, rmdir); |
| 656 | } |
| 657 | |
| 658 | static object * |
| 659 | posix_stat(self, args) |
| 660 | object *self; |
| 661 | object *args; |
| 662 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 663 | return posix_do_stat(self, args, stat); |
| 664 | } |
| 665 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 666 | #ifdef HAVE_SYSTEM |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 667 | static object * |
| 668 | posix_system(self, args) |
| 669 | object *self; |
| 670 | object *args; |
| 671 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 672 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 673 | long sts; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 674 | if (!getargs(args, "s", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 675 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 676 | BGN_SAVE |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 677 | sts = system(command); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 678 | END_SAVE |
| 679 | return newintobject(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 680 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 681 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 682 | |
| 683 | static object * |
| 684 | posix_umask(self, args) |
| 685 | object *self; |
| 686 | object *args; |
| 687 | { |
| 688 | int i; |
| 689 | if (!getintarg(args, &i)) |
| 690 | return NULL; |
| 691 | i = umask(i); |
| 692 | if (i < 0) |
| 693 | return posix_error(); |
| 694 | return newintobject((long)i); |
| 695 | } |
| 696 | |
| 697 | static object * |
| 698 | posix_unlink(self, args) |
| 699 | object *self; |
| 700 | object *args; |
| 701 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 702 | return posix_1str(args, unlink); |
| 703 | } |
| 704 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 705 | #ifdef HAVE_UNAME |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 706 | static object * |
| 707 | posix_uname(self, args) |
| 708 | object *self; |
| 709 | object *args; |
| 710 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 711 | struct utsname u; |
| 712 | object *v; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 713 | int res; |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 714 | if (!getnoarg(args)) |
| 715 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 716 | BGN_SAVE |
| 717 | res = uname(&u); |
| 718 | END_SAVE |
| 719 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 720 | return posix_error(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 721 | return mkvalue("(sssss)", |
| 722 | u.sysname, |
| 723 | u.nodename, |
| 724 | u.release, |
| 725 | u.version, |
| 726 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 727 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 728 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 729 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 730 | static object * |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 731 | posix_utime(self, args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 732 | object *self; |
| 733 | object *args; |
| 734 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 735 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 736 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 737 | int res; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 738 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 739 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 740 | struct utimbuf buf; |
| 741 | #define ATIME buf.actime |
| 742 | #define MTIME buf.modtime |
| 743 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 744 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 745 | time_t buf[2]; |
| 746 | #define ATIME buf[0] |
| 747 | #define MTIME buf[1] |
| 748 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 749 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 750 | |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 751 | if (!getargs(args, "(s(ll))", &path, &atime, &mtime)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 752 | return NULL; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 753 | ATIME = atime; |
Guido van Rossum | d1b3481 | 1995-02-07 15:39:29 +0000 | [diff] [blame] | 754 | MTIME = mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 755 | BGN_SAVE |
| 756 | res = utime(path, UTIME_ARG); |
| 757 | END_SAVE |
| 758 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 759 | return posix_error(); |
| 760 | INCREF(None); |
| 761 | return None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 762 | #undef UTIME_ARG |
| 763 | #undef ATIME |
| 764 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 767 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 768 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 769 | |
| 770 | static object * |
| 771 | posix__exit(self, args) |
| 772 | object *self; |
| 773 | object *args; |
| 774 | { |
| 775 | int sts; |
| 776 | if (!getintarg(args, &sts)) |
| 777 | return NULL; |
| 778 | _exit(sts); |
| 779 | /* NOTREACHED */ |
| 780 | } |
| 781 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 782 | #ifdef HAVE_EXECV |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 783 | static object * |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 784 | posix_execv(self, args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 785 | object *self; |
| 786 | object *args; |
| 787 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 788 | char *path; |
| 789 | object *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 790 | char **argvlist; |
| 791 | int i, argc; |
| 792 | object *(*getitem) PROTO((object *, int)); |
| 793 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 794 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 795 | argv is a list or tuple of strings. */ |
| 796 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 797 | if (!getargs(args, "(sO)", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 798 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 799 | if (is_listobject(argv)) { |
| 800 | argc = getlistsize(argv); |
| 801 | getitem = getlistitem; |
| 802 | } |
| 803 | else if (is_tupleobject(argv)) { |
| 804 | argc = gettuplesize(argv); |
| 805 | getitem = gettupleitem; |
| 806 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 807 | else { |
| 808 | badarg: |
| 809 | err_badarg(); |
| 810 | return NULL; |
| 811 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 812 | |
| 813 | argvlist = NEW(char *, argc+1); |
| 814 | if (argvlist == NULL) |
| 815 | return NULL; |
| 816 | for (i = 0; i < argc; i++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 817 | if (!getargs((*getitem)(argv, i), "s", &argvlist[i])) { |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 818 | DEL(argvlist); |
| 819 | goto badarg; |
| 820 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 821 | } |
| 822 | argvlist[argc] = NULL; |
| 823 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 824 | #ifdef BAD_EXEC_PROTOTYPES |
| 825 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 826 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 827 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 828 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 829 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 830 | /* If we get here it's definitely an error */ |
| 831 | |
| 832 | DEL(argvlist); |
| 833 | return posix_error(); |
| 834 | } |
| 835 | |
| 836 | static object * |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 837 | posix_execve(self, args) |
| 838 | object *self; |
| 839 | object *args; |
| 840 | { |
| 841 | char *path; |
| 842 | object *argv, *env; |
| 843 | char **argvlist; |
| 844 | char **envlist; |
| 845 | object *key, *val; |
| 846 | int i, pos, argc, envc; |
| 847 | object *(*getitem) PROTO((object *, int)); |
| 848 | |
| 849 | /* execve has three arguments: (path, argv, env), where |
| 850 | argv is a list or tuple of strings and env is a dictionary |
| 851 | like posix.environ. */ |
| 852 | |
| 853 | if (!getargs(args, "(sOO)", &path, &argv, &env)) |
| 854 | return NULL; |
| 855 | if (is_listobject(argv)) { |
| 856 | argc = getlistsize(argv); |
| 857 | getitem = getlistitem; |
| 858 | } |
| 859 | else if (is_tupleobject(argv)) { |
| 860 | argc = gettuplesize(argv); |
| 861 | getitem = gettupleitem; |
| 862 | } |
| 863 | else { |
| 864 | err_setstr(TypeError, "argv must be tuple or list"); |
| 865 | return NULL; |
| 866 | } |
| 867 | if (!is_dictobject(env)) { |
| 868 | err_setstr(TypeError, "env must be dictionary"); |
| 869 | return NULL; |
| 870 | } |
| 871 | |
| 872 | argvlist = NEW(char *, argc+1); |
| 873 | if (argvlist == NULL) { |
| 874 | err_nomem(); |
| 875 | return NULL; |
| 876 | } |
| 877 | for (i = 0; i < argc; i++) { |
| 878 | if (!getargs((*getitem)(argv, i), |
| 879 | "s;argv must be list of strings", |
| 880 | &argvlist[i])) { |
| 881 | goto fail_1; |
| 882 | } |
| 883 | } |
| 884 | argvlist[argc] = NULL; |
| 885 | |
| 886 | i = getmappingsize(env); |
| 887 | envlist = NEW(char *, i + 1); |
| 888 | if (envlist == NULL) { |
| 889 | err_nomem(); |
| 890 | goto fail_1; |
| 891 | } |
| 892 | pos = 0; |
| 893 | envc = 0; |
| 894 | while (mappinggetnext(env, &pos, &key, &val)) { |
| 895 | char *p, *k, *v; |
| 896 | if (!getargs(key, "s;non-string key in env", &k) || |
| 897 | !getargs(val, "s;non-string value in env", &v)) { |
| 898 | goto fail_2; |
| 899 | } |
| 900 | p = NEW(char, getstringsize(key) + getstringsize(val) + 2); |
| 901 | if (p == NULL) { |
| 902 | err_nomem(); |
| 903 | goto fail_2; |
| 904 | } |
| 905 | sprintf(p, "%s=%s", k, v); |
| 906 | envlist[envc++] = p; |
| 907 | } |
| 908 | envlist[envc] = 0; |
| 909 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 910 | |
| 911 | #ifdef BAD_EXEC_PROTOTYPES |
| 912 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 913 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 914 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 915 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 916 | |
| 917 | /* If we get here it's definitely an error */ |
| 918 | |
| 919 | (void) posix_error(); |
| 920 | |
| 921 | fail_2: |
| 922 | while (--envc >= 0) |
| 923 | DEL(envlist[envc]); |
| 924 | DEL(envlist); |
| 925 | fail_1: |
| 926 | DEL(argvlist); |
| 927 | |
| 928 | return NULL; |
| 929 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 930 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 931 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 932 | #ifdef HAVE_FORK |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 933 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 934 | posix_fork(self, args) |
| 935 | object *self; |
| 936 | object *args; |
| 937 | { |
| 938 | int pid; |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 939 | if (!getnoarg(args)) |
| 940 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 941 | pid = fork(); |
| 942 | if (pid == -1) |
| 943 | return posix_error(); |
| 944 | return newintobject((long)pid); |
| 945 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 946 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 947 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 948 | #ifdef HAVE_GETEGID |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 949 | static object * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 950 | posix_getegid(self, args) |
| 951 | object *self; |
| 952 | object *args; |
| 953 | { |
| 954 | if (!getnoarg(args)) |
| 955 | return NULL; |
| 956 | return newintobject((long)getegid()); |
| 957 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 958 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 960 | #ifdef HAVE_GETEUID |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 961 | static object * |
| 962 | posix_geteuid(self, args) |
| 963 | object *self; |
| 964 | object *args; |
| 965 | { |
| 966 | if (!getnoarg(args)) |
| 967 | return NULL; |
| 968 | return newintobject((long)geteuid()); |
| 969 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 970 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 971 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 972 | #ifdef HAVE_GETGID |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 973 | static object * |
| 974 | posix_getgid(self, args) |
| 975 | object *self; |
| 976 | object *args; |
| 977 | { |
| 978 | if (!getnoarg(args)) |
| 979 | return NULL; |
| 980 | return newintobject((long)getgid()); |
| 981 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 982 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 983 | |
| 984 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 985 | posix_getpid(self, args) |
| 986 | object *self; |
| 987 | object *args; |
| 988 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 989 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 990 | return NULL; |
| 991 | return newintobject((long)getpid()); |
| 992 | } |
| 993 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 994 | #ifdef HAVE_GETPGRP |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 995 | static object * |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 996 | posix_getpgrp(self, args) |
| 997 | object *self; |
| 998 | object *args; |
| 999 | { |
| 1000 | if (!getnoarg(args)) |
| 1001 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1002 | #ifdef GETPGRP_HAVE_ARG |
Guido van Rossum | 971443b | 1991-06-07 13:59:29 +0000 | [diff] [blame] | 1003 | return newintobject((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1004 | #else /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1005 | return newintobject((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1006 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1007 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1008 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1009 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1010 | #ifdef HAVE_SETPGRP |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1011 | static object * |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1012 | posix_setpgrp(self, args) |
| 1013 | object *self; |
| 1014 | object *args; |
| 1015 | { |
| 1016 | if (!getnoarg(args)) |
| 1017 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1018 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1019 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1020 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1021 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1022 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1023 | return posix_error(); |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1024 | INCREF(None); |
| 1025 | return None; |
| 1026 | } |
| 1027 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1028 | #endif /* HAVE_SETPGRP */ |
| 1029 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1030 | #ifdef HAVE_GETPPID |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1031 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1032 | posix_getppid(self, args) |
| 1033 | object *self; |
| 1034 | object *args; |
| 1035 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1036 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1037 | return NULL; |
| 1038 | return newintobject((long)getppid()); |
| 1039 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1040 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1041 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1042 | #ifdef HAVE_GETUID |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1043 | static object * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1044 | posix_getuid(self, args) |
| 1045 | object *self; |
| 1046 | object *args; |
| 1047 | { |
| 1048 | if (!getnoarg(args)) |
| 1049 | return NULL; |
| 1050 | return newintobject((long)getuid()); |
| 1051 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1052 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1053 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1054 | #ifdef HAVE_KILL |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1055 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1056 | posix_kill(self, args) |
| 1057 | object *self; |
| 1058 | object *args; |
| 1059 | { |
| 1060 | int pid, sig; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1061 | if (!getargs(args, "(ii)", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1062 | return NULL; |
| 1063 | if (kill(pid, sig) == -1) |
| 1064 | return posix_error(); |
| 1065 | INCREF(None); |
| 1066 | return None; |
| 1067 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1068 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1069 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1070 | #ifdef HAVE_PLOCK |
| 1071 | |
| 1072 | #ifdef HAVE_SYS_LOCK_H |
| 1073 | #include <sys/lock.h> |
| 1074 | #endif |
| 1075 | |
| 1076 | static object * |
| 1077 | posix_plock(self, args) |
| 1078 | object *self; |
| 1079 | object *args; |
| 1080 | { |
| 1081 | int op; |
| 1082 | if (!getargs(args, "i", &op)) |
| 1083 | return NULL; |
| 1084 | if (plock(op) == -1) |
| 1085 | return posix_error(); |
| 1086 | INCREF(None); |
| 1087 | return None; |
| 1088 | } |
| 1089 | #endif |
| 1090 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1091 | #ifdef HAVE_POPEN |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1092 | static object * |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1093 | posix_popen(self, args) |
| 1094 | object *self; |
| 1095 | object *args; |
| 1096 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1097 | char *name; |
| 1098 | char *mode = "r"; |
| 1099 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1100 | FILE *fp; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1101 | object *f; |
| 1102 | if (!newgetargs(args, "s|si", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1103 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1104 | BGN_SAVE |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1105 | fp = popen(name, mode); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1106 | END_SAVE |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1107 | if (fp == NULL) |
| 1108 | return posix_error(); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1109 | f = newopenfileobject(fp, name, mode, pclose); |
| 1110 | if (f != NULL) |
| 1111 | setfilebufsize(f, bufsize); |
| 1112 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1113 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1114 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1115 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1116 | #ifdef HAVE_SETUID |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1117 | static object * |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1118 | posix_setuid(self, args) |
| 1119 | object *self; |
| 1120 | object *args; |
| 1121 | { |
| 1122 | int uid; |
| 1123 | if (!getargs(args, "i", &uid)) |
| 1124 | return NULL; |
| 1125 | if (setuid(uid) < 0) |
| 1126 | return posix_error(); |
| 1127 | INCREF(None); |
| 1128 | return None; |
| 1129 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1130 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1131 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1132 | #ifdef HAVE_SETGID |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1133 | static object * |
| 1134 | posix_setgid(self, args) |
| 1135 | object *self; |
| 1136 | object *args; |
| 1137 | { |
| 1138 | int gid; |
| 1139 | if (!getargs(args, "i", &gid)) |
| 1140 | return NULL; |
| 1141 | if (setgid(gid) < 0) |
| 1142 | return posix_error(); |
| 1143 | INCREF(None); |
| 1144 | return None; |
| 1145 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1146 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1147 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1148 | #ifdef HAVE_WAITPID |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1149 | static object * |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1150 | posix_waitpid(self, args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1151 | object *self; |
| 1152 | object *args; |
| 1153 | { |
Guido van Rossum | fd03e2b | 1996-06-19 23:17:02 +0000 | [diff] [blame] | 1154 | int pid, options, sts = 0; |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1155 | if (!getargs(args, "(ii)", &pid, &options)) |
| 1156 | return NULL; |
| 1157 | BGN_SAVE |
| 1158 | pid = waitpid(pid, &sts, options); |
| 1159 | END_SAVE |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1160 | if (pid == -1) |
| 1161 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1162 | else |
| 1163 | return mkvalue("ii", pid, sts); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1164 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1165 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1166 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1167 | #ifdef HAVE_WAIT |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1168 | static object * |
| 1169 | posix_wait(self, args) |
| 1170 | object *self; |
| 1171 | object *args; |
| 1172 | { |
| 1173 | int pid, sts; |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1174 | BGN_SAVE |
| 1175 | pid = wait(&sts); |
| 1176 | END_SAVE |
| 1177 | if (pid == -1) |
| 1178 | return posix_error(); |
| 1179 | else |
| 1180 | return mkvalue("ii", pid, sts); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1181 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1182 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1183 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1184 | static object * |
| 1185 | posix_lstat(self, args) |
| 1186 | object *self; |
| 1187 | object *args; |
| 1188 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1189 | #ifdef HAVE_LSTAT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1190 | return posix_do_stat(self, args, lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1191 | #else /* !HAVE_LSTAT */ |
| 1192 | return posix_do_stat(self, args, stat); |
| 1193 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1196 | #ifdef HAVE_READLINK |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1197 | static object * |
| 1198 | posix_readlink(self, args) |
| 1199 | object *self; |
| 1200 | object *args; |
| 1201 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1202 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1203 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1204 | int n; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1205 | if (!getargs(args, "s", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1206 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1207 | BGN_SAVE |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1208 | n = readlink(path, buf, (int) sizeof buf); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1209 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1210 | if (n < 0) |
| 1211 | return posix_error(); |
| 1212 | return newsizedstringobject(buf, n); |
| 1213 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1214 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1215 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1216 | #ifdef HAVE_SYMLINK |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1217 | static object * |
| 1218 | posix_symlink(self, args) |
| 1219 | object *self; |
| 1220 | object *args; |
| 1221 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1222 | return posix_2str(args, symlink); |
| 1223 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1224 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1225 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1226 | #ifdef HAVE_TIMES |
| 1227 | #ifndef HZ |
| 1228 | #define HZ 60 /* Universal constant :-) */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1229 | #endif /* HZ */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1230 | static object * |
| 1231 | posix_times(self, args) |
| 1232 | object *self; |
| 1233 | object *args; |
| 1234 | { |
| 1235 | struct tms t; |
| 1236 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1237 | if (!getnoarg(args)) |
| 1238 | return NULL; |
| 1239 | errno = 0; |
| 1240 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1241 | if (c == (clock_t) -1) |
| 1242 | return posix_error(); |
Guido van Rossum | bbc3410 | 1995-09-13 17:39:51 +0000 | [diff] [blame] | 1243 | return mkvalue("ddddd", |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1244 | (double)t.tms_utime / HZ, |
| 1245 | (double)t.tms_stime / HZ, |
| 1246 | (double)t.tms_cutime / HZ, |
Guido van Rossum | bbc3410 | 1995-09-13 17:39:51 +0000 | [diff] [blame] | 1247 | (double)t.tms_cstime / HZ, |
| 1248 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1249 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1250 | #endif /* HAVE_TIMES */ |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 1251 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 1252 | #define HAVE_TIMES /* so the method table will pick it up */ |
| 1253 | static object * |
| 1254 | posix_times(self, args) |
| 1255 | object *self; |
| 1256 | object *args; |
| 1257 | { |
| 1258 | FILETIME create, exit, kernel, user; |
| 1259 | HANDLE hProc; |
| 1260 | if (!getnoarg(args)) |
| 1261 | return NULL; |
| 1262 | hProc = GetCurrentProcess(); |
| 1263 | GetProcessTimes(hProc,&create, &exit, &kernel, &user); |
Guido van Rossum | bbc3410 | 1995-09-13 17:39:51 +0000 | [diff] [blame] | 1264 | return mkvalue("ddddd", |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 1265 | (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime) / 2E6, |
| 1266 | (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6, |
| 1267 | (double)0, |
Guido van Rossum | bbc3410 | 1995-09-13 17:39:51 +0000 | [diff] [blame] | 1268 | (double)0, |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 1269 | (double)0); |
| 1270 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1271 | #endif /* MS_WIN32 */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1272 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1273 | #ifdef HAVE_SETSID |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1274 | static object * |
| 1275 | posix_setsid(self, args) |
| 1276 | object *self; |
| 1277 | object *args; |
| 1278 | { |
| 1279 | if (!getnoarg(args)) |
| 1280 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1281 | if (setsid() < 0) |
| 1282 | return posix_error(); |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1283 | INCREF(None); |
| 1284 | return None; |
| 1285 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1286 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1287 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1288 | #ifdef HAVE_SETPGID |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1289 | static object * |
| 1290 | posix_setpgid(self, args) |
| 1291 | object *self; |
| 1292 | object *args; |
| 1293 | { |
| 1294 | int pid, pgrp; |
| 1295 | if (!getargs(args, "(ii)", &pid, &pgrp)) |
| 1296 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1297 | if (setpgid(pid, pgrp) < 0) |
| 1298 | return posix_error(); |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1299 | INCREF(None); |
| 1300 | return None; |
| 1301 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1302 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1303 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1304 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1305 | static object * |
| 1306 | posix_tcgetpgrp(self, args) |
| 1307 | object *self; |
| 1308 | object *args; |
| 1309 | { |
| 1310 | int fd, pgid; |
| 1311 | if (!getargs(args, "i", &fd)) |
| 1312 | return NULL; |
| 1313 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1314 | if (pgid < 0) |
| 1315 | return posix_error(); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1316 | return newintobject((long)pgid); |
| 1317 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1318 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1319 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1320 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1321 | static object * |
| 1322 | posix_tcsetpgrp(self, args) |
| 1323 | object *self; |
| 1324 | object *args; |
| 1325 | { |
| 1326 | int fd, pgid; |
| 1327 | if (!getargs(args, "(ii)", &fd, &pgid)) |
| 1328 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1329 | if (tcsetpgrp(fd, pgid) < 0) |
| 1330 | return posix_error(); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1331 | INCREF(None); |
| 1332 | return None; |
| 1333 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1334 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1335 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1336 | /* Functions acting on file descriptors */ |
| 1337 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1338 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1339 | posix_open(self, args) |
| 1340 | object *self; |
| 1341 | object *args; |
| 1342 | { |
| 1343 | char *file; |
| 1344 | int flag; |
| 1345 | int mode = 0777; |
| 1346 | int fd; |
| 1347 | if (!getargs(args, "(si)", &file, &flag)) { |
| 1348 | err_clear(); |
| 1349 | if (!getargs(args, "(sii)", &file, &flag, &mode)) |
| 1350 | return NULL; |
| 1351 | } |
| 1352 | BGN_SAVE |
| 1353 | fd = open(file, flag, mode); |
| 1354 | END_SAVE |
| 1355 | if (fd < 0) |
| 1356 | return posix_error(); |
| 1357 | return newintobject((long)fd); |
| 1358 | } |
| 1359 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1360 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1361 | posix_close(self, args) |
| 1362 | object *self; |
| 1363 | object *args; |
| 1364 | { |
| 1365 | int fd, res; |
| 1366 | if (!getargs(args, "i", &fd)) |
| 1367 | return NULL; |
| 1368 | BGN_SAVE |
| 1369 | res = close(fd); |
| 1370 | END_SAVE |
| 1371 | if (res < 0) |
| 1372 | return posix_error(); |
| 1373 | INCREF(None); |
| 1374 | return None; |
| 1375 | } |
| 1376 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1377 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1378 | posix_dup(self, args) |
| 1379 | object *self; |
| 1380 | object *args; |
| 1381 | { |
| 1382 | int fd; |
| 1383 | if (!getargs(args, "i", &fd)) |
| 1384 | return NULL; |
| 1385 | BGN_SAVE |
| 1386 | fd = dup(fd); |
| 1387 | END_SAVE |
| 1388 | if (fd < 0) |
| 1389 | return posix_error(); |
| 1390 | return newintobject((long)fd); |
| 1391 | } |
| 1392 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1393 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1394 | posix_dup2(self, args) |
| 1395 | object *self; |
| 1396 | object *args; |
| 1397 | { |
| 1398 | int fd, fd2, res; |
| 1399 | if (!getargs(args, "(ii)", &fd, &fd2)) |
| 1400 | return NULL; |
| 1401 | BGN_SAVE |
| 1402 | res = dup2(fd, fd2); |
| 1403 | END_SAVE |
| 1404 | if (res < 0) |
| 1405 | return posix_error(); |
| 1406 | INCREF(None); |
| 1407 | return None; |
| 1408 | } |
| 1409 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1410 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1411 | posix_lseek(self, args) |
| 1412 | object *self; |
| 1413 | object *args; |
| 1414 | { |
| 1415 | int fd, how; |
| 1416 | long pos, res; |
| 1417 | if (!getargs(args, "(ili)", &fd, &pos, &how)) |
| 1418 | return NULL; |
| 1419 | #ifdef SEEK_SET |
| 1420 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 1421 | switch (how) { |
| 1422 | case 0: how = SEEK_SET; break; |
| 1423 | case 1: how = SEEK_CUR; break; |
| 1424 | case 2: how = SEEK_END; break; |
| 1425 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1426 | #endif /* SEEK_END */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1427 | BGN_SAVE |
| 1428 | res = lseek(fd, pos, how); |
| 1429 | END_SAVE |
| 1430 | if (res < 0) |
| 1431 | return posix_error(); |
| 1432 | return newintobject(res); |
| 1433 | } |
| 1434 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1435 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1436 | posix_read(self, args) |
| 1437 | object *self; |
| 1438 | object *args; |
| 1439 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 1440 | int fd, size, n; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1441 | object *buffer; |
| 1442 | if (!getargs(args, "(ii)", &fd, &size)) |
| 1443 | return NULL; |
| 1444 | buffer = newsizedstringobject((char *)NULL, size); |
| 1445 | if (buffer == NULL) |
| 1446 | return NULL; |
| 1447 | BGN_SAVE |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 1448 | n = read(fd, getstringvalue(buffer), size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1449 | END_SAVE |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 1450 | if (n < 0) { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1451 | DECREF(buffer); |
| 1452 | return posix_error(); |
| 1453 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 1454 | if (n != size) |
| 1455 | resizestring(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1456 | return buffer; |
| 1457 | } |
| 1458 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1459 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1460 | posix_write(self, args) |
| 1461 | object *self; |
| 1462 | object *args; |
| 1463 | { |
| 1464 | int fd, size; |
| 1465 | char *buffer; |
| 1466 | if (!getargs(args, "(is#)", &fd, &buffer, &size)) |
| 1467 | return NULL; |
| 1468 | BGN_SAVE |
| 1469 | size = write(fd, buffer, size); |
| 1470 | END_SAVE |
| 1471 | if (size < 0) |
| 1472 | return posix_error(); |
| 1473 | return newintobject((long)size); |
| 1474 | } |
| 1475 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1476 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1477 | posix_fstat(self, args) |
| 1478 | object *self; |
| 1479 | object *args; |
| 1480 | { |
| 1481 | int fd; |
| 1482 | struct stat st; |
| 1483 | int res; |
| 1484 | if (!getargs(args, "i", &fd)) |
| 1485 | return NULL; |
| 1486 | BGN_SAVE |
| 1487 | res = fstat(fd, &st); |
| 1488 | END_SAVE |
| 1489 | if (res != 0) |
| 1490 | return posix_error(); |
| 1491 | return mkvalue("(llllllllll)", |
| 1492 | (long)st.st_mode, |
| 1493 | (long)st.st_ino, |
| 1494 | (long)st.st_dev, |
| 1495 | (long)st.st_nlink, |
| 1496 | (long)st.st_uid, |
| 1497 | (long)st.st_gid, |
| 1498 | (long)st.st_size, |
| 1499 | (long)st.st_atime, |
| 1500 | (long)st.st_mtime, |
| 1501 | (long)st.st_ctime); |
| 1502 | } |
| 1503 | |
| 1504 | static object * |
| 1505 | posix_fdopen(self, args) |
| 1506 | object *self; |
| 1507 | object *args; |
| 1508 | { |
| 1509 | extern int fclose PROTO((FILE *)); |
| 1510 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1511 | char *mode = "r"; |
| 1512 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1513 | FILE *fp; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1514 | object *f; |
| 1515 | if (!newgetargs(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1516 | return NULL; |
| 1517 | BGN_SAVE |
| 1518 | fp = fdopen(fd, mode); |
| 1519 | END_SAVE |
| 1520 | if (fp == NULL) |
| 1521 | return posix_error(); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1522 | f = newopenfileobject(fp, "(fdopen)", mode, fclose); |
| 1523 | if (f != NULL) |
| 1524 | setfilebufsize(f, bufsize); |
| 1525 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1528 | #ifdef HAVE_PIPE |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1529 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1530 | posix_pipe(self, args) |
| 1531 | object *self; |
| 1532 | object *args; |
| 1533 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1534 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1535 | int fds[2]; |
| 1536 | int res; |
| 1537 | if (!getargs(args, "")) |
| 1538 | return NULL; |
| 1539 | BGN_SAVE |
| 1540 | res = pipe(fds); |
| 1541 | END_SAVE |
| 1542 | if (res != 0) |
| 1543 | return posix_error(); |
| 1544 | return mkvalue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1545 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 1546 | HANDLE read, write; |
| 1547 | BOOL ok; |
| 1548 | if (!getargs(args, "")) |
| 1549 | return NULL; |
| 1550 | BGN_SAVE |
| 1551 | ok = CreatePipe( &read, &write, NULL, 0); |
| 1552 | END_SAVE |
| 1553 | if (!ok) |
| 1554 | return posix_error(); |
| 1555 | return mkvalue("(ii)", read, write); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1556 | #endif /* MS_WIN32 */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1557 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1558 | #endif /* HAVE_PIPE */ |
| 1559 | |
| 1560 | #ifdef HAVE_MKFIFO |
| 1561 | static object * |
| 1562 | posix_mkfifo(self, args) |
| 1563 | object *self; |
| 1564 | object *args; |
| 1565 | { |
| 1566 | char *file; |
| 1567 | int mode = 0666; |
| 1568 | int res; |
| 1569 | if (!newgetargs(args, "s|i", &file, &mode)) |
| 1570 | return NULL; |
| 1571 | BGN_SAVE |
| 1572 | res = mkfifo(file, mode); |
| 1573 | END_SAVE |
| 1574 | if (res < 0) |
| 1575 | return posix_error(); |
| 1576 | INCREF(None); |
| 1577 | return None; |
| 1578 | } |
| 1579 | #endif |
| 1580 | |
| 1581 | #ifdef HAVE_FTRUNCATE |
| 1582 | static object * |
| 1583 | posix_ftruncate(self, args) |
| 1584 | object *self; /* Not used */ |
| 1585 | object *args; |
| 1586 | { |
| 1587 | int fd; |
| 1588 | long length; |
| 1589 | int res; |
| 1590 | |
| 1591 | if (!getargs(args, "(il)", &fd, &length)) |
| 1592 | return NULL; |
| 1593 | |
| 1594 | BGN_SAVE |
| 1595 | res = ftruncate(fd, length); |
| 1596 | END_SAVE |
| 1597 | if (res < 0) { |
| 1598 | err_errno(IOError); |
| 1599 | return NULL; |
| 1600 | } |
| 1601 | INCREF(None); |
| 1602 | return None; |
| 1603 | } |
| 1604 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1605 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 1606 | #ifdef HAVE_PUTENV |
| 1607 | static object * |
| 1608 | posix_putenv(self,args) |
| 1609 | object *self; |
| 1610 | object *args; |
| 1611 | { |
| 1612 | char *s1, *s2; |
| 1613 | char *new; |
| 1614 | |
| 1615 | if (!newgetargs(args, "ss", &s1, &s2)) |
| 1616 | return NULL; |
| 1617 | /* XXX This leaks memory -- not easy to fix :-( */ |
| 1618 | if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL) |
| 1619 | return err_nomem(); |
| 1620 | (void) sprintf(new, "%s=%s", s1, s2); |
| 1621 | if (putenv(new)) { |
| 1622 | posix_error(); |
| 1623 | return NULL; |
| 1624 | } |
| 1625 | INCREF(None); |
| 1626 | return None; |
| 1627 | } |
| 1628 | #endif |
| 1629 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1630 | static struct methodlist posix_methods[] = { |
| 1631 | {"chdir", posix_chdir}, |
| 1632 | {"chmod", posix_chmod}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1633 | #ifdef HAVE_CHOWN |
| 1634 | {"chown", posix_chown}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1635 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1636 | #ifdef HAVE_GETCWD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1637 | {"getcwd", posix_getcwd}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1638 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1639 | #ifdef HAVE_LINK |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1640 | {"link", posix_link}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1641 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1642 | {"listdir", posix_listdir}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1643 | {"lstat", posix_lstat}, |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1644 | {"mkdir", posix_mkdir, 1}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1645 | #ifdef HAVE_NICE |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1646 | {"nice", posix_nice}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1647 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1648 | #ifdef HAVE_READLINK |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1649 | {"readlink", posix_readlink}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1650 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1651 | {"rename", posix_rename}, |
| 1652 | {"rmdir", posix_rmdir}, |
| 1653 | {"stat", posix_stat}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1654 | #ifdef HAVE_SYMLINK |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1655 | {"symlink", posix_symlink}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1656 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1657 | #ifdef HAVE_SYSTEM |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1658 | {"system", posix_system}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1659 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1660 | {"umask", posix_umask}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1661 | #ifdef HAVE_UNAME |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1662 | {"uname", posix_uname}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1663 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1664 | {"unlink", posix_unlink}, |
Guido van Rossum | f4a5912 | 1996-02-13 00:12:56 +0000 | [diff] [blame] | 1665 | {"remove", posix_unlink}, |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1666 | {"utime", posix_utime}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1667 | #ifdef HAVE_TIMES |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1668 | {"times", posix_times}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1669 | #endif /* HAVE_TIMES */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1670 | {"_exit", posix__exit}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1671 | #ifdef HAVE_EXECV |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1672 | {"execv", posix_execv}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1673 | {"execve", posix_execve}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1674 | #endif /* HAVE_EXECV */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1675 | #ifdef HAVE_FORK |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1676 | {"fork", posix_fork}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1677 | #endif /* HAVE_FORK */ |
| 1678 | #ifdef HAVE_GETEGID |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1679 | {"getegid", posix_getegid}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1680 | #endif /* HAVE_GETEGID */ |
| 1681 | #ifdef HAVE_GETEUID |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1682 | {"geteuid", posix_geteuid}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1683 | #endif /* HAVE_GETEUID */ |
| 1684 | #ifdef HAVE_GETGID |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1685 | {"getgid", posix_getgid}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1686 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1687 | {"getpid", posix_getpid}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1688 | #ifdef HAVE_GETPGRP |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1689 | {"getpgrp", posix_getpgrp}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1690 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1691 | #ifdef HAVE_GETPPID |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1692 | {"getppid", posix_getppid}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1693 | #endif /* HAVE_GETPPID */ |
| 1694 | #ifdef HAVE_GETUID |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1695 | {"getuid", posix_getuid}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1696 | #endif /* HAVE_GETUID */ |
| 1697 | #ifdef HAVE_KILL |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1698 | {"kill", posix_kill}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1699 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1700 | #ifdef HAVE_PLOCK |
| 1701 | {"plock", posix_plock}, |
| 1702 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1703 | #ifdef HAVE_POPEN |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1704 | {"popen", posix_popen, 1}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1705 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1706 | #ifdef HAVE_SETUID |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1707 | {"setuid", posix_setuid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1708 | #endif /* HAVE_SETUID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1709 | #ifdef HAVE_SETGID |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1710 | {"setgid", posix_setgid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1711 | #endif /* HAVE_SETGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1712 | #ifdef HAVE_SETPGRP |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1713 | {"setpgrp", posix_setpgrp}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1714 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1715 | #ifdef HAVE_WAIT |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1716 | {"wait", posix_wait}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1717 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1718 | #ifdef HAVE_WAITPID |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1719 | {"waitpid", posix_waitpid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1720 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1721 | #ifdef HAVE_SETSID |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1722 | {"setsid", posix_setsid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1723 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1724 | #ifdef HAVE_SETPGID |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1725 | {"setpgid", posix_setpgid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1726 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1727 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1728 | {"tcgetpgrp", posix_tcgetpgrp}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1729 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1730 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1731 | {"tcsetpgrp", posix_tcsetpgrp}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1732 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1733 | {"open", posix_open}, |
| 1734 | {"close", posix_close}, |
| 1735 | {"dup", posix_dup}, |
| 1736 | {"dup2", posix_dup2}, |
| 1737 | {"lseek", posix_lseek}, |
| 1738 | {"read", posix_read}, |
| 1739 | {"write", posix_write}, |
| 1740 | {"fstat", posix_fstat}, |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 1741 | {"fdopen", posix_fdopen, 1}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1742 | #ifdef HAVE_PIPE |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1743 | {"pipe", posix_pipe}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1744 | #endif |
| 1745 | #ifdef HAVE_MKFIFO |
| 1746 | {"mkfifo", posix_mkfifo, 1}, |
| 1747 | #endif |
| 1748 | #ifdef HAVE_FTRUNCATE |
| 1749 | {"ftruncate", posix_ftruncate, 1}, |
| 1750 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 1751 | #ifdef HAVE_PUTENV |
| 1752 | {"putenv", posix_putenv, 1}, |
| 1753 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1754 | {NULL, NULL} /* Sentinel */ |
| 1755 | }; |
| 1756 | |
| 1757 | |
Guido van Rossum | a0e7130 | 1996-05-28 22:30:38 +0000 | [diff] [blame] | 1758 | #if defined(_MSC_VER) || defined(__WATCOMC__) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1759 | void |
| 1760 | initnt() |
| 1761 | { |
| 1762 | object *m, *d, *v; |
| 1763 | |
| 1764 | m = initmodule("nt", posix_methods); |
| 1765 | d = getmoduledict(m); |
| 1766 | |
| 1767 | /* Initialize nt.environ dictionary */ |
| 1768 | v = convertenviron(); |
| 1769 | if (v == NULL || dictinsert(d, "environ", v) != 0) |
| 1770 | fatal("can't define nt.environ"); |
| 1771 | DECREF(v); |
| 1772 | |
| 1773 | /* Initialize nt.error exception */ |
| 1774 | PosixError = newstringobject("nt.error"); |
| 1775 | if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0) |
| 1776 | fatal("can't define nt.error"); |
| 1777 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1778 | #else /* not a PC port */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1779 | void |
| 1780 | initposix() |
| 1781 | { |
| 1782 | object *m, *d, *v; |
| 1783 | |
| 1784 | m = initmodule("posix", posix_methods); |
| 1785 | d = getmoduledict(m); |
| 1786 | |
| 1787 | /* Initialize posix.environ dictionary */ |
| 1788 | v = convertenviron(); |
| 1789 | if (v == NULL || dictinsert(d, "environ", v) != 0) |
| 1790 | fatal("can't define posix.environ"); |
| 1791 | DECREF(v); |
| 1792 | |
Guido van Rossum | de4e124 | 1995-03-30 10:56:54 +0000 | [diff] [blame] | 1793 | #ifdef WNOHANG |
| 1794 | /* Export WNOHANG symbol */ |
| 1795 | v = newintobject((long)WNOHANG); |
| 1796 | if (v == NULL || dictinsert(d, "WNOHANG", v) != 0) |
| 1797 | fatal("can't define posix.WNOHANG"); |
| 1798 | DECREF(v); |
| 1799 | #endif |
| 1800 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1801 | /* Initialize posix.error exception */ |
| 1802 | PosixError = newstringobject("posix.error"); |
| 1803 | if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0) |
| 1804 | fatal("can't define posix.error"); |
| 1805 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1806 | #endif /* !_MSC_VER */ |