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