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