Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3 | Amsterdam, 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 | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | #include "modsupport.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 29 | #include "ceval.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 31 | #include <string.h> |
| 32 | #include <errno.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 35 | |
| 36 | #include "mytime.h" /* For clock_t on some systems */ |
| 37 | |
| 38 | #ifdef HAVE_FCNTL_H |
| 39 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 40 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 42 | #ifndef NT |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 43 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 44 | #include <unistd.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 45 | #else /* !HAVE_UNISTD_H */ |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 46 | extern int mkdir PROTO((const char *, mode_t)); |
| 47 | extern int chdir PROTO((const char *)); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 48 | extern int rmdir PROTO((const char *)); |
| 49 | extern int chmod PROTO((const char *, mode_t)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 50 | extern int chown PROTO((const char *, uid_t, gid_t)); |
| 51 | extern char *getcwd PROTO((char *, int)); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 52 | extern char *strerror PROTO((int)); |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 53 | extern int link PROTO((const char *, const char *)); |
| 54 | extern int rename PROTO((const char *, const char *)); |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 55 | extern int stat PROTO((const char *, struct stat *)); |
| 56 | extern int unlink PROTO((const char *)); |
| 57 | extern int pclose PROTO((FILE *)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 58 | #ifdef HAVE_SYMLINK |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 59 | extern int symlink PROTO((const char *, const char *)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 60 | #endif /_ HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | #ifdef HAVE_LSTAT |
| 62 | extern int lstat PROTO((const char *, struct stat *)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 63 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 65 | #endif /* !NT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 67 | #ifndef NT |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | /* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */ |
| 69 | extern int rename(); |
| 70 | extern int pclose(); |
| 71 | extern int lstat(); |
| 72 | extern int symlink(); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 73 | #endif /* !NT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | |
| 75 | #ifdef HAVE_UTIME_H |
| 76 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 77 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | |
| 79 | #ifdef HAVE_SYS_TIMES_H |
| 80 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 81 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | |
| 83 | #ifdef HAVE_SYS_PARAM_H |
| 84 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 85 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 86 | |
| 87 | #ifdef HAVE_SYS_UTSNAME_H |
| 88 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 89 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 90 | |
| 91 | #ifndef MAXPATHLEN |
| 92 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 93 | #endif /* MAXPATHLEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 94 | |
| 95 | /* unistd.h defines _POSIX_VERSION on POSIX.1 systems. */ |
| 96 | #if defined(DIRENT) || defined(_POSIX_VERSION) |
| 97 | #include <dirent.h> |
| 98 | #define NLENGTH(dirent) (strlen((dirent)->d_name)) |
| 99 | #else /* not (DIRENT or _POSIX_VERSION) */ |
| 100 | #define dirent direct |
| 101 | #define NLENGTH(dirent) ((dirent)->d_namlen) |
| 102 | #ifdef SYSNDIR |
| 103 | #include <sys/ndir.h> |
| 104 | #endif /* SYSNDIR */ |
| 105 | #ifdef SYSDIR |
| 106 | #include <sys/dir.h> |
| 107 | #endif /* SYSDIR */ |
| 108 | #ifdef NDIR |
| 109 | #include <ndir.h> |
| 110 | #endif /* NDIR */ |
| 111 | #endif /* not (DIRENT or _POSIX_VERSION) */ |
| 112 | |
| 113 | #ifdef NT |
| 114 | #include <direct.h> |
| 115 | #include <io.h> |
| 116 | #include <process.h> |
| 117 | #include <windows.h> |
| 118 | #define popen _popen |
| 119 | #endif /* NT */ |
| 120 | |
| 121 | #ifdef OS2 |
| 122 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 123 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | |
| 125 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 126 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 127 | #ifndef NT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | extern char **environ; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 129 | #endif /* !NT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | |
| 131 | static object * |
| 132 | convertenviron() |
| 133 | { |
| 134 | object *d; |
| 135 | char **e; |
| 136 | d = newdictobject(); |
| 137 | if (d == NULL) |
| 138 | return NULL; |
| 139 | if (environ == NULL) |
| 140 | return d; |
| 141 | /* XXX This part ignores errors */ |
| 142 | for (e = environ; *e != NULL; e++) { |
| 143 | object *v; |
| 144 | char *p = strchr(*e, '='); |
| 145 | if (p == NULL) |
| 146 | continue; |
| 147 | v = newstringobject(p+1); |
| 148 | if (v == NULL) |
| 149 | continue; |
| 150 | *p = '\0'; |
| 151 | (void) dictinsert(d, *e, v); |
| 152 | *p = '='; |
| 153 | DECREF(v); |
| 154 | } |
| 155 | return d; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | static object *PosixError; /* Exception posix.error */ |
| 160 | |
| 161 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 162 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 163 | static object * posix_error() { return err_errno(PosixError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
| 167 | /* POSIX generic methods */ |
| 168 | |
| 169 | static object * |
| 170 | posix_1str(args, func) |
| 171 | object *args; |
| 172 | int (*func) FPROTO((const char *)); |
| 173 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 174 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 175 | int res; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 176 | if (!getargs(args, "s", &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 178 | BGN_SAVE |
| 179 | res = (*func)(path1); |
| 180 | END_SAVE |
| 181 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | return posix_error(); |
| 183 | INCREF(None); |
| 184 | return None; |
| 185 | } |
| 186 | |
| 187 | static object * |
| 188 | posix_2str(args, func) |
| 189 | object *args; |
| 190 | int (*func) FPROTO((const char *, const char *)); |
| 191 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 192 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 193 | int res; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 194 | if (!getargs(args, "(ss)", &path1, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 196 | BGN_SAVE |
| 197 | res = (*func)(path1, path2); |
| 198 | END_SAVE |
| 199 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 200 | return posix_error(); |
| 201 | INCREF(None); |
| 202 | return None; |
| 203 | } |
| 204 | |
| 205 | static object * |
| 206 | posix_strint(args, func) |
| 207 | object *args; |
| 208 | int (*func) FPROTO((const char *, int)); |
| 209 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 210 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | int i; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 212 | int res; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 213 | if (!getargs(args, "(si)", &path, &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 215 | BGN_SAVE |
| 216 | res = (*func)(path, i); |
| 217 | END_SAVE |
| 218 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | return posix_error(); |
| 220 | INCREF(None); |
| 221 | return None; |
| 222 | } |
| 223 | |
| 224 | static object * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | posix_strintint(args, func) |
| 226 | object *args; |
| 227 | int (*func) FPROTO((const char *, int, int)); |
| 228 | { |
| 229 | char *path; |
| 230 | int i,i2; |
| 231 | int res; |
| 232 | if (!getargs(args, "(sii)", &path, &i, &i2)) |
| 233 | return NULL; |
| 234 | BGN_SAVE |
| 235 | res = (*func)(path, i, i2); |
| 236 | END_SAVE |
| 237 | if (res < 0) |
| 238 | return posix_error(); |
| 239 | INCREF(None); |
| 240 | return None; |
| 241 | } |
| 242 | |
| 243 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 244 | posix_do_stat(self, args, statfunc) |
| 245 | object *self; |
| 246 | object *args; |
| 247 | int (*statfunc) FPROTO((const char *, struct stat *)); |
| 248 | { |
| 249 | struct stat st; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 250 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 251 | int res; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 252 | if (!getargs(args, "s", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 253 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 254 | BGN_SAVE |
| 255 | res = (*statfunc)(path, &st); |
| 256 | END_SAVE |
| 257 | if (res != 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 258 | return posix_error(); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 259 | return mkvalue("(llllllllll)", |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 260 | (long)st.st_mode, |
| 261 | (long)st.st_ino, |
| 262 | (long)st.st_dev, |
| 263 | (long)st.st_nlink, |
| 264 | (long)st.st_uid, |
| 265 | (long)st.st_gid, |
| 266 | (long)st.st_size, |
| 267 | (long)st.st_atime, |
| 268 | (long)st.st_mtime, |
| 269 | (long)st.st_ctime); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
| 273 | /* POSIX methods */ |
| 274 | |
| 275 | static object * |
| 276 | posix_chdir(self, args) |
| 277 | object *self; |
| 278 | object *args; |
| 279 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 280 | return posix_1str(args, chdir); |
| 281 | } |
| 282 | |
| 283 | static object * |
| 284 | posix_chmod(self, args) |
| 285 | object *self; |
| 286 | object *args; |
| 287 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 288 | return posix_strint(args, chmod); |
| 289 | } |
| 290 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 291 | #ifdef HAVE_CHOWN |
| 292 | static object * |
| 293 | posix_chown(self, args) |
| 294 | object *self; |
| 295 | object *args; |
| 296 | { |
| 297 | return posix_strintint(args, chown); |
| 298 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 299 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | static object * |
| 302 | posix_getcwd(self, args) |
| 303 | object *self; |
| 304 | object *args; |
| 305 | { |
| 306 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 307 | char *res; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | if (!getnoarg(args)) |
| 309 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 310 | BGN_SAVE |
| 311 | res = getcwd(buf, sizeof buf); |
| 312 | END_SAVE |
| 313 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 314 | return posix_error(); |
| 315 | return newstringobject(buf); |
| 316 | } |
| 317 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 318 | #ifdef HAVE_LINK |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | static object * |
| 320 | posix_link(self, args) |
| 321 | object *self; |
| 322 | object *args; |
| 323 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 324 | return posix_2str(args, link); |
| 325 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 326 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 328 | static object * |
| 329 | posix_listdir(self, args) |
| 330 | object *self; |
| 331 | object *args; |
| 332 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 333 | #ifdef NT |
| 334 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 335 | char *name; |
| 336 | int len; |
| 337 | object *d, *v; |
| 338 | HANDLE hFindFile; |
| 339 | WIN32_FIND_DATA FileData; |
| 340 | char namebuf[MAX_PATH+5]; |
| 341 | |
| 342 | if (!getargs(args, "s#", &name, &len)) |
| 343 | return NULL; |
| 344 | if (len >= MAX_PATH) { |
| 345 | err_setstr(ValueError, "path too long"); |
| 346 | return NULL; |
| 347 | } |
| 348 | strcpy(namebuf, name); |
| 349 | if (namebuf[len-1] != '/' && namebuf[len-1] != '\\') |
| 350 | namebuf[len++] = '/'; |
| 351 | strcpy(namebuf + len, "*.*"); |
| 352 | |
| 353 | if ((d = newlistobject(0)) == NULL) |
| 354 | return NULL; |
| 355 | |
| 356 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 357 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 358 | errno = GetLastError(); |
| 359 | return posix_error(); |
| 360 | } |
| 361 | do { |
| 362 | v = newstringobject(FileData.cFileName); |
| 363 | if (v == NULL) { |
| 364 | DECREF(d); |
| 365 | d = NULL; |
| 366 | break; |
| 367 | } |
| 368 | if (addlistitem(d, v) != 0) { |
| 369 | DECREF(v); |
| 370 | DECREF(d); |
| 371 | d = NULL; |
| 372 | break; |
| 373 | } |
| 374 | DECREF(v); |
| 375 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 376 | |
| 377 | if (FindClose(hFindFile) == FALSE) { |
| 378 | errno = GetLastError(); |
| 379 | return posix_error(); |
| 380 | } |
| 381 | |
| 382 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 383 | |
| 384 | #else /* !NT */ |
| 385 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 386 | char *name; |
| 387 | object *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 388 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 389 | struct dirent *ep; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 390 | if (!getargs(args, "s", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 391 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 392 | BGN_SAVE |
| 393 | if ((dirp = opendir(name)) == NULL) { |
| 394 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 395 | return posix_error(); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 396 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 397 | if ((d = newlistobject(0)) == NULL) { |
| 398 | closedir(dirp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 399 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | return NULL; |
| 401 | } |
| 402 | while ((ep = readdir(dirp)) != NULL) { |
| 403 | v = newstringobject(ep->d_name); |
| 404 | if (v == NULL) { |
| 405 | DECREF(d); |
| 406 | d = NULL; |
| 407 | break; |
| 408 | } |
| 409 | if (addlistitem(d, v) != 0) { |
| 410 | DECREF(v); |
| 411 | DECREF(d); |
| 412 | d = NULL; |
| 413 | break; |
| 414 | } |
| 415 | DECREF(v); |
| 416 | } |
| 417 | closedir(dirp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 418 | END_SAVE |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 420 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 421 | |
| 422 | #endif /* !NT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static object * |
| 426 | posix_mkdir(self, args) |
| 427 | object *self; |
| 428 | object *args; |
| 429 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 430 | return posix_strint(args, mkdir); |
| 431 | } |
| 432 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 433 | #ifdef HAVE_NICE |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 434 | static object * |
| 435 | posix_nice(self, args) |
| 436 | object *self; |
| 437 | object *args; |
| 438 | { |
| 439 | int increment, value; |
| 440 | |
| 441 | if (!getargs(args, "i", &increment)) |
| 442 | return NULL; |
| 443 | value = nice(increment); |
| 444 | if (value == -1) |
| 445 | return posix_error(); |
| 446 | return newintobject((long) value); |
| 447 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 448 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 449 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 450 | static object * |
| 451 | posix_rename(self, args) |
| 452 | object *self; |
| 453 | object *args; |
| 454 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 455 | return posix_2str(args, rename); |
| 456 | } |
| 457 | |
| 458 | static object * |
| 459 | posix_rmdir(self, args) |
| 460 | object *self; |
| 461 | object *args; |
| 462 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 463 | return posix_1str(args, rmdir); |
| 464 | } |
| 465 | |
| 466 | static object * |
| 467 | posix_stat(self, args) |
| 468 | object *self; |
| 469 | object *args; |
| 470 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 471 | return posix_do_stat(self, args, stat); |
| 472 | } |
| 473 | |
| 474 | static object * |
| 475 | posix_system(self, args) |
| 476 | object *self; |
| 477 | object *args; |
| 478 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 479 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 480 | long sts; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 481 | if (!getargs(args, "s", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 482 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 483 | BGN_SAVE |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 484 | sts = system(command); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 485 | END_SAVE |
| 486 | return newintobject(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static object * |
| 490 | posix_umask(self, args) |
| 491 | object *self; |
| 492 | object *args; |
| 493 | { |
| 494 | int i; |
| 495 | if (!getintarg(args, &i)) |
| 496 | return NULL; |
| 497 | i = umask(i); |
| 498 | if (i < 0) |
| 499 | return posix_error(); |
| 500 | return newintobject((long)i); |
| 501 | } |
| 502 | |
| 503 | static object * |
| 504 | posix_unlink(self, args) |
| 505 | object *self; |
| 506 | object *args; |
| 507 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 508 | return posix_1str(args, unlink); |
| 509 | } |
| 510 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 511 | #ifdef HAVE_UNAME |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 512 | static object * |
| 513 | posix_uname(self, args) |
| 514 | object *self; |
| 515 | object *args; |
| 516 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 517 | struct utsname u; |
| 518 | object *v; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 519 | int res; |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 520 | if (!getnoarg(args)) |
| 521 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 522 | BGN_SAVE |
| 523 | res = uname(&u); |
| 524 | END_SAVE |
| 525 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 526 | return posix_error(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 527 | return mkvalue("(sssss)", |
| 528 | u.sysname, |
| 529 | u.nodename, |
| 530 | u.release, |
| 531 | u.version, |
| 532 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 533 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 534 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 535 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 536 | static object * |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 537 | posix_utime(self, args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 538 | object *self; |
| 539 | object *args; |
| 540 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 541 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 542 | int res; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 543 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 544 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 545 | struct utimbuf buf; |
| 546 | #define ATIME buf.actime |
| 547 | #define MTIME buf.modtime |
| 548 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 549 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 550 | time_t buf[2]; |
| 551 | #define ATIME buf[0] |
| 552 | #define MTIME buf[1] |
| 553 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 554 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 555 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 556 | if (!getargs(args, "(s(ll))", &path, &ATIME, &MTIME)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 557 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 558 | BGN_SAVE |
| 559 | res = utime(path, UTIME_ARG); |
| 560 | END_SAVE |
| 561 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 562 | return posix_error(); |
| 563 | INCREF(None); |
| 564 | return None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 565 | #undef UTIME_ARG |
| 566 | #undef ATIME |
| 567 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 570 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 571 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 572 | |
| 573 | static object * |
| 574 | posix__exit(self, args) |
| 575 | object *self; |
| 576 | object *args; |
| 577 | { |
| 578 | int sts; |
| 579 | if (!getintarg(args, &sts)) |
| 580 | return NULL; |
| 581 | _exit(sts); |
| 582 | /* NOTREACHED */ |
| 583 | } |
| 584 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 585 | static object * |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 586 | posix_execv(self, args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 587 | object *self; |
| 588 | object *args; |
| 589 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 590 | char *path; |
| 591 | object *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 592 | char **argvlist; |
| 593 | int i, argc; |
| 594 | object *(*getitem) PROTO((object *, int)); |
| 595 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 596 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 597 | argv is a list or tuple of strings. */ |
| 598 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 599 | if (!getargs(args, "(sO)", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 600 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 601 | if (is_listobject(argv)) { |
| 602 | argc = getlistsize(argv); |
| 603 | getitem = getlistitem; |
| 604 | } |
| 605 | else if (is_tupleobject(argv)) { |
| 606 | argc = gettuplesize(argv); |
| 607 | getitem = gettupleitem; |
| 608 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 609 | else { |
| 610 | badarg: |
| 611 | err_badarg(); |
| 612 | return NULL; |
| 613 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 614 | |
| 615 | argvlist = NEW(char *, argc+1); |
| 616 | if (argvlist == NULL) |
| 617 | return NULL; |
| 618 | for (i = 0; i < argc; i++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 619 | if (!getargs((*getitem)(argv, i), "s", &argvlist[i])) { |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 620 | DEL(argvlist); |
| 621 | goto badarg; |
| 622 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 623 | } |
| 624 | argvlist[argc] = NULL; |
| 625 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 626 | #ifdef BAD_EXEC_PROTOTYPES |
| 627 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 628 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 629 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 630 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 632 | /* If we get here it's definitely an error */ |
| 633 | |
| 634 | DEL(argvlist); |
| 635 | return posix_error(); |
| 636 | } |
| 637 | |
| 638 | static object * |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 639 | posix_execve(self, args) |
| 640 | object *self; |
| 641 | object *args; |
| 642 | { |
| 643 | char *path; |
| 644 | object *argv, *env; |
| 645 | char **argvlist; |
| 646 | char **envlist; |
| 647 | object *key, *val; |
| 648 | int i, pos, argc, envc; |
| 649 | object *(*getitem) PROTO((object *, int)); |
| 650 | |
| 651 | /* execve has three arguments: (path, argv, env), where |
| 652 | argv is a list or tuple of strings and env is a dictionary |
| 653 | like posix.environ. */ |
| 654 | |
| 655 | if (!getargs(args, "(sOO)", &path, &argv, &env)) |
| 656 | return NULL; |
| 657 | if (is_listobject(argv)) { |
| 658 | argc = getlistsize(argv); |
| 659 | getitem = getlistitem; |
| 660 | } |
| 661 | else if (is_tupleobject(argv)) { |
| 662 | argc = gettuplesize(argv); |
| 663 | getitem = gettupleitem; |
| 664 | } |
| 665 | else { |
| 666 | err_setstr(TypeError, "argv must be tuple or list"); |
| 667 | return NULL; |
| 668 | } |
| 669 | if (!is_dictobject(env)) { |
| 670 | err_setstr(TypeError, "env must be dictionary"); |
| 671 | return NULL; |
| 672 | } |
| 673 | |
| 674 | argvlist = NEW(char *, argc+1); |
| 675 | if (argvlist == NULL) { |
| 676 | err_nomem(); |
| 677 | return NULL; |
| 678 | } |
| 679 | for (i = 0; i < argc; i++) { |
| 680 | if (!getargs((*getitem)(argv, i), |
| 681 | "s;argv must be list of strings", |
| 682 | &argvlist[i])) { |
| 683 | goto fail_1; |
| 684 | } |
| 685 | } |
| 686 | argvlist[argc] = NULL; |
| 687 | |
| 688 | i = getmappingsize(env); |
| 689 | envlist = NEW(char *, i + 1); |
| 690 | if (envlist == NULL) { |
| 691 | err_nomem(); |
| 692 | goto fail_1; |
| 693 | } |
| 694 | pos = 0; |
| 695 | envc = 0; |
| 696 | while (mappinggetnext(env, &pos, &key, &val)) { |
| 697 | char *p, *k, *v; |
| 698 | if (!getargs(key, "s;non-string key in env", &k) || |
| 699 | !getargs(val, "s;non-string value in env", &v)) { |
| 700 | goto fail_2; |
| 701 | } |
| 702 | p = NEW(char, getstringsize(key) + getstringsize(val) + 2); |
| 703 | if (p == NULL) { |
| 704 | err_nomem(); |
| 705 | goto fail_2; |
| 706 | } |
| 707 | sprintf(p, "%s=%s", k, v); |
| 708 | envlist[envc++] = p; |
| 709 | } |
| 710 | envlist[envc] = 0; |
| 711 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 712 | |
| 713 | #ifdef BAD_EXEC_PROTOTYPES |
| 714 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 715 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 716 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 717 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 718 | |
| 719 | /* If we get here it's definitely an error */ |
| 720 | |
| 721 | (void) posix_error(); |
| 722 | |
| 723 | fail_2: |
| 724 | while (--envc >= 0) |
| 725 | DEL(envlist[envc]); |
| 726 | DEL(envlist); |
| 727 | fail_1: |
| 728 | DEL(argvlist); |
| 729 | |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 734 | posix_fork(self, args) |
| 735 | object *self; |
| 736 | object *args; |
| 737 | { |
| 738 | int pid; |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 739 | if (!getnoarg(args)) |
| 740 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 741 | pid = fork(); |
| 742 | if (pid == -1) |
| 743 | return posix_error(); |
| 744 | return newintobject((long)pid); |
| 745 | } |
| 746 | |
| 747 | static object * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 748 | posix_getegid(self, args) |
| 749 | object *self; |
| 750 | object *args; |
| 751 | { |
| 752 | if (!getnoarg(args)) |
| 753 | return NULL; |
| 754 | return newintobject((long)getegid()); |
| 755 | } |
| 756 | |
| 757 | static object * |
| 758 | posix_geteuid(self, args) |
| 759 | object *self; |
| 760 | object *args; |
| 761 | { |
| 762 | if (!getnoarg(args)) |
| 763 | return NULL; |
| 764 | return newintobject((long)geteuid()); |
| 765 | } |
| 766 | |
| 767 | static object * |
| 768 | posix_getgid(self, args) |
| 769 | object *self; |
| 770 | object *args; |
| 771 | { |
| 772 | if (!getnoarg(args)) |
| 773 | return NULL; |
| 774 | return newintobject((long)getgid()); |
| 775 | } |
| 776 | |
| 777 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 778 | posix_getpid(self, args) |
| 779 | object *self; |
| 780 | object *args; |
| 781 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 782 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 783 | return NULL; |
| 784 | return newintobject((long)getpid()); |
| 785 | } |
| 786 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 787 | #ifdef HAVE_GETPGRP |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 788 | static object * |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 789 | posix_getpgrp(self, args) |
| 790 | object *self; |
| 791 | object *args; |
| 792 | { |
| 793 | if (!getnoarg(args)) |
| 794 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 795 | #ifdef GETPGRP_HAVE_ARG |
Guido van Rossum | 971443b | 1991-06-07 13:59:29 +0000 | [diff] [blame] | 796 | return newintobject((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 797 | #else /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 798 | return newintobject((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 799 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 800 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 801 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 802 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 803 | #ifdef HAVE_SETPGRP |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 804 | static object * |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 805 | posix_setpgrp(self, args) |
| 806 | object *self; |
| 807 | object *args; |
| 808 | { |
| 809 | if (!getnoarg(args)) |
| 810 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 811 | #ifdef GETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 812 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 813 | #else /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 814 | if (setpgrp() < 0) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 815 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 816 | return posix_error(); |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 817 | INCREF(None); |
| 818 | return None; |
| 819 | } |
| 820 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 821 | #endif /* HAVE_SETPGRP */ |
| 822 | |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 823 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 824 | posix_getppid(self, args) |
| 825 | object *self; |
| 826 | object *args; |
| 827 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 828 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 829 | return NULL; |
| 830 | return newintobject((long)getppid()); |
| 831 | } |
| 832 | |
| 833 | static object * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 834 | posix_getuid(self, args) |
| 835 | object *self; |
| 836 | object *args; |
| 837 | { |
| 838 | if (!getnoarg(args)) |
| 839 | return NULL; |
| 840 | return newintobject((long)getuid()); |
| 841 | } |
| 842 | |
| 843 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 844 | posix_kill(self, args) |
| 845 | object *self; |
| 846 | object *args; |
| 847 | { |
| 848 | int pid, sig; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 849 | if (!getargs(args, "(ii)", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 850 | return NULL; |
| 851 | if (kill(pid, sig) == -1) |
| 852 | return posix_error(); |
| 853 | INCREF(None); |
| 854 | return None; |
| 855 | } |
| 856 | |
| 857 | static object * |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 858 | posix_popen(self, args) |
| 859 | object *self; |
| 860 | object *args; |
| 861 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 862 | char *name, *mode; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 863 | FILE *fp; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 864 | if (!getargs(args, "(ss)", &name, &mode)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 865 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 866 | BGN_SAVE |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 867 | fp = popen(name, mode); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 868 | END_SAVE |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 869 | if (fp == NULL) |
| 870 | return posix_error(); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 871 | return newopenfileobject(fp, name, mode, pclose); |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 874 | #ifdef HAVE_SETUID |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 875 | static object * |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 876 | posix_setuid(self, args) |
| 877 | object *self; |
| 878 | object *args; |
| 879 | { |
| 880 | int uid; |
| 881 | if (!getargs(args, "i", &uid)) |
| 882 | return NULL; |
| 883 | if (setuid(uid) < 0) |
| 884 | return posix_error(); |
| 885 | INCREF(None); |
| 886 | return None; |
| 887 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 888 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 889 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 890 | #ifdef HAVE_SETGID |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 891 | static object * |
| 892 | posix_setgid(self, args) |
| 893 | object *self; |
| 894 | object *args; |
| 895 | { |
| 896 | int gid; |
| 897 | if (!getargs(args, "i", &gid)) |
| 898 | return NULL; |
| 899 | if (setgid(gid) < 0) |
| 900 | return posix_error(); |
| 901 | INCREF(None); |
| 902 | return None; |
| 903 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 904 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 905 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 906 | #ifdef HAVE_WAITPID |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 907 | static object * |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 908 | posix_waitpid(self, args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 909 | object *self; |
| 910 | object *args; |
| 911 | { |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 912 | int pid, options, sts; |
| 913 | if (!getargs(args, "(ii)", &pid, &options)) |
| 914 | return NULL; |
| 915 | BGN_SAVE |
| 916 | pid = waitpid(pid, &sts, options); |
| 917 | END_SAVE |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 918 | if (pid == -1) |
| 919 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 920 | else |
| 921 | return mkvalue("ii", pid, sts); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 922 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 923 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 924 | |
| 925 | static object * |
| 926 | posix_wait(self, args) |
| 927 | object *self; |
| 928 | object *args; |
| 929 | { |
| 930 | int pid, sts; |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 931 | BGN_SAVE |
| 932 | pid = wait(&sts); |
| 933 | END_SAVE |
| 934 | if (pid == -1) |
| 935 | return posix_error(); |
| 936 | else |
| 937 | return mkvalue("ii", pid, sts); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 940 | static object * |
| 941 | posix_lstat(self, args) |
| 942 | object *self; |
| 943 | object *args; |
| 944 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 945 | #ifdef HAVE_LSTAT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 946 | return posix_do_stat(self, args, lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 947 | #else /* !HAVE_LSTAT */ |
| 948 | return posix_do_stat(self, args, stat); |
| 949 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 952 | #ifdef HAVE_READLINK |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 953 | static object * |
| 954 | posix_readlink(self, args) |
| 955 | object *self; |
| 956 | object *args; |
| 957 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 958 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 959 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 960 | int n; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 961 | if (!getargs(args, "s", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 962 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 963 | BGN_SAVE |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 964 | n = readlink(path, buf, (int) sizeof buf); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 965 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 966 | if (n < 0) |
| 967 | return posix_error(); |
| 968 | return newsizedstringobject(buf, n); |
| 969 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 970 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 971 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 972 | #ifdef HAVE_SYMLINK |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 973 | static object * |
| 974 | posix_symlink(self, args) |
| 975 | object *self; |
| 976 | object *args; |
| 977 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 978 | return posix_2str(args, symlink); |
| 979 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 980 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 981 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 982 | #ifdef HAVE_TIMES |
| 983 | #ifndef HZ |
| 984 | #define HZ 60 /* Universal constant :-) */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 985 | #endif /* HZ */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 986 | static object * |
| 987 | posix_times(self, args) |
| 988 | object *self; |
| 989 | object *args; |
| 990 | { |
| 991 | struct tms t; |
| 992 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 993 | if (!getnoarg(args)) |
| 994 | return NULL; |
| 995 | errno = 0; |
| 996 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 997 | if (c == (clock_t) -1) |
| 998 | return posix_error(); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 999 | return mkvalue("dddd", |
| 1000 | (double)t.tms_utime / HZ, |
| 1001 | (double)t.tms_stime / HZ, |
| 1002 | (double)t.tms_cutime / HZ, |
| 1003 | (double)t.tms_cstime / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1004 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1005 | #endif /* HAVE_TIMES */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1006 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1007 | #ifdef HAVE_SETSID |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1008 | static object * |
| 1009 | posix_setsid(self, args) |
| 1010 | object *self; |
| 1011 | object *args; |
| 1012 | { |
| 1013 | if (!getnoarg(args)) |
| 1014 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1015 | if (setsid() < 0) |
| 1016 | return posix_error(); |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1017 | INCREF(None); |
| 1018 | return None; |
| 1019 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1020 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1021 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1022 | #ifdef HAVE_SETPGID |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1023 | static object * |
| 1024 | posix_setpgid(self, args) |
| 1025 | object *self; |
| 1026 | object *args; |
| 1027 | { |
| 1028 | int pid, pgrp; |
| 1029 | if (!getargs(args, "(ii)", &pid, &pgrp)) |
| 1030 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1031 | if (setpgid(pid, pgrp) < 0) |
| 1032 | return posix_error(); |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1033 | INCREF(None); |
| 1034 | return None; |
| 1035 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1036 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1037 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1038 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1039 | static object * |
| 1040 | posix_tcgetpgrp(self, args) |
| 1041 | object *self; |
| 1042 | object *args; |
| 1043 | { |
| 1044 | int fd, pgid; |
| 1045 | if (!getargs(args, "i", &fd)) |
| 1046 | return NULL; |
| 1047 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1048 | if (pgid < 0) |
| 1049 | return posix_error(); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1050 | return newintobject((long)pgid); |
| 1051 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1052 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1053 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1054 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1055 | static object * |
| 1056 | posix_tcsetpgrp(self, args) |
| 1057 | object *self; |
| 1058 | object *args; |
| 1059 | { |
| 1060 | int fd, pgid; |
| 1061 | if (!getargs(args, "(ii)", &fd, &pgid)) |
| 1062 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1063 | if (tcsetpgrp(fd, pgid) < 0) |
| 1064 | return posix_error(); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1065 | INCREF(None); |
| 1066 | return None; |
| 1067 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1068 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1069 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1070 | /* Functions acting on file descriptors */ |
| 1071 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1072 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1073 | posix_open(self, args) |
| 1074 | object *self; |
| 1075 | object *args; |
| 1076 | { |
| 1077 | char *file; |
| 1078 | int flag; |
| 1079 | int mode = 0777; |
| 1080 | int fd; |
| 1081 | if (!getargs(args, "(si)", &file, &flag)) { |
| 1082 | err_clear(); |
| 1083 | if (!getargs(args, "(sii)", &file, &flag, &mode)) |
| 1084 | return NULL; |
| 1085 | } |
| 1086 | BGN_SAVE |
| 1087 | fd = open(file, flag, mode); |
| 1088 | END_SAVE |
| 1089 | if (fd < 0) |
| 1090 | return posix_error(); |
| 1091 | return newintobject((long)fd); |
| 1092 | } |
| 1093 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1094 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1095 | posix_close(self, args) |
| 1096 | object *self; |
| 1097 | object *args; |
| 1098 | { |
| 1099 | int fd, res; |
| 1100 | if (!getargs(args, "i", &fd)) |
| 1101 | return NULL; |
| 1102 | BGN_SAVE |
| 1103 | res = close(fd); |
| 1104 | END_SAVE |
| 1105 | if (res < 0) |
| 1106 | return posix_error(); |
| 1107 | INCREF(None); |
| 1108 | return None; |
| 1109 | } |
| 1110 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1111 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1112 | posix_dup(self, args) |
| 1113 | object *self; |
| 1114 | object *args; |
| 1115 | { |
| 1116 | int fd; |
| 1117 | if (!getargs(args, "i", &fd)) |
| 1118 | return NULL; |
| 1119 | BGN_SAVE |
| 1120 | fd = dup(fd); |
| 1121 | END_SAVE |
| 1122 | if (fd < 0) |
| 1123 | return posix_error(); |
| 1124 | return newintobject((long)fd); |
| 1125 | } |
| 1126 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1127 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1128 | posix_dup2(self, args) |
| 1129 | object *self; |
| 1130 | object *args; |
| 1131 | { |
| 1132 | int fd, fd2, res; |
| 1133 | if (!getargs(args, "(ii)", &fd, &fd2)) |
| 1134 | return NULL; |
| 1135 | BGN_SAVE |
| 1136 | res = dup2(fd, fd2); |
| 1137 | END_SAVE |
| 1138 | if (res < 0) |
| 1139 | return posix_error(); |
| 1140 | INCREF(None); |
| 1141 | return None; |
| 1142 | } |
| 1143 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1144 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1145 | posix_lseek(self, args) |
| 1146 | object *self; |
| 1147 | object *args; |
| 1148 | { |
| 1149 | int fd, how; |
| 1150 | long pos, res; |
| 1151 | if (!getargs(args, "(ili)", &fd, &pos, &how)) |
| 1152 | return NULL; |
| 1153 | #ifdef SEEK_SET |
| 1154 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 1155 | switch (how) { |
| 1156 | case 0: how = SEEK_SET; break; |
| 1157 | case 1: how = SEEK_CUR; break; |
| 1158 | case 2: how = SEEK_END; break; |
| 1159 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1160 | #endif /* SEEK_END */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1161 | BGN_SAVE |
| 1162 | res = lseek(fd, pos, how); |
| 1163 | END_SAVE |
| 1164 | if (res < 0) |
| 1165 | return posix_error(); |
| 1166 | return newintobject(res); |
| 1167 | } |
| 1168 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1169 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1170 | posix_read(self, args) |
| 1171 | object *self; |
| 1172 | object *args; |
| 1173 | { |
| 1174 | int fd, size; |
| 1175 | object *buffer; |
| 1176 | if (!getargs(args, "(ii)", &fd, &size)) |
| 1177 | return NULL; |
| 1178 | buffer = newsizedstringobject((char *)NULL, size); |
| 1179 | if (buffer == NULL) |
| 1180 | return NULL; |
| 1181 | BGN_SAVE |
| 1182 | size = read(fd, getstringvalue(buffer), size); |
| 1183 | END_SAVE |
| 1184 | if (size < 0) { |
| 1185 | DECREF(buffer); |
| 1186 | return posix_error(); |
| 1187 | } |
| 1188 | resizestring(&buffer, size); |
| 1189 | return buffer; |
| 1190 | } |
| 1191 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1192 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1193 | posix_write(self, args) |
| 1194 | object *self; |
| 1195 | object *args; |
| 1196 | { |
| 1197 | int fd, size; |
| 1198 | char *buffer; |
| 1199 | if (!getargs(args, "(is#)", &fd, &buffer, &size)) |
| 1200 | return NULL; |
| 1201 | BGN_SAVE |
| 1202 | size = write(fd, buffer, size); |
| 1203 | END_SAVE |
| 1204 | if (size < 0) |
| 1205 | return posix_error(); |
| 1206 | return newintobject((long)size); |
| 1207 | } |
| 1208 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1209 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1210 | posix_fstat(self, args) |
| 1211 | object *self; |
| 1212 | object *args; |
| 1213 | { |
| 1214 | int fd; |
| 1215 | struct stat st; |
| 1216 | int res; |
| 1217 | if (!getargs(args, "i", &fd)) |
| 1218 | return NULL; |
| 1219 | BGN_SAVE |
| 1220 | res = fstat(fd, &st); |
| 1221 | END_SAVE |
| 1222 | if (res != 0) |
| 1223 | return posix_error(); |
| 1224 | return mkvalue("(llllllllll)", |
| 1225 | (long)st.st_mode, |
| 1226 | (long)st.st_ino, |
| 1227 | (long)st.st_dev, |
| 1228 | (long)st.st_nlink, |
| 1229 | (long)st.st_uid, |
| 1230 | (long)st.st_gid, |
| 1231 | (long)st.st_size, |
| 1232 | (long)st.st_atime, |
| 1233 | (long)st.st_mtime, |
| 1234 | (long)st.st_ctime); |
| 1235 | } |
| 1236 | |
| 1237 | static object * |
| 1238 | posix_fdopen(self, args) |
| 1239 | object *self; |
| 1240 | object *args; |
| 1241 | { |
| 1242 | extern int fclose PROTO((FILE *)); |
| 1243 | int fd; |
| 1244 | char *mode; |
| 1245 | FILE *fp; |
| 1246 | if (!getargs(args, "(is)", &fd, &mode)) |
| 1247 | return NULL; |
| 1248 | BGN_SAVE |
| 1249 | fp = fdopen(fd, mode); |
| 1250 | END_SAVE |
| 1251 | if (fp == NULL) |
| 1252 | return posix_error(); |
| 1253 | /* From now on, ignore SIGPIPE and let the error checking |
| 1254 | do the work. */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1255 | return newopenfileobject(fp, "(fdopen)", mode, fclose); |
| 1256 | } |
| 1257 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1258 | static object * |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1259 | posix_pipe(self, args) |
| 1260 | object *self; |
| 1261 | object *args; |
| 1262 | { |
| 1263 | int fds[2]; |
| 1264 | int res; |
| 1265 | if (!getargs(args, "")) |
| 1266 | return NULL; |
| 1267 | BGN_SAVE |
| 1268 | res = pipe(fds); |
| 1269 | END_SAVE |
| 1270 | if (res != 0) |
| 1271 | return posix_error(); |
| 1272 | return mkvalue("(ii)", fds[0], fds[1]); |
| 1273 | } |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1274 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1275 | static struct methodlist posix_methods[] = { |
| 1276 | {"chdir", posix_chdir}, |
| 1277 | {"chmod", posix_chmod}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1278 | #ifdef HAVE_CHOWN |
| 1279 | {"chown", posix_chown}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1280 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1281 | {"getcwd", posix_getcwd}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1282 | #ifdef HAVE_LINK |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1283 | {"link", posix_link}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1284 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1285 | {"listdir", posix_listdir}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1286 | {"lstat", posix_lstat}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1287 | {"mkdir", posix_mkdir}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1288 | #ifdef HAVE_NICE |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1289 | {"nice", posix_nice}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1290 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1291 | #ifdef HAVE_READLINK |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1292 | {"readlink", posix_readlink}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1293 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1294 | {"rename", posix_rename}, |
| 1295 | {"rmdir", posix_rmdir}, |
| 1296 | {"stat", posix_stat}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1297 | #ifdef HAVE_SYMLINK |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1298 | {"symlink", posix_symlink}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1299 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1300 | {"system", posix_system}, |
| 1301 | {"umask", posix_umask}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1302 | #ifdef HAVE_UNAME |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1303 | {"uname", posix_uname}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1304 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1305 | {"unlink", posix_unlink}, |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1306 | {"utime", posix_utime}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1307 | #ifdef HAVE_TIMES |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 1308 | {"times", posix_times}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1309 | #endif /* HAVE_TIMES */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1310 | {"_exit", posix__exit}, |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1311 | {"execv", posix_execv}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1312 | {"execve", posix_execve}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1313 | #ifndef NT |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1314 | {"fork", posix_fork}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1315 | {"getegid", posix_getegid}, |
| 1316 | {"geteuid", posix_geteuid}, |
| 1317 | {"getgid", posix_getgid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1318 | #endif /* !NT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1319 | {"getpid", posix_getpid}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1320 | #ifdef HAVE_GETPGRP |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1321 | {"getpgrp", posix_getpgrp}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1322 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1323 | #ifndef NT |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1324 | {"getppid", posix_getppid}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1325 | {"getuid", posix_getuid}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1326 | {"kill", posix_kill}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1327 | #endif /* !NT */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1328 | {"popen", posix_popen}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1329 | #ifdef HAVE_SETUID |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1330 | {"setuid", posix_setuid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1331 | #endif /* HAVE_SETUID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1332 | #ifdef HAVE_SETGID |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1333 | {"setgid", posix_setgid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1334 | #endif /* HAVE_SETGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1335 | #ifdef HAVE_SETPGRP |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1336 | {"setpgrp", posix_setpgrp}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1337 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1338 | #ifndef NT |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1339 | {"wait", posix_wait}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1340 | #endif /* !NT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1341 | #ifdef HAVE_WAITPID |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 1342 | {"waitpid", posix_waitpid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1343 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1344 | #ifdef HAVE_SETSID |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1345 | {"setsid", posix_setsid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1346 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1347 | #ifdef HAVE_SETPGID |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1348 | {"setpgid", posix_setpgid}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1349 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1350 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1351 | {"tcgetpgrp", posix_tcgetpgrp}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1352 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1353 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1354 | {"tcsetpgrp", posix_tcsetpgrp}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1355 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1356 | {"open", posix_open}, |
| 1357 | {"close", posix_close}, |
| 1358 | {"dup", posix_dup}, |
| 1359 | {"dup2", posix_dup2}, |
| 1360 | {"lseek", posix_lseek}, |
| 1361 | {"read", posix_read}, |
| 1362 | {"write", posix_write}, |
| 1363 | {"fstat", posix_fstat}, |
| 1364 | {"fdopen", posix_fdopen}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1365 | #ifndef NT |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1366 | {"pipe", posix_pipe}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1367 | #endif /* !NT */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1368 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1369 | {NULL, NULL} /* Sentinel */ |
| 1370 | }; |
| 1371 | |
| 1372 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1373 | #ifdef NT |
| 1374 | void |
| 1375 | initnt() |
| 1376 | { |
| 1377 | object *m, *d, *v; |
| 1378 | |
| 1379 | m = initmodule("nt", posix_methods); |
| 1380 | d = getmoduledict(m); |
| 1381 | |
| 1382 | /* Initialize nt.environ dictionary */ |
| 1383 | v = convertenviron(); |
| 1384 | if (v == NULL || dictinsert(d, "environ", v) != 0) |
| 1385 | fatal("can't define nt.environ"); |
| 1386 | DECREF(v); |
| 1387 | |
| 1388 | /* Initialize nt.error exception */ |
| 1389 | PosixError = newstringobject("nt.error"); |
| 1390 | if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0) |
| 1391 | fatal("can't define nt.error"); |
| 1392 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1393 | #else /* !NT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1394 | void |
| 1395 | initposix() |
| 1396 | { |
| 1397 | object *m, *d, *v; |
| 1398 | |
| 1399 | m = initmodule("posix", posix_methods); |
| 1400 | d = getmoduledict(m); |
| 1401 | |
| 1402 | /* Initialize posix.environ dictionary */ |
| 1403 | v = convertenviron(); |
| 1404 | if (v == NULL || dictinsert(d, "environ", v) != 0) |
| 1405 | fatal("can't define posix.environ"); |
| 1406 | DECREF(v); |
| 1407 | |
| 1408 | /* Initialize posix.error exception */ |
| 1409 | PosixError = newstringobject("posix.error"); |
| 1410 | if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0) |
| 1411 | fatal("can't define posix.error"); |
| 1412 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame^] | 1413 | #endif /* !NT */ |