Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 9954699 | 1995-01-08 14:33:34 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +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 | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 25 | /* Mac module implementation */ |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 26 | |
| 27 | #include "allobjects.h" |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 28 | #include "modsupport.h" |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 29 | #include "ceval.h" |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <errno.h> |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 34 | |
| 35 | #ifdef THINK_C |
| 36 | #include "unix.h" |
| 37 | #undef S_IFMT |
| 38 | #undef S_IFDIR |
| 39 | #undef S_IFCHR |
| 40 | #undef S_IFBLK |
| 41 | #undef S_IFREG |
| 42 | #undef S_ISDIR |
| 43 | #undef S_ISREG |
| 44 | #endif |
| 45 | |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 46 | #ifdef USE_GUSI |
| 47 | #include <sys/types.h> |
| 48 | #include <stat.h> |
| 49 | #define macstat stat |
| 50 | #else |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 51 | #include "macstat.h" |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 52 | #endif |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 53 | |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 54 | #ifdef __MWERKS__ |
| 55 | /* For CodeWarrior 4 also define CW4 */ |
| 56 | #include <unix.h> |
| 57 | #else |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 58 | #include <fcntl.h> |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 59 | #endif |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 5693412 | 1995-02-13 16:19:04 +0000 | [diff] [blame] | 61 | #if defined(__MWERKS__) && defined(__powerc) |
Jack Jansen | 7cbf480 | 1995-01-22 16:52:38 +0000 | [diff] [blame] | 62 | #define MALLOC_DEBUG |
| 63 | #endif |
| 64 | |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 65 | #include "macdefs.h" |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 66 | #ifdef USE_GUSI |
| 67 | #include <dirent.h> |
| 68 | #else |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 69 | #include "dirent.h" |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 70 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 71 | |
| 72 | #ifndef MAXPATHLEN |
| 73 | #define MAXPATHLEN 1024 |
| 74 | #endif |
| 75 | |
| 76 | /* Prototypes for Unix simulation on Mac */ |
| 77 | |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 78 | int chdir PROTO((const char *path)); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 79 | char *getbootvol PROTO((void)); |
| 80 | char *getwd PROTO((char *)); |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 81 | #ifdef USE_GUSI |
| 82 | int mkdir PROTO((const char *path)); |
| 83 | DIR * opendir PROTO((const char *)); |
| 84 | int closedir PROTO((DIR *)); |
| 85 | #else |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 86 | int mkdir PROTO((const char *path, int mode)); |
| 87 | DIR * opendir PROTO((char *)); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 88 | void closedir PROTO((DIR *)); |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 89 | #endif |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 90 | struct dirent * readdir PROTO((DIR *)); |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 91 | int rmdir PROTO((const char *path)); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 92 | int sync PROTO((void)); |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 93 | #if defined(THINK_C) || defined(__SC__) || defined(USE_GUSI) |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 94 | int unlink PROTO((char *)); |
| 95 | #else |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 96 | int unlink PROTO((const char *)); |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 97 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | |
| 101 | static object *MacError; /* Exception mac.error */ |
| 102 | |
| 103 | /* Set a MAC-specific error from errno, and return NULL */ |
| 104 | |
| 105 | static object * |
| 106 | mac_error() |
| 107 | { |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 108 | return err_errno(MacError); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* MAC generic methods */ |
| 112 | |
| 113 | static object * |
| 114 | mac_1str(args, func) |
| 115 | object *args; |
| 116 | int (*func) FPROTO((const char *)); |
| 117 | { |
| 118 | char *path1; |
| 119 | int res; |
| 120 | if (!getargs(args, "s", &path1)) |
| 121 | return NULL; |
| 122 | BGN_SAVE |
| 123 | res = (*func)(path1); |
| 124 | END_SAVE |
| 125 | if (res < 0) |
| 126 | return mac_error(); |
| 127 | INCREF(None); |
| 128 | return None; |
| 129 | } |
| 130 | |
| 131 | static object * |
| 132 | mac_2str(args, func) |
| 133 | object *args; |
| 134 | int (*func) FPROTO((const char *, const char *)); |
| 135 | { |
| 136 | char *path1, *path2; |
| 137 | int res; |
| 138 | if (!getargs(args, "(ss)", &path1, &path2)) |
| 139 | return NULL; |
| 140 | BGN_SAVE |
| 141 | res = (*func)(path1, path2); |
| 142 | END_SAVE |
| 143 | if (res < 0) |
| 144 | return mac_error(); |
| 145 | INCREF(None); |
| 146 | return None; |
| 147 | } |
| 148 | |
| 149 | static object * |
| 150 | mac_strint(args, func) |
| 151 | object *args; |
| 152 | int (*func) FPROTO((const char *, int)); |
| 153 | { |
| 154 | char *path; |
| 155 | int i; |
| 156 | int res; |
| 157 | if (!getargs(args, "(si)", &path, &i)) |
| 158 | return NULL; |
| 159 | BGN_SAVE |
| 160 | res = (*func)(path, i); |
| 161 | END_SAVE |
| 162 | if (res < 0) |
| 163 | return mac_error(); |
| 164 | INCREF(None); |
| 165 | return None; |
| 166 | } |
| 167 | |
| 168 | static object * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 169 | mac_chdir(self, args) |
| 170 | object *self; |
| 171 | object *args; |
| 172 | { |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 173 | return mac_1str(args, chdir); |
| 174 | } |
| 175 | |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 176 | #ifndef CW4 |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 177 | static object * |
| 178 | mac_close(self, args) |
| 179 | object *self; |
| 180 | object *args; |
| 181 | { |
| 182 | int fd, res; |
| 183 | if (!getargs(args, "i", &fd)) |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 184 | return NULL; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 185 | BGN_SAVE |
| 186 | res = close(fd); |
| 187 | END_SAVE |
| 188 | if (res < 0) |
| 189 | return mac_error(); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 190 | INCREF(None); |
| 191 | return None; |
| 192 | } |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 193 | #endif /* !__MWERKS__ */ |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 195 | #ifdef MPW |
| 196 | |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 197 | static object * |
| 198 | mac_dup(self, args) |
| 199 | object *self; |
| 200 | object *args; |
| 201 | { |
| 202 | int fd; |
| 203 | if (!getargs(args, "i", &fd)) |
| 204 | return NULL; |
| 205 | BGN_SAVE |
| 206 | fd = dup(fd); |
| 207 | END_SAVE |
| 208 | if (fd < 0) |
| 209 | return mac_error(); |
| 210 | return newintobject((long)fd); |
| 211 | } |
| 212 | |
Guido van Rossum | e783444 | 1994-08-26 09:09:48 +0000 | [diff] [blame] | 213 | #endif /* MPW */ |
| 214 | |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 215 | #ifndef __MWERKS__ |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 216 | static object * |
| 217 | mac_fdopen(self, args) |
| 218 | object *self; |
| 219 | object *args; |
| 220 | { |
| 221 | extern int fclose PROTO((FILE *)); |
| 222 | int fd; |
| 223 | char *mode; |
| 224 | FILE *fp; |
| 225 | if (!getargs(args, "(is)", &fd, &mode)) |
| 226 | return NULL; |
| 227 | BGN_SAVE |
| 228 | fp = fdopen(fd, mode); |
| 229 | END_SAVE |
| 230 | if (fp == NULL) |
| 231 | return mac_error(); |
| 232 | return newopenfileobject(fp, "(fdopen)", mode, fclose); |
| 233 | } |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 234 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 235 | |
| 236 | static object * |
| 237 | mac_getbootvol(self, args) |
| 238 | object *self; |
| 239 | object *args; |
| 240 | { |
| 241 | char *res; |
| 242 | if (!getnoarg(args)) |
| 243 | return NULL; |
| 244 | BGN_SAVE |
| 245 | res = getbootvol(); |
| 246 | END_SAVE |
| 247 | if (res == NULL) |
| 248 | return mac_error(); |
| 249 | return newstringobject(res); |
| 250 | } |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 251 | |
| 252 | static object * |
| 253 | mac_getcwd(self, args) |
| 254 | object *self; |
| 255 | object *args; |
| 256 | { |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 257 | char path[MAXPATHLEN]; |
| 258 | char *res; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 259 | if (!getnoarg(args)) |
| 260 | return NULL; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 261 | BGN_SAVE |
| 262 | res = getwd(path); |
| 263 | END_SAVE |
| 264 | if (res == NULL) { |
| 265 | err_setstr(MacError, path); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 268 | return newstringobject(res); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 271 | static object * |
| 272 | mac_listdir(self, args) |
| 273 | object *self; |
| 274 | object *args; |
| 275 | { |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 276 | char *name; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 277 | object *d, *v; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 278 | DIR *dirp; |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 279 | struct dirent *ep; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 280 | if (!getargs(args, "s", &name)) |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 281 | return NULL; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 282 | BGN_SAVE |
| 283 | if ((dirp = opendir(name)) == NULL) { |
| 284 | RET_SAVE |
| 285 | return mac_error(); |
| 286 | } |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 287 | if ((d = newlistobject(0)) == NULL) { |
| 288 | closedir(dirp); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 289 | RET_SAVE |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 290 | return NULL; |
| 291 | } |
| 292 | while ((ep = readdir(dirp)) != NULL) { |
| 293 | v = newstringobject(ep->d_name); |
| 294 | if (v == NULL) { |
| 295 | DECREF(d); |
| 296 | d = NULL; |
| 297 | break; |
| 298 | } |
| 299 | if (addlistitem(d, v) != 0) { |
| 300 | DECREF(v); |
| 301 | DECREF(d); |
| 302 | d = NULL; |
| 303 | break; |
| 304 | } |
| 305 | DECREF(v); |
| 306 | } |
| 307 | closedir(dirp); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 308 | END_SAVE |
| 309 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 310 | return d; |
| 311 | } |
| 312 | |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 313 | #ifndef CW4 |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 314 | static object * |
| 315 | mac_lseek(self, args) |
| 316 | object *self; |
| 317 | object *args; |
| 318 | { |
| 319 | int fd; |
| 320 | int where; |
| 321 | int how; |
| 322 | long res; |
| 323 | if (!getargs(args, "(iii)", &fd, &where, &how)) |
| 324 | return NULL; |
| 325 | BGN_SAVE |
| 326 | res = lseek(fd, (long)where, how); |
| 327 | END_SAVE |
| 328 | if (res < 0) |
| 329 | return mac_error(); |
| 330 | return newintobject(res); |
| 331 | } |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 332 | #endif /* !CW4 */ |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 333 | |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 334 | #ifdef USE_GUSI |
| 335 | /* GUSI mkdir doesn't accept the (dummy) mode. Grrr. */ |
| 336 | int _gusi_mkdir(name, mode) |
| 337 | char *name; |
| 338 | int mode; |
| 339 | { |
| 340 | return mkdir(name); |
| 341 | } |
| 342 | #endif /* USE_GUSI */ |
| 343 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 344 | static object * |
| 345 | mac_mkdir(self, args) |
| 346 | object *self; |
| 347 | object *args; |
| 348 | { |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 349 | #ifdef USE_GUSI |
| 350 | return mac_strint(args, _gusi_mkdir); |
| 351 | #else |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 352 | return mac_strint(args, mkdir); |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 353 | #endif |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 356 | #ifndef CW4 |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 357 | static object * |
| 358 | mac_open(self, args) |
| 359 | object *self; |
| 360 | object *args; |
| 361 | { |
| 362 | char *path; |
| 363 | int mode; |
| 364 | int fd; |
| 365 | if (!getargs(args, "(si)", &path, &mode)) |
| 366 | return NULL; |
| 367 | BGN_SAVE |
| 368 | fd = open(path, mode); |
| 369 | END_SAVE |
| 370 | if (fd < 0) |
| 371 | return mac_error(); |
| 372 | return newintobject((long)fd); |
| 373 | } |
| 374 | |
| 375 | static object * |
| 376 | mac_read(self, args) |
| 377 | object *self; |
| 378 | object *args; |
| 379 | { |
| 380 | int fd, size; |
| 381 | object *buffer; |
| 382 | if (!getargs(args, "(ii)", &fd, &size)) |
| 383 | return NULL; |
| 384 | buffer = newsizedstringobject((char *)NULL, size); |
| 385 | if (buffer == NULL) |
| 386 | return NULL; |
| 387 | BGN_SAVE |
| 388 | size = read(fd, getstringvalue(buffer), size); |
| 389 | END_SAVE |
| 390 | if (size < 0) { |
| 391 | DECREF(buffer); |
| 392 | return mac_error(); |
| 393 | } |
| 394 | resizestring(&buffer, size); |
| 395 | return buffer; |
| 396 | } |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 397 | #endif /* !__MWERKS */ |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 398 | |
| 399 | static object * |
| 400 | mac_rename(self, args) |
| 401 | object *self; |
| 402 | object *args; |
| 403 | { |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 404 | return mac_2str(args, rename); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 407 | static object * |
| 408 | mac_rmdir(self, args) |
| 409 | object *self; |
| 410 | object *args; |
| 411 | { |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 412 | return mac_1str(args, rmdir); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 415 | static object * |
| 416 | mac_stat(self, args) |
| 417 | object *self; |
| 418 | object *args; |
| 419 | { |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 420 | struct macstat st; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 421 | char *path; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 422 | int res; |
| 423 | if (!getargs(args, "s", &path)) |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 424 | return NULL; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 425 | BGN_SAVE |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 426 | res = macstat(path, &st); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 427 | END_SAVE |
| 428 | if (res != 0) |
| 429 | return mac_error(); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 430 | return mkvalue("(llllllllll)", |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 431 | (long)st.st_mode, |
Guido van Rossum | e783444 | 1994-08-26 09:09:48 +0000 | [diff] [blame] | 432 | (long)st.st_ino, |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 433 | (long)st.st_dev, |
| 434 | (long)st.st_nlink, |
| 435 | (long)st.st_uid, |
| 436 | (long)st.st_gid, |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 437 | (long)st.st_size, |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 438 | (long)st.st_atime, |
| 439 | (long)st.st_mtime, |
| 440 | (long)st.st_ctime); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 443 | static object * |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 444 | mac_xstat(self, args) |
| 445 | object *self; |
| 446 | object *args; |
| 447 | { |
| 448 | struct macstat st; |
| 449 | char *path; |
| 450 | int res; |
| 451 | if (!getargs(args, "s", &path)) |
| 452 | return NULL; |
| 453 | BGN_SAVE |
| 454 | res = macstat(path, &st); |
| 455 | END_SAVE |
| 456 | if (res != 0) |
| 457 | return mac_error(); |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 458 | #ifdef USE_GUSI |
| 459 | return mkvalue("(llllllllll)", |
| 460 | (long)st.st_mode, |
| 461 | (long)st.st_ino, |
| 462 | (long)st.st_dev, |
| 463 | (long)st.st_nlink, |
| 464 | (long)st.st_uid, |
| 465 | (long)st.st_gid, |
| 466 | (long)st.st_size, |
| 467 | (long)st.st_atime, |
| 468 | (long)st.st_mtime, |
| 469 | (long)st.st_ctime); |
| 470 | #else |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 471 | return mkvalue("(llllllllllls#s#)", |
| 472 | (long)st.st_mode, |
| 473 | (long)st.st_ino, |
| 474 | (long)st.st_dev, |
| 475 | (long)st.st_nlink, |
| 476 | (long)st.st_uid, |
| 477 | (long)st.st_gid, |
| 478 | (long)st.st_size, |
| 479 | (long)st.st_atime, |
| 480 | (long)st.st_mtime, |
| 481 | (long)st.st_ctime, |
| 482 | (long)st.st_rsize, |
| 483 | st.st_creator, 4, |
| 484 | st.st_type, 4); |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame^] | 485 | #endif |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static object * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 489 | mac_sync(self, args) |
| 490 | object *self; |
| 491 | object *args; |
| 492 | { |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 493 | int res; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 494 | if (!getnoarg(args)) |
| 495 | return NULL; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 496 | BGN_SAVE |
| 497 | res = sync(); |
| 498 | END_SAVE |
| 499 | if (res != 0) |
| 500 | return mac_error(); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 501 | INCREF(None); |
| 502 | return None; |
| 503 | } |
| 504 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 505 | static object * |
| 506 | mac_unlink(self, args) |
| 507 | object *self; |
| 508 | object *args; |
| 509 | { |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 510 | return mac_1str(args, (int (*)(const char *))unlink); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 513 | #ifndef CW4 |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 514 | static object * |
| 515 | mac_write(self, args) |
| 516 | object *self; |
| 517 | object *args; |
| 518 | { |
| 519 | int fd, size; |
| 520 | char *buffer; |
| 521 | if (!getargs(args, "(is#)", &fd, &buffer, &size)) |
| 522 | return NULL; |
| 523 | BGN_SAVE |
| 524 | size = write(fd, buffer, size); |
| 525 | END_SAVE |
| 526 | if (size < 0) |
| 527 | return mac_error(); |
| 528 | return newintobject((long)size); |
| 529 | } |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 530 | #endif /* !__MWERKS__ */ |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 531 | |
Jack Jansen | d50e4e1 | 1995-01-18 13:58:04 +0000 | [diff] [blame] | 532 | #ifdef MALLOC_DEBUG |
| 533 | static object * |
| 534 | mac_mstats(self, args) |
| 535 | object*self; |
| 536 | object *args; |
| 537 | { |
| 538 | mstats("python"); |
| 539 | INCREF(None); |
| 540 | return None; |
| 541 | } |
| 542 | #endif MALLOC_DEBUG |
| 543 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 544 | static struct methodlist mac_methods[] = { |
| 545 | {"chdir", mac_chdir}, |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 546 | #ifndef CW4 |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 547 | {"close", mac_close}, |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 548 | #endif |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 549 | #ifdef MPW |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 550 | {"dup", mac_dup}, |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 551 | #endif |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 552 | #ifndef __MWERKS__ |
Guido van Rossum | e783444 | 1994-08-26 09:09:48 +0000 | [diff] [blame] | 553 | {"fdopen", mac_fdopen}, |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 554 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 555 | {"getbootvol", mac_getbootvol}, /* non-standard */ |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 556 | {"getcwd", mac_getcwd}, |
Guido van Rossum | d1ef596 | 1995-02-19 15:50:35 +0000 | [diff] [blame] | 557 | {"listdir", mac_listdir, 0}, |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 558 | #ifndef CW4 |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 559 | {"lseek", mac_lseek}, |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 560 | #endif |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 561 | {"mkdir", mac_mkdir}, |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 562 | #ifndef CW4 |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 563 | {"open", mac_open}, |
| 564 | {"read", mac_read}, |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 565 | #endif |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 566 | {"rename", mac_rename}, |
| 567 | {"rmdir", mac_rmdir}, |
| 568 | {"stat", mac_stat}, |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 569 | {"xstat", mac_xstat}, |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 570 | {"sync", mac_sync}, |
| 571 | {"unlink", mac_unlink}, |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 572 | #ifndef CW4 |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 573 | {"write", mac_write}, |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 574 | #endif |
Jack Jansen | d50e4e1 | 1995-01-18 13:58:04 +0000 | [diff] [blame] | 575 | #ifdef MALLOC_DEBUG |
| 576 | {"mstats", mac_mstats}, |
| 577 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 578 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 579 | {NULL, NULL} /* Sentinel */ |
| 580 | }; |
| 581 | |
| 582 | |
| 583 | void |
| 584 | initmac() |
| 585 | { |
Jack Jansen | d50e4e1 | 1995-01-18 13:58:04 +0000 | [diff] [blame] | 586 | object *m, *d; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 587 | |
| 588 | m = initmodule("mac", mac_methods); |
| 589 | d = getmoduledict(m); |
| 590 | |
| 591 | /* Initialize mac.error exception */ |
| 592 | MacError = newstringobject("mac.error"); |
| 593 | if (MacError == NULL || dictinsert(d, "error", MacError) != 0) |
| 594 | fatal("can't define mac.error"); |
| 595 | } |