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 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 115 | char *path1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | if (!getstrarg(args, &path1)) |
| 117 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 118 | if ((*func)(path1) < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 129 | char *path1, *path2; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | if (!getstrstrarg(args, &path1, &path2)) |
| 131 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 132 | if ((*func)(path1, path2) < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 143 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | int i; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 145 | if (!getstrintarg(args, &path, &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 147 | if ((*func)(path, i) < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 160 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | object *v; |
| 162 | if (!getstrarg(args, &path)) |
| 163 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 164 | if ((*statfunc)(path, &st) != 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 239 | char *name; |
| 240 | object *d, *v; |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 241 | |
| 242 | #ifdef MSDOS |
| 243 | struct ffblk ep; |
| 244 | int rv; |
| 245 | if (!getstrarg(args, &name)) |
| 246 | return NULL; |
| 247 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 248 | if (findfirst(name, &ep, 0) == -1) |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 249 | return posix_error(); |
| 250 | if ((d = newlistobject(0)) == NULL) |
| 251 | return NULL; |
| 252 | do { |
| 253 | v = newstringobject(ep.ff_name); |
| 254 | if (v == NULL) { |
| 255 | DECREF(d); |
| 256 | d = NULL; |
| 257 | break; |
| 258 | } |
| 259 | if (addlistitem(d, v) != 0) { |
| 260 | DECREF(v); |
| 261 | DECREF(d); |
| 262 | d = NULL; |
| 263 | break; |
| 264 | } |
| 265 | DECREF(v); |
| 266 | } while ((rv = findnext(&ep)) == 0); |
| 267 | #else /* !MSDOS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 268 | DIR *dirp; |
| 269 | struct direct *ep; |
| 270 | if (!getstrarg(args, &name)) |
| 271 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 272 | if ((dirp = opendir(name)) == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 273 | return posix_error(); |
| 274 | if ((d = newlistobject(0)) == NULL) { |
| 275 | closedir(dirp); |
| 276 | return NULL; |
| 277 | } |
| 278 | while ((ep = readdir(dirp)) != NULL) { |
| 279 | v = newstringobject(ep->d_name); |
| 280 | if (v == NULL) { |
| 281 | DECREF(d); |
| 282 | d = NULL; |
| 283 | break; |
| 284 | } |
| 285 | if (addlistitem(d, v) != 0) { |
| 286 | DECREF(v); |
| 287 | DECREF(d); |
| 288 | d = NULL; |
| 289 | break; |
| 290 | } |
| 291 | DECREF(v); |
| 292 | } |
| 293 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 294 | #endif /* !MSDOS */ |
| 295 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | return d; |
| 297 | } |
| 298 | |
| 299 | static object * |
| 300 | posix_mkdir(self, args) |
| 301 | object *self; |
| 302 | object *args; |
| 303 | { |
| 304 | extern int mkdir PROTO((const char *, mode_t)); |
| 305 | return posix_strint(args, mkdir); |
| 306 | } |
| 307 | |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 308 | #ifdef i386 |
| 309 | int |
| 310 | rename(from, to) |
| 311 | char *from; |
| 312 | char *to; |
| 313 | { |
| 314 | int status; |
| 315 | /* XXX Shouldn't this unlink the destination first? */ |
| 316 | status = link(from, to); |
| 317 | if (status != 0) |
| 318 | return status; |
| 319 | return unlink(from); |
| 320 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 321 | #endif /* i386 */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 322 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | static object * |
| 324 | posix_rename(self, args) |
| 325 | object *self; |
| 326 | object *args; |
| 327 | { |
| 328 | extern int rename PROTO((const char *, const char *)); |
| 329 | return posix_2str(args, rename); |
| 330 | } |
| 331 | |
| 332 | static object * |
| 333 | posix_rmdir(self, args) |
| 334 | object *self; |
| 335 | object *args; |
| 336 | { |
| 337 | extern int rmdir PROTO((const char *)); |
| 338 | return posix_1str(args, rmdir); |
| 339 | } |
| 340 | |
| 341 | static object * |
| 342 | posix_stat(self, args) |
| 343 | object *self; |
| 344 | object *args; |
| 345 | { |
| 346 | extern int stat PROTO((const char *, struct stat *)); |
| 347 | return posix_do_stat(self, args, stat); |
| 348 | } |
| 349 | |
| 350 | static object * |
| 351 | posix_system(self, args) |
| 352 | object *self; |
| 353 | object *args; |
| 354 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 355 | char *command; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 356 | int sts; |
| 357 | if (!getstrarg(args, &command)) |
| 358 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 359 | sts = system(command); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 360 | return newintobject((long)sts); |
| 361 | } |
| 362 | |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 363 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | static object * |
| 365 | posix_umask(self, args) |
| 366 | object *self; |
| 367 | object *args; |
| 368 | { |
| 369 | int i; |
| 370 | if (!getintarg(args, &i)) |
| 371 | return NULL; |
| 372 | i = umask(i); |
| 373 | if (i < 0) |
| 374 | return posix_error(); |
| 375 | return newintobject((long)i); |
| 376 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 377 | #endif /* !MSDOS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 378 | |
| 379 | static object * |
| 380 | posix_unlink(self, args) |
| 381 | object *self; |
| 382 | object *args; |
| 383 | { |
| 384 | extern int unlink PROTO((const char *)); |
| 385 | return posix_1str(args, unlink); |
| 386 | } |
| 387 | |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 388 | #ifdef UTIME_STRUCT |
| 389 | #include <utime.h> |
| 390 | #endif |
| 391 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 392 | static object * |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 393 | posix_utime(self, args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 394 | object *self; |
| 395 | object *args; |
| 396 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 397 | char *path; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 398 | |
| 399 | #ifdef UTIME_STRUCT |
| 400 | struct utimbuf buf; |
| 401 | #define ATIME buf.actime |
| 402 | #define MTIME buf.modtime |
| 403 | #define UTIME_ARG &buf |
| 404 | |
| 405 | #else |
| 406 | time_t buf[2]; |
| 407 | #define ATIME buf[0] |
| 408 | #define MTIME buf[1] |
| 409 | #define UTIME_ARG buf |
| 410 | #endif |
| 411 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 412 | if (!getargs(args, "(s(ll))", &path, &ATIME, &MTIME)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 413 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 414 | if (utime(path, UTIME_ARG) < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 415 | return posix_error(); |
| 416 | INCREF(None); |
| 417 | return None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 418 | #undef UTIME_ARG |
| 419 | #undef ATIME |
| 420 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 423 | |
| 424 | #ifndef MSDOS |
| 425 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 426 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 427 | |
| 428 | static object * |
| 429 | posix__exit(self, args) |
| 430 | object *self; |
| 431 | object *args; |
| 432 | { |
| 433 | int sts; |
| 434 | if (!getintarg(args, &sts)) |
| 435 | return NULL; |
| 436 | _exit(sts); |
| 437 | /* NOTREACHED */ |
| 438 | } |
| 439 | |
| 440 | /* XXX To do: exece, execp */ |
| 441 | |
| 442 | static object * |
| 443 | posix_exec(self, args) |
| 444 | object *self; |
| 445 | object *args; |
| 446 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 447 | char *path; |
| 448 | object *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 449 | char **argvlist; |
| 450 | int i, argc; |
| 451 | object *(*getitem) PROTO((object *, int)); |
| 452 | |
| 453 | /* exec has two arguments: (path, argv), where |
| 454 | argv is a list or tuple of strings. */ |
| 455 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 456 | if (!getargs(args, "(sO)", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 457 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 458 | if (is_listobject(argv)) { |
| 459 | argc = getlistsize(argv); |
| 460 | getitem = getlistitem; |
| 461 | } |
| 462 | else if (is_tupleobject(argv)) { |
| 463 | argc = gettuplesize(argv); |
| 464 | getitem = gettupleitem; |
| 465 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 466 | else { |
| 467 | badarg: |
| 468 | err_badarg(); |
| 469 | return NULL; |
| 470 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 471 | |
| 472 | argvlist = NEW(char *, argc+1); |
| 473 | if (argvlist == NULL) |
| 474 | return NULL; |
| 475 | for (i = 0; i < argc; i++) { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 476 | if (!getstrarg((*getitem)(argv, i), &argvlist[i])) { |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 477 | DEL(argvlist); |
| 478 | goto badarg; |
| 479 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 480 | } |
| 481 | argvlist[argc] = NULL; |
| 482 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 483 | execv(path, argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 484 | |
| 485 | /* If we get here it's definitely an error */ |
| 486 | |
| 487 | DEL(argvlist); |
| 488 | return posix_error(); |
| 489 | } |
| 490 | |
| 491 | static object * |
| 492 | posix_fork(self, args) |
| 493 | object *self; |
| 494 | object *args; |
| 495 | { |
| 496 | int pid; |
| 497 | pid = fork(); |
| 498 | if (pid == -1) |
| 499 | return posix_error(); |
| 500 | return newintobject((long)pid); |
| 501 | } |
| 502 | |
| 503 | static object * |
| 504 | posix_getpid(self, args) |
| 505 | object *self; |
| 506 | object *args; |
| 507 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 508 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 509 | return NULL; |
| 510 | return newintobject((long)getpid()); |
| 511 | } |
| 512 | |
| 513 | static object * |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 514 | posix_getpgrp(self, args) |
| 515 | object *self; |
| 516 | object *args; |
| 517 | { |
| 518 | if (!getnoarg(args)) |
| 519 | return NULL; |
Guido van Rossum | 971443b | 1991-06-07 13:59:29 +0000 | [diff] [blame] | 520 | return newintobject((long)getpgrp(0)); |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 524 | posix_getppid(self, args) |
| 525 | object *self; |
| 526 | object *args; |
| 527 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 528 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 529 | return NULL; |
| 530 | return newintobject((long)getppid()); |
| 531 | } |
| 532 | |
| 533 | static object * |
| 534 | posix_kill(self, args) |
| 535 | object *self; |
| 536 | object *args; |
| 537 | { |
| 538 | int pid, sig; |
| 539 | if (!getintintarg(args, &pid, &sig)) |
| 540 | return NULL; |
| 541 | if (kill(pid, sig) == -1) |
| 542 | return posix_error(); |
| 543 | INCREF(None); |
| 544 | return None; |
| 545 | } |
| 546 | |
| 547 | static object * |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 548 | posix_popen(self, args) |
| 549 | object *self; |
| 550 | object *args; |
| 551 | { |
| 552 | extern int pclose PROTO((FILE *)); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 553 | char *name, *mode; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 554 | FILE *fp; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 555 | if (!getargs(args, "(ss)", &name, &mode)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 556 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 557 | fp = popen(name, mode); |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 558 | if (fp == NULL) |
| 559 | return posix_error(); |
Guido van Rossum | e0d452d | 1991-07-27 21:41:01 +0000 | [diff] [blame] | 560 | /* From now on, ignore SIGPIPE and let the error checking |
| 561 | do the work. */ |
| 562 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 563 | return newopenfileobject(fp, name, mode, pclose); |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 567 | posix_wait(self, args) /* Also waitpid() */ |
| 568 | object *self; |
| 569 | object *args; |
| 570 | { |
| 571 | object *v; |
| 572 | int pid, sts; |
| 573 | if (args == NULL) |
| 574 | pid = wait(&sts); |
| 575 | else { |
| 576 | #ifdef NO_WAITPID |
| 577 | err_setstr(RuntimeError, |
| 578 | "posix.wait(pid, options) not supported on this system"); |
| 579 | #else |
| 580 | int options; |
| 581 | if (!getintintarg(args, &pid, &options)) |
| 582 | return NULL; |
| 583 | pid = waitpid(pid, &sts, options); |
| 584 | #endif |
| 585 | } |
| 586 | if (pid == -1) |
| 587 | return posix_error(); |
| 588 | v = newtupleobject(2); |
| 589 | if (v != NULL) { |
| 590 | settupleitem(v, 0, newintobject((long)pid)); |
| 591 | settupleitem(v, 1, newintobject((long)sts)); |
| 592 | if (err_occurred()) { |
| 593 | DECREF(v); |
| 594 | v = NULL; |
| 595 | } |
| 596 | } |
| 597 | return v; |
| 598 | } |
| 599 | |
| 600 | #endif /* MSDOS */ |
| 601 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 602 | #ifndef NO_LSTAT |
| 603 | |
| 604 | static object * |
| 605 | posix_lstat(self, args) |
| 606 | object *self; |
| 607 | object *args; |
| 608 | { |
| 609 | extern int lstat PROTO((const char *, struct stat *)); |
| 610 | return posix_do_stat(self, args, lstat); |
| 611 | } |
| 612 | |
| 613 | static object * |
| 614 | posix_readlink(self, args) |
| 615 | object *self; |
| 616 | object *args; |
| 617 | { |
| 618 | char buf[1024]; /* XXX Should use MAXPATHLEN */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 619 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 620 | int n; |
| 621 | if (!getstrarg(args, &path)) |
| 622 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 623 | n = readlink(path, buf, sizeof buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 624 | if (n < 0) |
| 625 | return posix_error(); |
| 626 | return newsizedstringobject(buf, n); |
| 627 | } |
| 628 | |
| 629 | static object * |
| 630 | posix_symlink(self, args) |
| 631 | object *self; |
| 632 | object *args; |
| 633 | { |
| 634 | extern int symlink PROTO((const char *, const char *)); |
| 635 | return posix_2str(args, symlink); |
| 636 | } |
| 637 | |
| 638 | #endif /* NO_LSTAT */ |
| 639 | |
| 640 | |
| 641 | static struct methodlist posix_methods[] = { |
| 642 | {"chdir", posix_chdir}, |
| 643 | {"chmod", posix_chmod}, |
| 644 | {"getcwd", posix_getcwd}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 645 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 646 | {"link", posix_link}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 647 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 648 | {"listdir", posix_listdir}, |
| 649 | {"mkdir", posix_mkdir}, |
| 650 | {"rename", posix_rename}, |
| 651 | {"rmdir", posix_rmdir}, |
| 652 | {"stat", posix_stat}, |
| 653 | {"system", posix_system}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 654 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 655 | {"umask", posix_umask}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 656 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 657 | {"unlink", posix_unlink}, |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 658 | {"utime", posix_utime}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 659 | #ifndef MSDOS |
| 660 | {"_exit", posix__exit}, |
| 661 | {"exec", posix_exec}, |
| 662 | {"fork", posix_fork}, |
| 663 | {"getpid", posix_getpid}, |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 664 | {"getpgrp", posix_getpgrp}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 665 | {"getppid", posix_getppid}, |
| 666 | {"kill", posix_kill}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 667 | {"popen", posix_popen}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 668 | {"wait", posix_wait}, |
| 669 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 670 | #ifndef NO_LSTAT |
| 671 | {"lstat", posix_lstat}, |
| 672 | {"readlink", posix_readlink}, |
| 673 | {"symlink", posix_symlink}, |
| 674 | #endif |
| 675 | {NULL, NULL} /* Sentinel */ |
| 676 | }; |
| 677 | |
| 678 | |
| 679 | void |
| 680 | initposix() |
| 681 | { |
| 682 | object *m, *d, *v; |
| 683 | |
| 684 | m = initmodule("posix", posix_methods); |
| 685 | d = getmoduledict(m); |
| 686 | |
| 687 | /* Initialize posix.environ dictionary */ |
| 688 | v = convertenviron(); |
| 689 | if (v == NULL || dictinsert(d, "environ", v) != 0) |
| 690 | fatal("can't define posix.environ"); |
| 691 | DECREF(v); |
| 692 | |
| 693 | /* Initialize posix.error exception */ |
| 694 | PosixError = newstringobject("posix.error"); |
| 695 | if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0) |
| 696 | fatal("can't define posix.error"); |
| 697 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 699 | |
| 700 | /* Function used elsewhere to get a file's modification time */ |
| 701 | |
| 702 | long |
| 703 | getmtime(path) |
| 704 | char *path; |
| 705 | { |
| 706 | struct stat st; |
| 707 | if (stat(path, &st) != 0) |
| 708 | return -1; |
| 709 | else |
| 710 | return st.st_mtime; |
| 711 | } |
| 712 | |
| 713 | |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 714 | #ifdef MSDOS |
| 715 | |
| 716 | /* A small "compatibility library" for TurboC under MS-DOS */ |
| 717 | |
| 718 | #include <sir.h> |
| 719 | #include <io.h> |
| 720 | #include <dos.h> |
| 721 | #include <fcntl.h> |
| 722 | |
| 723 | int |
| 724 | chmod(path, mode) |
| 725 | char *path; |
| 726 | int mode; |
| 727 | { |
| 728 | return _chmod(path, 1, mode); |
| 729 | } |
| 730 | |
| 731 | int |
| 732 | utime(path, times) |
| 733 | char *path; |
| 734 | time_t times[2]; |
| 735 | { |
| 736 | struct date dt; |
| 737 | struct time tm; |
| 738 | struct ftime dft; |
| 739 | int fh; |
| 740 | unixtodos(tv[0].tv_sec,&dt,&tm); |
| 741 | dft.ft_tsec = tm.ti_sec; dft.ft_min = tm.ti_min; |
| 742 | dft.ft_hour = tm.ti_hour; dft.ft_day = dt.da_day; |
| 743 | dft.ft_month = dt.da_mon; |
| 744 | dft.ft_year = (dt.da_year - 1980); /* this is for TC library */ |
| 745 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame^] | 746 | if ((fh = open(path,O_RDWR)) < 0) |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 747 | return posix_error(); /* can't open file to set time */ |
| 748 | if (setftime(fh,&dft) < 0) |
| 749 | { |
| 750 | close(fh); |
| 751 | return posix_error(); |
| 752 | } |
| 753 | close(fh); /* close the temp handle */ |
| 754 | } |
| 755 | |
| 756 | #endif /* MSDOS */ |