Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* POSIX module implementation */ |
| 26 | |
Guido van Rossum | 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 | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 37 | #ifdef __sgi |
| 38 | #define DO_PG |
| 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 | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 76 | #include "ceval.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 78 | #ifdef _SEQUENT_ |
| 79 | #include <unistd.h> |
| 80 | #else /* _SEQUENT_ */ |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 81 | /* XXX Aren't these always declared in unistd.h? */ |
Guido van Rossum | 7f77e2d | 1990-10-30 13:34:38 +0000 | [diff] [blame] | 82 | extern char *strerror PROTO((int)); |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 83 | extern int chmod PROTO((const char *, mode_t)); |
| 84 | extern char *getcwd PROTO((char *, int)); /* XXX or size_t? */ |
| 85 | extern int mkdir PROTO((const char *, mode_t)); |
| 86 | extern int chdir PROTO((const char *)); |
| 87 | extern int link PROTO((const char *, const char *)); |
| 88 | extern int rename PROTO((const char *, const char *)); |
| 89 | extern int rmdir PROTO((const char *)); |
| 90 | extern int stat PROTO((const char *, struct stat *)); |
| 91 | extern int unlink PROTO((const char *)); |
| 92 | extern int pclose PROTO((FILE *)); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 93 | #endif /* _SEQUENT_ */ |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 94 | #ifdef NO_LSTAT |
| 95 | #define lstat stat |
| 96 | #else |
| 97 | extern int lstat PROTO((const char *, struct stat *)); |
| 98 | extern int symlink PROTO((const char *, const char *)); |
| 99 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | |
| 102 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 103 | |
| 104 | extern char **environ; |
| 105 | |
| 106 | static object * |
| 107 | convertenviron() |
| 108 | { |
| 109 | object *d; |
| 110 | char **e; |
| 111 | d = newdictobject(); |
| 112 | if (d == NULL) |
| 113 | return NULL; |
| 114 | if (environ == NULL) |
| 115 | return d; |
| 116 | /* XXX This part ignores errors */ |
| 117 | for (e = environ; *e != NULL; e++) { |
| 118 | object *v; |
| 119 | char *p = strchr(*e, '='); |
| 120 | if (p == NULL) |
| 121 | continue; |
| 122 | v = newstringobject(p+1); |
| 123 | if (v == NULL) |
| 124 | continue; |
| 125 | *p = '\0'; |
| 126 | (void) dictinsert(d, *e, v); |
| 127 | *p = '='; |
| 128 | DECREF(v); |
| 129 | } |
| 130 | return d; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | static object *PosixError; /* Exception posix.error */ |
| 135 | |
| 136 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 137 | |
Guido van Rossum | 9a1581c | 1990-10-21 13:12:47 +0000 | [diff] [blame] | 138 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 139 | posix_error() |
| 140 | { |
Guido van Rossum | e8f305a | 1990-10-14 20:04:28 +0000 | [diff] [blame] | 141 | return err_errno(PosixError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | /* POSIX generic methods */ |
| 146 | |
| 147 | static object * |
| 148 | posix_1str(args, func) |
| 149 | object *args; |
| 150 | int (*func) FPROTO((const char *)); |
| 151 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 152 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 153 | int res; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 154 | if (!getstrarg(args, &path1)) |
| 155 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 156 | BGN_SAVE |
| 157 | res = (*func)(path1); |
| 158 | END_SAVE |
| 159 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 160 | return posix_error(); |
| 161 | INCREF(None); |
| 162 | return None; |
| 163 | } |
| 164 | |
| 165 | static object * |
| 166 | posix_2str(args, func) |
| 167 | object *args; |
| 168 | int (*func) FPROTO((const char *, const char *)); |
| 169 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 170 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 171 | int res; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 172 | if (!getstrstrarg(args, &path1, &path2)) |
| 173 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 174 | BGN_SAVE |
| 175 | res = (*func)(path1, path2); |
| 176 | END_SAVE |
| 177 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 178 | return posix_error(); |
| 179 | INCREF(None); |
| 180 | return None; |
| 181 | } |
| 182 | |
| 183 | static object * |
| 184 | posix_strint(args, func) |
| 185 | object *args; |
| 186 | int (*func) FPROTO((const char *, int)); |
| 187 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 188 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | int i; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 190 | int res; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 191 | if (!getstrintarg(args, &path, &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 193 | BGN_SAVE |
| 194 | res = (*func)(path, i); |
| 195 | END_SAVE |
| 196 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | return posix_error(); |
| 198 | INCREF(None); |
| 199 | return None; |
| 200 | } |
| 201 | |
| 202 | static object * |
| 203 | posix_do_stat(self, args, statfunc) |
| 204 | object *self; |
| 205 | object *args; |
| 206 | int (*statfunc) FPROTO((const char *, struct stat *)); |
| 207 | { |
| 208 | struct stat st; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 209 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | object *v; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 211 | int res; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | if (!getstrarg(args, &path)) |
| 213 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 214 | BGN_SAVE |
| 215 | res = (*statfunc)(path, &st); |
| 216 | END_SAVE |
| 217 | if (res != 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | return posix_error(); |
| 219 | v = newtupleobject(10); |
| 220 | if (v == NULL) |
| 221 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | #define SET(i, st_member) settupleitem(v, i, newintobject((long)st.st_member)) |
| 223 | SET(0, st_mode); |
| 224 | SET(1, st_ino); |
| 225 | SET(2, st_dev); |
| 226 | SET(3, st_nlink); |
| 227 | SET(4, st_uid); |
| 228 | SET(5, st_gid); |
| 229 | SET(6, st_size); |
| 230 | SET(7, st_atime); |
| 231 | SET(8, st_mtime); |
| 232 | SET(9, st_ctime); |
| 233 | #undef SET |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 234 | if (err_occurred()) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 235 | DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 236 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | } |
| 238 | return v; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /* POSIX methods */ |
| 243 | |
| 244 | static object * |
| 245 | posix_chdir(self, args) |
| 246 | object *self; |
| 247 | object *args; |
| 248 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 249 | return posix_1str(args, chdir); |
| 250 | } |
| 251 | |
| 252 | static object * |
| 253 | posix_chmod(self, args) |
| 254 | object *self; |
| 255 | object *args; |
| 256 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | return posix_strint(args, chmod); |
| 258 | } |
| 259 | |
| 260 | static object * |
| 261 | posix_getcwd(self, args) |
| 262 | object *self; |
| 263 | object *args; |
| 264 | { |
| 265 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 266 | char *res; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 267 | if (!getnoarg(args)) |
| 268 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 269 | BGN_SAVE |
| 270 | res = getcwd(buf, sizeof buf); |
| 271 | END_SAVE |
| 272 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 273 | return posix_error(); |
| 274 | return newstringobject(buf); |
| 275 | } |
| 276 | |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 277 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 278 | static object * |
| 279 | posix_link(self, args) |
| 280 | object *self; |
| 281 | object *args; |
| 282 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 283 | return posix_2str(args, link); |
| 284 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 285 | #endif /* !MSDOS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | |
| 287 | static object * |
| 288 | posix_listdir(self, args) |
| 289 | object *self; |
| 290 | object *args; |
| 291 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 292 | char *name; |
| 293 | object *d, *v; |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 294 | |
| 295 | #ifdef MSDOS |
| 296 | struct ffblk ep; |
| 297 | int rv; |
| 298 | if (!getstrarg(args, &name)) |
| 299 | return NULL; |
| 300 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 301 | if (findfirst(name, &ep, 0) == -1) |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 302 | return posix_error(); |
| 303 | if ((d = newlistobject(0)) == NULL) |
| 304 | return NULL; |
| 305 | do { |
| 306 | v = newstringobject(ep.ff_name); |
| 307 | if (v == NULL) { |
| 308 | DECREF(d); |
| 309 | d = NULL; |
| 310 | break; |
| 311 | } |
| 312 | if (addlistitem(d, v) != 0) { |
| 313 | DECREF(v); |
| 314 | DECREF(d); |
| 315 | d = NULL; |
| 316 | break; |
| 317 | } |
| 318 | DECREF(v); |
| 319 | } while ((rv = findnext(&ep)) == 0); |
| 320 | #else /* !MSDOS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | DIR *dirp; |
| 322 | struct direct *ep; |
| 323 | if (!getstrarg(args, &name)) |
| 324 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 325 | BGN_SAVE |
| 326 | if ((dirp = opendir(name)) == NULL) { |
| 327 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | return posix_error(); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 329 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 330 | if ((d = newlistobject(0)) == NULL) { |
| 331 | closedir(dirp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 332 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | return NULL; |
| 334 | } |
| 335 | while ((ep = readdir(dirp)) != NULL) { |
| 336 | v = newstringobject(ep->d_name); |
| 337 | if (v == NULL) { |
| 338 | DECREF(d); |
| 339 | d = NULL; |
| 340 | break; |
| 341 | } |
| 342 | if (addlistitem(d, v) != 0) { |
| 343 | DECREF(v); |
| 344 | DECREF(d); |
| 345 | d = NULL; |
| 346 | break; |
| 347 | } |
| 348 | DECREF(v); |
| 349 | } |
| 350 | closedir(dirp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 351 | END_SAVE |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 352 | #endif /* !MSDOS */ |
| 353 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | return d; |
| 355 | } |
| 356 | |
| 357 | static object * |
| 358 | posix_mkdir(self, args) |
| 359 | object *self; |
| 360 | object *args; |
| 361 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | return posix_strint(args, mkdir); |
| 363 | } |
| 364 | |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 365 | #ifndef MSDOS |
| 366 | static object * |
| 367 | posix_nice(self, args) |
| 368 | object *self; |
| 369 | object *args; |
| 370 | { |
| 371 | int increment, value; |
| 372 | |
| 373 | if (!getargs(args, "i", &increment)) |
| 374 | return NULL; |
| 375 | value = nice(increment); |
| 376 | if (value == -1) |
| 377 | return posix_error(); |
| 378 | return newintobject((long) value); |
| 379 | } |
| 380 | #endif |
| 381 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 382 | #if i386 && ! _SEQUENT_ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 383 | int |
| 384 | rename(from, to) |
| 385 | char *from; |
| 386 | char *to; |
| 387 | { |
| 388 | int status; |
| 389 | /* XXX Shouldn't this unlink the destination first? */ |
| 390 | status = link(from, to); |
| 391 | if (status != 0) |
| 392 | return status; |
| 393 | return unlink(from); |
| 394 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 395 | #endif /* i386 && ! _SEQUENT_ */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 396 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 397 | static object * |
| 398 | posix_rename(self, args) |
| 399 | object *self; |
| 400 | object *args; |
| 401 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 402 | return posix_2str(args, rename); |
| 403 | } |
| 404 | |
| 405 | static object * |
| 406 | posix_rmdir(self, args) |
| 407 | object *self; |
| 408 | object *args; |
| 409 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | return posix_1str(args, rmdir); |
| 411 | } |
| 412 | |
| 413 | static object * |
| 414 | posix_stat(self, args) |
| 415 | object *self; |
| 416 | object *args; |
| 417 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 418 | return posix_do_stat(self, args, stat); |
| 419 | } |
| 420 | |
| 421 | static object * |
| 422 | posix_system(self, args) |
| 423 | object *self; |
| 424 | object *args; |
| 425 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 426 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 427 | long sts; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 428 | if (!getstrarg(args, &command)) |
| 429 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 430 | BGN_SAVE |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 431 | sts = system(command); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 432 | END_SAVE |
| 433 | return newintobject(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 436 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 437 | static object * |
| 438 | posix_umask(self, args) |
| 439 | object *self; |
| 440 | object *args; |
| 441 | { |
| 442 | int i; |
| 443 | if (!getintarg(args, &i)) |
| 444 | return NULL; |
| 445 | i = umask(i); |
| 446 | if (i < 0) |
| 447 | return posix_error(); |
| 448 | return newintobject((long)i); |
| 449 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 450 | #endif /* !MSDOS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 451 | |
| 452 | static object * |
| 453 | posix_unlink(self, args) |
| 454 | object *self; |
| 455 | object *args; |
| 456 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 457 | return posix_1str(args, unlink); |
| 458 | } |
| 459 | |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 460 | #ifndef NO_UNAME |
| 461 | #include <sys/utsname.h> |
| 462 | |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 463 | extern int uname PROTO((struct utsname *)); |
| 464 | |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 465 | static object * |
| 466 | posix_uname(self, args) |
| 467 | object *self; |
| 468 | object *args; |
| 469 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 470 | struct utsname u; |
| 471 | object *v; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 472 | int res; |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 473 | if (!getnoarg(args)) |
| 474 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 475 | BGN_SAVE |
| 476 | res = uname(&u); |
| 477 | END_SAVE |
| 478 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 479 | return posix_error(); |
| 480 | v = newtupleobject(5); |
| 481 | if (v == NULL) |
| 482 | return NULL; |
| 483 | #define SET(i, member) settupleitem(v, i, newstringobject(u.member)) |
| 484 | SET(0, sysname); |
| 485 | SET(1, nodename); |
| 486 | SET(2, release); |
| 487 | SET(3, version); |
| 488 | SET(4, machine); |
| 489 | #undef SET |
| 490 | if (err_occurred()) { |
| 491 | DECREF(v); |
| 492 | return NULL; |
| 493 | } |
| 494 | return v; |
| 495 | } |
| 496 | #endif /* NO_UNAME */ |
| 497 | |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 498 | #ifdef UTIME_STRUCT |
| 499 | #include <utime.h> |
| 500 | #endif |
| 501 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 502 | static object * |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 503 | posix_utime(self, args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 504 | object *self; |
| 505 | object *args; |
| 506 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 507 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 508 | int res; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 509 | |
| 510 | #ifdef UTIME_STRUCT |
| 511 | struct utimbuf buf; |
| 512 | #define ATIME buf.actime |
| 513 | #define MTIME buf.modtime |
| 514 | #define UTIME_ARG &buf |
| 515 | |
| 516 | #else |
| 517 | time_t buf[2]; |
| 518 | #define ATIME buf[0] |
| 519 | #define MTIME buf[1] |
| 520 | #define UTIME_ARG buf |
| 521 | #endif |
| 522 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 523 | if (!getargs(args, "(s(ll))", &path, &ATIME, &MTIME)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 524 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 525 | BGN_SAVE |
| 526 | res = utime(path, UTIME_ARG); |
| 527 | END_SAVE |
| 528 | if (res < 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 529 | return posix_error(); |
| 530 | INCREF(None); |
| 531 | return None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 532 | #undef UTIME_ARG |
| 533 | #undef ATIME |
| 534 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 537 | |
| 538 | #ifndef MSDOS |
| 539 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 540 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 541 | |
| 542 | static object * |
| 543 | posix__exit(self, args) |
| 544 | object *self; |
| 545 | object *args; |
| 546 | { |
| 547 | int sts; |
| 548 | if (!getintarg(args, &sts)) |
| 549 | return NULL; |
| 550 | _exit(sts); |
| 551 | /* NOTREACHED */ |
| 552 | } |
| 553 | |
| 554 | /* XXX To do: exece, execp */ |
| 555 | |
| 556 | static object * |
| 557 | posix_exec(self, args) |
| 558 | object *self; |
| 559 | object *args; |
| 560 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 561 | char *path; |
| 562 | object *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 563 | char **argvlist; |
| 564 | int i, argc; |
| 565 | object *(*getitem) PROTO((object *, int)); |
| 566 | |
| 567 | /* exec has two arguments: (path, argv), where |
| 568 | argv is a list or tuple of strings. */ |
| 569 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 570 | if (!getargs(args, "(sO)", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 571 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 572 | if (is_listobject(argv)) { |
| 573 | argc = getlistsize(argv); |
| 574 | getitem = getlistitem; |
| 575 | } |
| 576 | else if (is_tupleobject(argv)) { |
| 577 | argc = gettuplesize(argv); |
| 578 | getitem = gettupleitem; |
| 579 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 580 | else { |
| 581 | badarg: |
| 582 | err_badarg(); |
| 583 | return NULL; |
| 584 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 585 | |
| 586 | argvlist = NEW(char *, argc+1); |
| 587 | if (argvlist == NULL) |
| 588 | return NULL; |
| 589 | for (i = 0; i < argc; i++) { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 590 | if (!getstrarg((*getitem)(argv, i), &argvlist[i])) { |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 591 | DEL(argvlist); |
| 592 | goto badarg; |
| 593 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 594 | } |
| 595 | argvlist[argc] = NULL; |
| 596 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 597 | execv(path, argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 598 | |
| 599 | /* If we get here it's definitely an error */ |
| 600 | |
| 601 | DEL(argvlist); |
| 602 | return posix_error(); |
| 603 | } |
| 604 | |
| 605 | static object * |
| 606 | posix_fork(self, args) |
| 607 | object *self; |
| 608 | object *args; |
| 609 | { |
| 610 | int pid; |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 611 | if (!getnoarg(args)) |
| 612 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 613 | pid = fork(); |
| 614 | if (pid == -1) |
| 615 | return posix_error(); |
| 616 | return newintobject((long)pid); |
| 617 | } |
| 618 | |
| 619 | static object * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 620 | posix_getegid(self, args) |
| 621 | object *self; |
| 622 | object *args; |
| 623 | { |
| 624 | if (!getnoarg(args)) |
| 625 | return NULL; |
| 626 | return newintobject((long)getegid()); |
| 627 | } |
| 628 | |
| 629 | static object * |
| 630 | posix_geteuid(self, args) |
| 631 | object *self; |
| 632 | object *args; |
| 633 | { |
| 634 | if (!getnoarg(args)) |
| 635 | return NULL; |
| 636 | return newintobject((long)geteuid()); |
| 637 | } |
| 638 | |
| 639 | static object * |
| 640 | posix_getgid(self, args) |
| 641 | object *self; |
| 642 | object *args; |
| 643 | { |
| 644 | if (!getnoarg(args)) |
| 645 | return NULL; |
| 646 | return newintobject((long)getgid()); |
| 647 | } |
| 648 | |
| 649 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 650 | posix_getpid(self, args) |
| 651 | object *self; |
| 652 | object *args; |
| 653 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 654 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 655 | return NULL; |
| 656 | return newintobject((long)getpid()); |
| 657 | } |
| 658 | |
| 659 | static object * |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 660 | posix_getpgrp(self, args) |
| 661 | object *self; |
| 662 | object *args; |
| 663 | { |
| 664 | if (!getnoarg(args)) |
| 665 | return NULL; |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 666 | #ifdef SYSV |
| 667 | return newintobject((long)getpgrp()); |
| 668 | #else |
Guido van Rossum | 971443b | 1991-06-07 13:59:29 +0000 | [diff] [blame] | 669 | return newintobject((long)getpgrp(0)); |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 670 | #endif |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static object * |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 674 | posix_setpgrp(self, args) |
| 675 | object *self; |
| 676 | object *args; |
| 677 | { |
| 678 | if (!getnoarg(args)) |
| 679 | return NULL; |
| 680 | #ifdef SYSV |
| 681 | if (setpgrp() < 0) |
| 682 | #else |
| 683 | if (setpgrp(0, 0) < 0) |
| 684 | #endif |
| 685 | return err_errno(PosixError); |
| 686 | INCREF(None); |
| 687 | return None; |
| 688 | } |
| 689 | |
| 690 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 691 | posix_getppid(self, args) |
| 692 | object *self; |
| 693 | object *args; |
| 694 | { |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 695 | if (!getnoarg(args)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 696 | return NULL; |
| 697 | return newintobject((long)getppid()); |
| 698 | } |
| 699 | |
| 700 | static object * |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 701 | posix_getuid(self, args) |
| 702 | object *self; |
| 703 | object *args; |
| 704 | { |
| 705 | if (!getnoarg(args)) |
| 706 | return NULL; |
| 707 | return newintobject((long)getuid()); |
| 708 | } |
| 709 | |
| 710 | static object * |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 711 | posix_kill(self, args) |
| 712 | object *self; |
| 713 | object *args; |
| 714 | { |
| 715 | int pid, sig; |
| 716 | if (!getintintarg(args, &pid, &sig)) |
| 717 | return NULL; |
| 718 | if (kill(pid, sig) == -1) |
| 719 | return posix_error(); |
| 720 | INCREF(None); |
| 721 | return None; |
| 722 | } |
| 723 | |
| 724 | static object * |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 725 | posix_popen(self, args) |
| 726 | object *self; |
| 727 | object *args; |
| 728 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 729 | char *name, *mode; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 730 | FILE *fp; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 731 | if (!getargs(args, "(ss)", &name, &mode)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 732 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 733 | BGN_SAVE |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 734 | fp = popen(name, mode); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 735 | END_SAVE |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 736 | if (fp == NULL) |
| 737 | return posix_error(); |
Guido van Rossum | e0d452d | 1991-07-27 21:41:01 +0000 | [diff] [blame] | 738 | /* From now on, ignore SIGPIPE and let the error checking |
| 739 | do the work. */ |
| 740 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 741 | return newopenfileobject(fp, name, mode, pclose); |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static object * |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 745 | posix_waitpid(self, args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 746 | object *self; |
| 747 | object *args; |
| 748 | { |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 749 | #ifdef NO_WAITPID |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 750 | err_setstr(PosixError, |
| 751 | "posix.waitpid() not supported on this system"); |
| 752 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 753 | #else |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 754 | int pid, options, sts; |
| 755 | if (!getargs(args, "(ii)", &pid, &options)) |
| 756 | return NULL; |
| 757 | BGN_SAVE |
| 758 | pid = waitpid(pid, &sts, options); |
| 759 | END_SAVE |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 760 | if (pid == -1) |
| 761 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 762 | else |
| 763 | return mkvalue("ii", pid, sts); |
| 764 | #endif |
| 765 | } |
| 766 | |
| 767 | static object * |
| 768 | posix_wait(self, args) |
| 769 | object *self; |
| 770 | object *args; |
| 771 | { |
| 772 | int pid, sts; |
| 773 | if (args != NULL) |
| 774 | return posix_waitpid(self, args); /* BW compat */ |
| 775 | BGN_SAVE |
| 776 | pid = wait(&sts); |
| 777 | END_SAVE |
| 778 | if (pid == -1) |
| 779 | return posix_error(); |
| 780 | else |
| 781 | return mkvalue("ii", pid, sts); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | #endif /* MSDOS */ |
| 785 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 786 | static object * |
| 787 | posix_lstat(self, args) |
| 788 | object *self; |
| 789 | object *args; |
| 790 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 791 | return posix_do_stat(self, args, lstat); |
| 792 | } |
| 793 | |
| 794 | static object * |
| 795 | posix_readlink(self, args) |
| 796 | object *self; |
| 797 | object *args; |
| 798 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 799 | #ifdef NO_LSTAT |
| 800 | err_setstr(PosixError, "readlink not implemented on this system"); |
| 801 | return NULL; |
| 802 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 803 | char buf[1024]; /* XXX Should use MAXPATHLEN */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 804 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 805 | int n; |
| 806 | if (!getstrarg(args, &path)) |
| 807 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 808 | BGN_SAVE |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 809 | n = readlink(path, buf, (int) sizeof buf); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 810 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 811 | if (n < 0) |
| 812 | return posix_error(); |
| 813 | return newsizedstringobject(buf, n); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 814 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | static object * |
| 818 | posix_symlink(self, args) |
| 819 | object *self; |
| 820 | object *args; |
| 821 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 822 | #ifdef NO_LSTAT |
| 823 | err_setstr(PosixError, "symlink not implemented on this system"); |
| 824 | return NULL; |
| 825 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 826 | return posix_2str(args, symlink); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 827 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 831 | #ifdef DO_TIMES |
| 832 | |
| 833 | static object * |
| 834 | posix_times(self, args) |
| 835 | object *self; |
| 836 | object *args; |
| 837 | { |
| 838 | struct tms t; |
| 839 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 840 | if (!getnoarg(args)) |
| 841 | return NULL; |
| 842 | errno = 0; |
| 843 | c = times(&t); |
| 844 | if (c == (clock_t) -1) { |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 845 | err_errno(PosixError); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 846 | return NULL; |
| 847 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 848 | return mkvalue("dddd", |
| 849 | (double)t.tms_utime / HZ, |
| 850 | (double)t.tms_stime / HZ, |
| 851 | (double)t.tms_cutime / HZ, |
| 852 | (double)t.tms_cstime / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 855 | #endif /* DO_TIMES */ |
| 856 | |
| 857 | #ifdef DO_PG |
| 858 | |
| 859 | static object * |
| 860 | posix_setsid(self, args) |
| 861 | object *self; |
| 862 | object *args; |
| 863 | { |
| 864 | if (!getnoarg(args)) |
| 865 | return NULL; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 866 | if (setsid() < 0) { |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 867 | err_errno(PosixError); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 868 | return NULL; |
| 869 | } |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 870 | INCREF(None); |
| 871 | return None; |
| 872 | } |
| 873 | |
| 874 | static object * |
| 875 | posix_setpgid(self, args) |
| 876 | object *self; |
| 877 | object *args; |
| 878 | { |
| 879 | int pid, pgrp; |
| 880 | if (!getargs(args, "(ii)", &pid, &pgrp)) |
| 881 | return NULL; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 882 | if (setpgid(pid, pgrp) < 0) { |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 883 | err_errno(PosixError); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 884 | return NULL; |
| 885 | } |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 886 | INCREF(None); |
| 887 | return None; |
| 888 | } |
| 889 | |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 890 | static object * |
| 891 | posix_tcgetpgrp(self, args) |
| 892 | object *self; |
| 893 | object *args; |
| 894 | { |
| 895 | int fd, pgid; |
| 896 | if (!getargs(args, "i", &fd)) |
| 897 | return NULL; |
| 898 | pgid = tcgetpgrp(fd); |
| 899 | if (pgid < 0) { |
| 900 | err_errno(PosixError); |
| 901 | return NULL; |
| 902 | } |
| 903 | return newintobject((long)pgid); |
| 904 | } |
| 905 | |
| 906 | static object * |
| 907 | posix_tcsetpgrp(self, args) |
| 908 | object *self; |
| 909 | object *args; |
| 910 | { |
| 911 | int fd, pgid; |
| 912 | if (!getargs(args, "(ii)", &fd, &pgid)) |
| 913 | return NULL; |
| 914 | if (tcsetpgrp(fd, pgid) < 0) { |
| 915 | err_errno(PosixError); |
| 916 | return NULL; |
| 917 | } |
| 918 | INCREF(None); |
| 919 | return None; |
| 920 | } |
| 921 | |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 922 | #endif /* DO_PG */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 923 | |
| 924 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 925 | static struct methodlist posix_methods[] = { |
| 926 | {"chdir", posix_chdir}, |
| 927 | {"chmod", posix_chmod}, |
| 928 | {"getcwd", posix_getcwd}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 929 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 930 | {"link", posix_link}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 931 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 932 | {"listdir", posix_listdir}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 933 | {"lstat", posix_lstat}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 934 | {"mkdir", posix_mkdir}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 935 | #ifndef MSDOS |
| 936 | {"nice", posix_nice}, |
| 937 | #endif |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 938 | {"readlink", posix_readlink}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 939 | {"rename", posix_rename}, |
| 940 | {"rmdir", posix_rmdir}, |
| 941 | {"stat", posix_stat}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 942 | {"symlink", posix_symlink}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 943 | {"system", posix_system}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 944 | #ifndef MSDOS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 945 | {"umask", posix_umask}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 946 | #endif |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 947 | #ifndef NO_UNAME |
| 948 | {"uname", posix_uname}, |
| 949 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 950 | {"unlink", posix_unlink}, |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 951 | {"utime", posix_utime}, |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 952 | #ifdef DO_TIMES |
| 953 | {"times", posix_times}, |
| 954 | #endif |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 955 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 956 | #ifndef MSDOS |
| 957 | {"_exit", posix__exit}, |
| 958 | {"exec", posix_exec}, |
| 959 | {"fork", posix_fork}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 960 | {"getegid", posix_getegid}, |
| 961 | {"geteuid", posix_geteuid}, |
| 962 | {"getgid", posix_getgid}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 963 | {"getpid", posix_getpid}, |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 964 | {"getpgrp", posix_getpgrp}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 965 | {"getppid", posix_getppid}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 966 | {"getuid", posix_getuid}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 967 | {"kill", posix_kill}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 968 | {"popen", posix_popen}, |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 969 | {"setpgrp", posix_setpgrp}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 970 | {"wait", posix_wait}, |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 971 | {"waitpid", posix_waitpid}, |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 972 | #endif |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 973 | |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 974 | #ifdef DO_PG |
| 975 | {"setsid", posix_setsid}, |
| 976 | {"setpgid", posix_setpgid}, |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 977 | {"tcgetpgrp", posix_tcgetpgrp}, |
| 978 | {"tcsetpgrp", posix_tcsetpgrp}, |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 979 | #endif |
| 980 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 981 | {NULL, NULL} /* Sentinel */ |
| 982 | }; |
| 983 | |
| 984 | |
| 985 | void |
| 986 | initposix() |
| 987 | { |
| 988 | object *m, *d, *v; |
| 989 | |
| 990 | m = initmodule("posix", posix_methods); |
| 991 | d = getmoduledict(m); |
| 992 | |
| 993 | /* Initialize posix.environ dictionary */ |
| 994 | v = convertenviron(); |
| 995 | if (v == NULL || dictinsert(d, "environ", v) != 0) |
| 996 | fatal("can't define posix.environ"); |
| 997 | DECREF(v); |
| 998 | |
| 999 | /* Initialize posix.error exception */ |
| 1000 | PosixError = newstringobject("posix.error"); |
| 1001 | if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0) |
| 1002 | fatal("can't define posix.error"); |
| 1003 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1004 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1005 | |
| 1006 | /* Function used elsewhere to get a file's modification time */ |
| 1007 | |
| 1008 | long |
| 1009 | getmtime(path) |
| 1010 | char *path; |
| 1011 | { |
| 1012 | struct stat st; |
| 1013 | if (stat(path, &st) != 0) |
| 1014 | return -1; |
| 1015 | else |
| 1016 | return st.st_mtime; |
| 1017 | } |
| 1018 | |
| 1019 | |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1020 | #ifdef MSDOS |
| 1021 | |
| 1022 | /* A small "compatibility library" for TurboC under MS-DOS */ |
| 1023 | |
| 1024 | #include <sir.h> |
| 1025 | #include <io.h> |
| 1026 | #include <dos.h> |
| 1027 | #include <fcntl.h> |
| 1028 | |
| 1029 | int |
| 1030 | chmod(path, mode) |
| 1031 | char *path; |
| 1032 | int mode; |
| 1033 | { |
| 1034 | return _chmod(path, 1, mode); |
| 1035 | } |
| 1036 | |
| 1037 | int |
| 1038 | utime(path, times) |
| 1039 | char *path; |
| 1040 | time_t times[2]; |
| 1041 | { |
| 1042 | struct date dt; |
| 1043 | struct time tm; |
| 1044 | struct ftime dft; |
| 1045 | int fh; |
| 1046 | unixtodos(tv[0].tv_sec,&dt,&tm); |
| 1047 | dft.ft_tsec = tm.ti_sec; dft.ft_min = tm.ti_min; |
| 1048 | dft.ft_hour = tm.ti_hour; dft.ft_day = dt.da_day; |
| 1049 | dft.ft_month = dt.da_mon; |
| 1050 | dft.ft_year = (dt.da_year - 1980); /* this is for TC library */ |
| 1051 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1052 | if ((fh = open(path,O_RDWR)) < 0) |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1053 | return posix_error(); /* can't open file to set time */ |
| 1054 | if (setftime(fh,&dft) < 0) |
| 1055 | { |
| 1056 | close(fh); |
| 1057 | return posix_error(); |
| 1058 | } |
| 1059 | close(fh); /* close the temp handle */ |
| 1060 | } |
| 1061 | |
| 1062 | #endif /* MSDOS */ |