Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 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 | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 27 | #ifdef AMOEBA |
| 28 | #define NO_LSTAT |
| 29 | #define SYSV |
| 30 | #endif |
| 31 | |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 32 | #ifdef MSDOS |
| 33 | #define NO_LSTAT |
| 34 | #endif |
| 35 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | #include <signal.h> |
| 37 | #include <string.h> |
| 38 | #include <setjmp.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | #ifdef SYSV |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 44 | #define UTIME_STRUCT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | #include <dirent.h> |
| 46 | #define direct dirent |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 47 | #ifdef i386 |
| 48 | #define mode_t int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | #endif |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 51 | #else /* !SYSV */ |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 52 | |
| 53 | #ifndef MSDOS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 54 | #include <sys/dir.h> |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 57 | #endif /* !SYSV */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 59 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | #include "modsupport.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 7f77e2d | 1990-10-30 13:34:38 +0000 | [diff] [blame] | 62 | extern char *strerror PROTO((int)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | |
| 65 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 66 | |
| 67 | extern char **environ; |
| 68 | |
| 69 | static object * |
| 70 | convertenviron() |
| 71 | { |
| 72 | object *d; |
| 73 | char **e; |
| 74 | d = newdictobject(); |
| 75 | if (d == NULL) |
| 76 | return NULL; |
| 77 | if (environ == NULL) |
| 78 | return d; |
| 79 | /* XXX This part ignores errors */ |
| 80 | for (e = environ; *e != NULL; e++) { |
| 81 | object *v; |
| 82 | char *p = strchr(*e, '='); |
| 83 | if (p == NULL) |
| 84 | continue; |
| 85 | v = newstringobject(p+1); |
| 86 | if (v == NULL) |
| 87 | continue; |
| 88 | *p = '\0'; |
| 89 | (void) dictinsert(d, *e, v); |
| 90 | *p = '='; |
| 91 | DECREF(v); |
| 92 | } |
| 93 | return d; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static object *PosixError; /* Exception posix.error */ |
| 98 | |
| 99 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 100 | |
Guido van Rossum | 9a1581c | 1990-10-21 13:12:47 +0000 | [diff] [blame] | 101 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | posix_error() |
| 103 | { |
Guido van Rossum | e8f305a | 1990-10-14 20:04:28 +0000 | [diff] [blame] | 104 | return err_errno(PosixError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | |
| 108 | /* POSIX generic methods */ |
| 109 | |
| 110 | static object * |
| 111 | posix_1str(args, func) |
| 112 | object *args; |
| 113 | int (*func) FPROTO((const char *)); |
| 114 | { |
| 115 | object *path1; |
| 116 | if (!getstrarg(args, &path1)) |
| 117 | return NULL; |
| 118 | if ((*func)(getstringvalue(path1)) < 0) |
| 119 | return posix_error(); |
| 120 | INCREF(None); |
| 121 | return None; |
| 122 | } |
| 123 | |
| 124 | static object * |
| 125 | posix_2str(args, func) |
| 126 | object *args; |
| 127 | int (*func) FPROTO((const char *, const char *)); |
| 128 | { |
| 129 | object *path1, *path2; |
| 130 | if (!getstrstrarg(args, &path1, &path2)) |
| 131 | return NULL; |
| 132 | if ((*func)(getstringvalue(path1), getstringvalue(path2)) < 0) |
| 133 | return posix_error(); |
| 134 | INCREF(None); |
| 135 | return None; |
| 136 | } |
| 137 | |
| 138 | static object * |
| 139 | posix_strint(args, func) |
| 140 | object *args; |
| 141 | int (*func) FPROTO((const char *, int)); |
| 142 | { |
| 143 | object *path1; |
| 144 | int i; |
| 145 | if (!getstrintarg(args, &path1, &i)) |
| 146 | return NULL; |
| 147 | if ((*func)(getstringvalue(path1), i) < 0) |
| 148 | return posix_error(); |
| 149 | INCREF(None); |
| 150 | return None; |
| 151 | } |
| 152 | |
| 153 | static object * |
| 154 | posix_do_stat(self, args, statfunc) |
| 155 | object *self; |
| 156 | object *args; |
| 157 | int (*statfunc) FPROTO((const char *, struct stat *)); |
| 158 | { |
| 159 | struct stat st; |
| 160 | object *path; |
| 161 | object *v; |
| 162 | if (!getstrarg(args, &path)) |
| 163 | return NULL; |
| 164 | if ((*statfunc)(getstringvalue(path), &st) != 0) |
| 165 | return posix_error(); |
| 166 | v = newtupleobject(10); |
| 167 | if (v == NULL) |
| 168 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | #define SET(i, st_member) settupleitem(v, i, newintobject((long)st.st_member)) |
| 170 | SET(0, st_mode); |
| 171 | SET(1, st_ino); |
| 172 | SET(2, st_dev); |
| 173 | SET(3, st_nlink); |
| 174 | SET(4, st_uid); |
| 175 | SET(5, st_gid); |
| 176 | SET(6, st_size); |
| 177 | SET(7, st_atime); |
| 178 | SET(8, st_mtime); |
| 179 | SET(9, st_ctime); |
| 180 | #undef SET |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 181 | if (err_occurred()) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 183 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | } |
| 185 | return v; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /* POSIX methods */ |
| 190 | |
| 191 | static object * |
| 192 | posix_chdir(self, args) |
| 193 | object *self; |
| 194 | object *args; |
| 195 | { |
| 196 | extern int chdir PROTO((const char *)); |
| 197 | return posix_1str(args, chdir); |
| 198 | } |
| 199 | |
| 200 | static object * |
| 201 | posix_chmod(self, args) |
| 202 | object *self; |
| 203 | object *args; |
| 204 | { |
| 205 | extern int chmod PROTO((const char *, mode_t)); |
| 206 | return posix_strint(args, chmod); |
| 207 | } |
| 208 | |
| 209 | static object * |
| 210 | posix_getcwd(self, args) |
| 211 | object *self; |
| 212 | object *args; |
| 213 | { |
| 214 | char buf[1026]; |
| 215 | extern char *getcwd PROTO((char *, int)); |
| 216 | if (!getnoarg(args)) |
| 217 | return NULL; |
| 218 | if (getcwd(buf, sizeof buf) == NULL) |
| 219 | return posix_error(); |
| 220 | return newstringobject(buf); |
| 221 | } |
| 222 | |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 223 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | static object * |
| 225 | posix_link(self, args) |
| 226 | object *self; |
| 227 | object *args; |
| 228 | { |
| 229 | extern int link PROTO((const char *, const char *)); |
| 230 | return posix_2str(args, link); |
| 231 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 232 | #endif /* !MSDOS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | |
| 234 | static object * |
| 235 | posix_listdir(self, args) |
| 236 | object *self; |
| 237 | object *args; |
| 238 | { |
| 239 | object *name, *d, *v; |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 240 | |
| 241 | #ifdef MSDOS |
| 242 | struct ffblk ep; |
| 243 | int rv; |
| 244 | if (!getstrarg(args, &name)) |
| 245 | return NULL; |
| 246 | |
| 247 | if (findfirst((char *) getstringvalue(name), &ep, 0) == -1) |
| 248 | return posix_error(); |
| 249 | if ((d = newlistobject(0)) == NULL) |
| 250 | return NULL; |
| 251 | do { |
| 252 | v = newstringobject(ep.ff_name); |
| 253 | if (v == NULL) { |
| 254 | DECREF(d); |
| 255 | d = NULL; |
| 256 | break; |
| 257 | } |
| 258 | if (addlistitem(d, v) != 0) { |
| 259 | DECREF(v); |
| 260 | DECREF(d); |
| 261 | d = NULL; |
| 262 | break; |
| 263 | } |
| 264 | DECREF(v); |
| 265 | } while ((rv = findnext(&ep)) == 0); |
| 266 | #else /* !MSDOS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 267 | DIR *dirp; |
| 268 | struct direct *ep; |
| 269 | if (!getstrarg(args, &name)) |
| 270 | return NULL; |
| 271 | if ((dirp = opendir(getstringvalue(name))) == NULL) |
| 272 | return posix_error(); |
| 273 | if ((d = newlistobject(0)) == NULL) { |
| 274 | closedir(dirp); |
| 275 | return NULL; |
| 276 | } |
| 277 | while ((ep = readdir(dirp)) != NULL) { |
| 278 | v = newstringobject(ep->d_name); |
| 279 | if (v == NULL) { |
| 280 | DECREF(d); |
| 281 | d = NULL; |
| 282 | break; |
| 283 | } |
| 284 | if (addlistitem(d, v) != 0) { |
| 285 | DECREF(v); |
| 286 | DECREF(d); |
| 287 | d = NULL; |
| 288 | break; |
| 289 | } |
| 290 | DECREF(v); |
| 291 | } |
| 292 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 293 | #endif /* !MSDOS */ |
| 294 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 295 | return d; |
| 296 | } |
| 297 | |
| 298 | static object * |
| 299 | posix_mkdir(self, args) |
| 300 | object *self; |
| 301 | object *args; |
| 302 | { |
| 303 | extern int mkdir PROTO((const char *, mode_t)); |
| 304 | return posix_strint(args, mkdir); |
| 305 | } |
| 306 | |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 307 | #ifdef i386 |
| 308 | int |
| 309 | rename(from, to) |
| 310 | char *from; |
| 311 | char *to; |
| 312 | { |
| 313 | int status; |
| 314 | /* XXX Shouldn't this unlink the destination first? */ |
| 315 | status = link(from, to); |
| 316 | if (status != 0) |
| 317 | return status; |
| 318 | return unlink(from); |
| 319 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 320 | #endif /* i386 */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | static object * |
| 323 | posix_rename(self, args) |
| 324 | object *self; |
| 325 | object *args; |
| 326 | { |
| 327 | extern int rename PROTO((const char *, const char *)); |
| 328 | return posix_2str(args, rename); |
| 329 | } |
| 330 | |
| 331 | static object * |
| 332 | posix_rmdir(self, args) |
| 333 | object *self; |
| 334 | object *args; |
| 335 | { |
| 336 | extern int rmdir PROTO((const char *)); |
| 337 | return posix_1str(args, rmdir); |
| 338 | } |
| 339 | |
| 340 | static object * |
| 341 | posix_stat(self, args) |
| 342 | object *self; |
| 343 | object *args; |
| 344 | { |
| 345 | extern int stat PROTO((const char *, struct stat *)); |
| 346 | return posix_do_stat(self, args, stat); |
| 347 | } |
| 348 | |
| 349 | static object * |
| 350 | posix_system(self, args) |
| 351 | object *self; |
| 352 | object *args; |
| 353 | { |
| 354 | object *command; |
| 355 | int sts; |
| 356 | if (!getstrarg(args, &command)) |
| 357 | return NULL; |
| 358 | sts = system(getstringvalue(command)); |
| 359 | return newintobject((long)sts); |
| 360 | } |
| 361 | |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 362 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 363 | static object * |
| 364 | posix_umask(self, args) |
| 365 | object *self; |
| 366 | object *args; |
| 367 | { |
| 368 | int i; |
| 369 | if (!getintarg(args, &i)) |
| 370 | return NULL; |
| 371 | i = umask(i); |
| 372 | if (i < 0) |
| 373 | return posix_error(); |
| 374 | return newintobject((long)i); |
| 375 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 376 | #endif /* !MSDOS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | |
| 378 | static object * |
| 379 | posix_unlink(self, args) |
| 380 | object *self; |
| 381 | object *args; |
| 382 | { |
| 383 | extern int unlink PROTO((const char *)); |
| 384 | return posix_1str(args, unlink); |
| 385 | } |
| 386 | |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 387 | #ifdef UTIME_STRUCT |
| 388 | #include <utime.h> |
| 389 | #endif |
| 390 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 391 | static object * |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 392 | posix_utime(self, args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | object *self; |
| 394 | object *args; |
| 395 | { |
| 396 | object *path; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 397 | |
| 398 | #ifdef UTIME_STRUCT |
| 399 | struct utimbuf buf; |
| 400 | #define ATIME buf.actime |
| 401 | #define MTIME buf.modtime |
| 402 | #define UTIME_ARG &buf |
| 403 | |
| 404 | #else |
| 405 | time_t buf[2]; |
| 406 | #define ATIME buf[0] |
| 407 | #define MTIME buf[1] |
| 408 | #define UTIME_ARG buf |
| 409 | #endif |
| 410 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 411 | if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) { |
| 412 | err_badarg(); |
| 413 | return NULL; |
| 414 | } |
| 415 | if (!getstrarg(gettupleitem(args, 0), &path) || |
Guido van Rossum | b988c33 | 1991-09-10 14:56:48 +0000 | [diff] [blame] | 416 | !getlonglongarg(gettupleitem(args, 1), &ATIME, &MTIME)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 417 | return NULL; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 418 | if (utime(getstringvalue(path), UTIME_ARG) < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | return posix_error(); |
| 420 | INCREF(None); |
| 421 | return None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 422 | #undef UTIME_ARG |
| 423 | #undef ATIME |
| 424 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 427 | |
| 428 | #ifndef MSDOS |
| 429 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 430 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 431 | |
| 432 | static object * |
| 433 | posix__exit(self, args) |
| 434 | object *self; |
| 435 | object *args; |
| 436 | { |
| 437 | int sts; |
| 438 | if (!getintarg(args, &sts)) |
| 439 | return NULL; |
| 440 | _exit(sts); |
| 441 | /* NOTREACHED */ |
| 442 | } |
| 443 | |
| 444 | /* XXX To do: exece, execp */ |
| 445 | |
| 446 | static object * |
| 447 | posix_exec(self, args) |
| 448 | object *self; |
| 449 | object *args; |
| 450 | { |
| 451 | object *path, *argv; |
| 452 | char **argvlist; |
| 453 | int i, argc; |
| 454 | object *(*getitem) PROTO((object *, int)); |
| 455 | |
| 456 | /* exec has two arguments: (path, argv), where |
| 457 | argv is a list or tuple of strings. */ |
| 458 | |
| 459 | if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) { |
| 460 | badarg: |
| 461 | err_badarg(); |
| 462 | return NULL; |
| 463 | } |
| 464 | if (!getstrarg(gettupleitem(args, 0), &path)) |
| 465 | return NULL; |
| 466 | argv = gettupleitem(args, 1); |
| 467 | if (argv == NULL) |
| 468 | goto badarg; |
| 469 | if (is_listobject(argv)) { |
| 470 | argc = getlistsize(argv); |
| 471 | getitem = getlistitem; |
| 472 | } |
| 473 | else if (is_tupleobject(argv)) { |
| 474 | argc = gettuplesize(argv); |
| 475 | getitem = gettupleitem; |
| 476 | } |
| 477 | else |
| 478 | goto badarg; |
| 479 | |
| 480 | argvlist = NEW(char *, argc+1); |
| 481 | if (argvlist == NULL) |
| 482 | return NULL; |
| 483 | for (i = 0; i < argc; i++) { |
| 484 | object *arg; |
| 485 | if (!getstrarg((*getitem)(argv, i), &arg)) { |
| 486 | DEL(argvlist); |
| 487 | goto badarg; |
| 488 | } |
| 489 | argvlist[i] = getstringvalue(arg); |
| 490 | } |
| 491 | argvlist[argc] = NULL; |
| 492 | |
| 493 | execv(getstringvalue(path), argvlist); |
| 494 | |
| 495 | /* If we get here it's definitely an error */ |
| 496 | |
| 497 | DEL(argvlist); |
| 498 | return posix_error(); |
| 499 | } |
| 500 | |
| 501 | static object * |
| 502 | posix_fork(self, args) |
| 503 | object *self; |
| 504 | object *args; |
| 505 | { |
| 506 | int pid; |
| 507 | pid = fork(); |
| 508 | if (pid == -1) |
| 509 | return posix_error(); |
| 510 | return newintobject((long)pid); |
| 511 | } |
| 512 | |
| 513 | static object * |
| 514 | posix_getpid(self, args) |
| 515 | object *self; |
| 516 | object *args; |
| 517 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 518 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 519 | return NULL; |
| 520 | return newintobject((long)getpid()); |
| 521 | } |
| 522 | |
| 523 | static object * |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 524 | posix_getpgrp(self, args) |
| 525 | object *self; |
| 526 | object *args; |
| 527 | { |
| 528 | if (!getnoarg(args)) |
| 529 | return NULL; |
Guido van Rossum | 971443b | 1991-06-07 13:59:29 +0000 | [diff] [blame] | 530 | return newintobject((long)getpgrp(0)); |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 534 | posix_getppid(self, args) |
| 535 | object *self; |
| 536 | object *args; |
| 537 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 538 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 539 | return NULL; |
| 540 | return newintobject((long)getppid()); |
| 541 | } |
| 542 | |
| 543 | static object * |
| 544 | posix_kill(self, args) |
| 545 | object *self; |
| 546 | object *args; |
| 547 | { |
| 548 | int pid, sig; |
| 549 | if (!getintintarg(args, &pid, &sig)) |
| 550 | return NULL; |
| 551 | if (kill(pid, sig) == -1) |
| 552 | return posix_error(); |
| 553 | INCREF(None); |
| 554 | return None; |
| 555 | } |
| 556 | |
| 557 | static object * |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 558 | posix_popen(self, args) |
| 559 | object *self; |
| 560 | object *args; |
| 561 | { |
| 562 | extern int pclose PROTO((FILE *)); |
| 563 | object *name, *mode; |
| 564 | FILE *fp; |
| 565 | if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2 || |
| 566 | !is_stringobject(name = gettupleitem(args, 0)) || |
| 567 | !is_stringobject(mode = gettupleitem(args, 1))) { |
Guido van Rossum | 9db4013 | 1991-07-01 18:48:44 +0000 | [diff] [blame] | 568 | err_setstr(TypeError, "popen() requires 2 string arguments"); |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 569 | return NULL; |
| 570 | } |
| 571 | fp = popen(getstringvalue(name), getstringvalue(mode)); |
| 572 | if (fp == NULL) |
| 573 | return posix_error(); |
Guido van Rossum | e0d452d | 1991-07-27 21:41:01 +0000 | [diff] [blame] | 574 | /* From now on, ignore SIGPIPE and let the error checking |
| 575 | do the work. */ |
| 576 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | b988c33 | 1991-09-10 14:56:48 +0000 | [diff] [blame] | 577 | return newopenfileobject(fp, getstringvalue(name), |
| 578 | getstringvalue(mode), pclose); |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 582 | posix_wait(self, args) /* Also waitpid() */ |
| 583 | object *self; |
| 584 | object *args; |
| 585 | { |
| 586 | object *v; |
| 587 | int pid, sts; |
| 588 | if (args == NULL) |
| 589 | pid = wait(&sts); |
| 590 | else { |
| 591 | #ifdef NO_WAITPID |
| 592 | err_setstr(RuntimeError, |
| 593 | "posix.wait(pid, options) not supported on this system"); |
| 594 | #else |
| 595 | int options; |
| 596 | if (!getintintarg(args, &pid, &options)) |
| 597 | return NULL; |
| 598 | pid = waitpid(pid, &sts, options); |
| 599 | #endif |
| 600 | } |
| 601 | if (pid == -1) |
| 602 | return posix_error(); |
| 603 | v = newtupleobject(2); |
| 604 | if (v != NULL) { |
| 605 | settupleitem(v, 0, newintobject((long)pid)); |
| 606 | settupleitem(v, 1, newintobject((long)sts)); |
| 607 | if (err_occurred()) { |
| 608 | DECREF(v); |
| 609 | v = NULL; |
| 610 | } |
| 611 | } |
| 612 | return v; |
| 613 | } |
| 614 | |
| 615 | #endif /* MSDOS */ |
| 616 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 617 | #ifndef NO_LSTAT |
| 618 | |
| 619 | static object * |
| 620 | posix_lstat(self, args) |
| 621 | object *self; |
| 622 | object *args; |
| 623 | { |
| 624 | extern int lstat PROTO((const char *, struct stat *)); |
| 625 | return posix_do_stat(self, args, lstat); |
| 626 | } |
| 627 | |
| 628 | static object * |
| 629 | posix_readlink(self, args) |
| 630 | object *self; |
| 631 | object *args; |
| 632 | { |
| 633 | char buf[1024]; /* XXX Should use MAXPATHLEN */ |
| 634 | object *path; |
| 635 | int n; |
| 636 | if (!getstrarg(args, &path)) |
| 637 | return NULL; |
| 638 | n = readlink(getstringvalue(path), buf, sizeof buf); |
| 639 | if (n < 0) |
| 640 | return posix_error(); |
| 641 | return newsizedstringobject(buf, n); |
| 642 | } |
| 643 | |
| 644 | static object * |
| 645 | posix_symlink(self, args) |
| 646 | object *self; |
| 647 | object *args; |
| 648 | { |
| 649 | extern int symlink PROTO((const char *, const char *)); |
| 650 | return posix_2str(args, symlink); |
| 651 | } |
| 652 | |
| 653 | #endif /* NO_LSTAT */ |
| 654 | |
| 655 | |
| 656 | static struct methodlist posix_methods[] = { |
| 657 | {"chdir", posix_chdir}, |
| 658 | {"chmod", posix_chmod}, |
| 659 | {"getcwd", posix_getcwd}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 660 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 661 | {"link", posix_link}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 662 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 663 | {"listdir", posix_listdir}, |
| 664 | {"mkdir", posix_mkdir}, |
| 665 | {"rename", posix_rename}, |
| 666 | {"rmdir", posix_rmdir}, |
| 667 | {"stat", posix_stat}, |
| 668 | {"system", posix_system}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 669 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 670 | {"umask", posix_umask}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 671 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 672 | {"unlink", posix_unlink}, |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 673 | {"utime", posix_utime}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 674 | #ifndef MSDOS |
| 675 | {"_exit", posix__exit}, |
| 676 | {"exec", posix_exec}, |
| 677 | {"fork", posix_fork}, |
| 678 | {"getpid", posix_getpid}, |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 679 | {"getpgrp", posix_getpgrp}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 680 | {"getppid", posix_getppid}, |
| 681 | {"kill", posix_kill}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 682 | {"popen", posix_popen}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 683 | {"wait", posix_wait}, |
| 684 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 685 | #ifndef NO_LSTAT |
| 686 | {"lstat", posix_lstat}, |
| 687 | {"readlink", posix_readlink}, |
| 688 | {"symlink", posix_symlink}, |
| 689 | #endif |
| 690 | {NULL, NULL} /* Sentinel */ |
| 691 | }; |
| 692 | |
| 693 | |
| 694 | void |
| 695 | initposix() |
| 696 | { |
| 697 | object *m, *d, *v; |
| 698 | |
| 699 | m = initmodule("posix", posix_methods); |
| 700 | d = getmoduledict(m); |
| 701 | |
| 702 | /* Initialize posix.environ dictionary */ |
| 703 | v = convertenviron(); |
| 704 | if (v == NULL || dictinsert(d, "environ", v) != 0) |
| 705 | fatal("can't define posix.environ"); |
| 706 | DECREF(v); |
| 707 | |
| 708 | /* Initialize posix.error exception */ |
| 709 | PosixError = newstringobject("posix.error"); |
| 710 | if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0) |
| 711 | fatal("can't define posix.error"); |
| 712 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 713 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 714 | |
| 715 | /* Function used elsewhere to get a file's modification time */ |
| 716 | |
| 717 | long |
| 718 | getmtime(path) |
| 719 | char *path; |
| 720 | { |
| 721 | struct stat st; |
| 722 | if (stat(path, &st) != 0) |
| 723 | return -1; |
| 724 | else |
| 725 | return st.st_mtime; |
| 726 | } |
| 727 | |
| 728 | |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 729 | #ifdef MSDOS |
| 730 | |
| 731 | /* A small "compatibility library" for TurboC under MS-DOS */ |
| 732 | |
| 733 | #include <sir.h> |
| 734 | #include <io.h> |
| 735 | #include <dos.h> |
| 736 | #include <fcntl.h> |
| 737 | |
| 738 | int |
| 739 | chmod(path, mode) |
| 740 | char *path; |
| 741 | int mode; |
| 742 | { |
| 743 | return _chmod(path, 1, mode); |
| 744 | } |
| 745 | |
| 746 | int |
| 747 | utime(path, times) |
| 748 | char *path; |
| 749 | time_t times[2]; |
| 750 | { |
| 751 | struct date dt; |
| 752 | struct time tm; |
| 753 | struct ftime dft; |
| 754 | int fh; |
| 755 | unixtodos(tv[0].tv_sec,&dt,&tm); |
| 756 | dft.ft_tsec = tm.ti_sec; dft.ft_min = tm.ti_min; |
| 757 | dft.ft_hour = tm.ti_hour; dft.ft_day = dt.da_day; |
| 758 | dft.ft_month = dt.da_mon; |
| 759 | dft.ft_year = (dt.da_year - 1980); /* this is for TC library */ |
| 760 | |
| 761 | if ((fh = open(getstringvalue(path),O_RDWR)) < 0) |
| 762 | return posix_error(); /* can't open file to set time */ |
| 763 | if (setftime(fh,&dft) < 0) |
| 764 | { |
| 765 | close(fh); |
| 766 | return posix_error(); |
| 767 | } |
| 768 | close(fh); /* close the temp handle */ |
| 769 | } |
| 770 | |
| 771 | #endif /* MSDOS */ |