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