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