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