changes by Barry, e.g. font lock & email addresses
[python/dscho.git] / Mac / Modules / macmodule.c
blob16ab2ffef366cce1de82b380e4dbb8f7a6811adc
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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.
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.
23 ******************************************************************/
25 /* Mac module implementation */
27 #include "allobjects.h"
28 #include "modsupport.h"
29 #include "ceval.h"
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
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
46 #include "macstat.h"
48 #ifdef __MWERKS__
49 /* For CodeWarrior 4 also define CW4 */
50 #include <unix.h>
51 #else
52 #include <fcntl.h>
53 #endif
55 #if defined(__MWERKS__) && defined(__powerc)
56 #define MALLOC_DEBUG
57 #endif
59 #include "macdefs.h"
60 #include "dirent.h"
62 #ifndef MAXPATHLEN
63 #define MAXPATHLEN 1024
64 #endif
66 /* Prototypes for Unix simulation on Mac */
68 int chdir PROTO((const char *path));
69 char *getbootvol PROTO((void));
70 char *getwd PROTO((char *));
71 int mkdir PROTO((const char *path, int mode));
72 DIR * opendir PROTO((char *));
73 void closedir PROTO((DIR *));
74 struct dirent * readdir PROTO((DIR *));
75 int rmdir PROTO((const char *path));
76 int sync PROTO((void));
77 #if defined(THINK_C) || defined(__SC__)
78 int unlink PROTO((char *));
79 #else
80 int unlink PROTO((const char *));
81 #endif
85 static object *MacError; /* Exception mac.error */
87 /* Set a MAC-specific error from errno, and return NULL */
89 static object *
90 mac_error()
92 return err_errno(MacError);
95 /* MAC generic methods */
97 static object *
98 mac_1str(args, func)
99 object *args;
100 int (*func) FPROTO((const char *));
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;
115 static object *
116 mac_2str(args, func)
117 object *args;
118 int (*func) FPROTO((const char *, const char *));
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;
133 static object *
134 mac_strint(args, func)
135 object *args;
136 int (*func) FPROTO((const char *, int));
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;
152 static object *
153 mac_chdir(self, args)
154 object *self;
155 object *args;
157 return mac_1str(args, chdir);
160 #ifndef CW4
161 static object *
162 mac_close(self, args)
163 object *self;
164 object *args;
166 int fd, res;
167 if (!getargs(args, "i", &fd))
168 return NULL;
169 BGN_SAVE
170 res = close(fd);
171 END_SAVE
172 if (res < 0)
173 return mac_error();
174 INCREF(None);
175 return None;
177 #endif /* !__MWERKS__ */
179 #ifdef MPW
181 static object *
182 mac_dup(self, args)
183 object *self;
184 object *args;
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);
197 #endif /* MPW */
199 #ifndef __MWERKS__
200 static object *
201 mac_fdopen(self, args)
202 object *self;
203 object *args;
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);
218 #endif
220 static object *
221 mac_getbootvol(self, args)
222 object *self;
223 object *args;
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);
236 static object *
237 mac_getcwd(self, args)
238 object *self;
239 object *args;
241 char path[MAXPATHLEN];
242 char *res;
243 if (!getnoarg(args))
244 return NULL;
245 BGN_SAVE
246 res = getwd(path);
247 END_SAVE
248 if (res == NULL) {
249 err_setstr(MacError, path);
250 return NULL;
252 return newstringobject(res);
255 static object *
256 mac_listdir(self, args)
257 object *self;
258 object *args;
260 char *name;
261 object *d, *v;
262 DIR *dirp;
263 struct dirent *ep;
264 if (!getargs(args, "s", &name))
265 return NULL;
266 BGN_SAVE
267 if ((dirp = opendir(name)) == NULL) {
268 RET_SAVE
269 return mac_error();
271 if ((d = newlistobject(0)) == NULL) {
272 closedir(dirp);
273 RET_SAVE
274 return NULL;
276 while ((ep = readdir(dirp)) != NULL) {
277 v = newstringobject(ep->d_name);
278 if (v == NULL) {
279 DECREF(d);
280 d = NULL;
281 break;
283 if (addlistitem(d, v) != 0) {
284 DECREF(v);
285 DECREF(d);
286 d = NULL;
287 break;
289 DECREF(v);
291 closedir(dirp);
292 END_SAVE
294 return d;
297 #ifndef CW4
298 static object *
299 mac_lseek(self, args)
300 object *self;
301 object *args;
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);
316 #endif /* !CW4 */
318 static object *
319 mac_mkdir(self, args)
320 object *self;
321 object *args;
323 return mac_strint(args, mkdir);
326 #ifndef CW4
327 static object *
328 mac_open(self, args)
329 object *self;
330 object *args;
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);
345 static object *
346 mac_read(self, args)
347 object *self;
348 object *args;
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();
364 resizestring(&buffer, size);
365 return buffer;
367 #endif /* !__MWERKS */
369 static object *
370 mac_rename(self, args)
371 object *self;
372 object *args;
374 return mac_2str(args, rename);
377 static object *
378 mac_rmdir(self, args)
379 object *self;
380 object *args;
382 return mac_1str(args, rmdir);
385 static object *
386 mac_stat(self, args)
387 object *self;
388 object *args;
390 struct macstat st;
391 char *path;
392 int res;
393 if (!getargs(args, "s", &path))
394 return NULL;
395 BGN_SAVE
396 res = macstat(path, &st);
397 END_SAVE
398 if (res != 0)
399 return mac_error();
400 return mkvalue("(llllllllll)",
401 (long)st.st_mode,
402 (long)st.st_ino,
403 (long)st.st_dev,
404 (long)st.st_nlink,
405 (long)st.st_uid,
406 (long)st.st_gid,
407 (long)st.st_size,
408 (long)st.st_atime,
409 (long)st.st_mtime,
410 (long)st.st_ctime);
413 static object *
414 mac_xstat(self, args)
415 object *self;
416 object *args;
418 struct macstat st;
419 char *path;
420 int res;
421 if (!getargs(args, "s", &path))
422 return NULL;
423 BGN_SAVE
424 res = macstat(path, &st);
425 END_SAVE
426 if (res != 0)
427 return mac_error();
428 return mkvalue("(llllllllllls#s#)",
429 (long)st.st_mode,
430 (long)st.st_ino,
431 (long)st.st_dev,
432 (long)st.st_nlink,
433 (long)st.st_uid,
434 (long)st.st_gid,
435 (long)st.st_size,
436 (long)st.st_atime,
437 (long)st.st_mtime,
438 (long)st.st_ctime,
439 (long)st.st_rsize,
440 st.st_creator, 4,
441 st.st_type, 4);
444 static object *
445 mac_sync(self, args)
446 object *self;
447 object *args;
449 int res;
450 if (!getnoarg(args))
451 return NULL;
452 BGN_SAVE
453 res = sync();
454 END_SAVE
455 if (res != 0)
456 return mac_error();
457 INCREF(None);
458 return None;
461 static object *
462 mac_unlink(self, args)
463 object *self;
464 object *args;
466 return mac_1str(args, (int (*)(const char *))unlink);
469 #ifndef CW4
470 static object *
471 mac_write(self, args)
472 object *self;
473 object *args;
475 int fd, size;
476 char *buffer;
477 if (!getargs(args, "(is#)", &fd, &buffer, &size))
478 return NULL;
479 BGN_SAVE
480 size = write(fd, buffer, size);
481 END_SAVE
482 if (size < 0)
483 return mac_error();
484 return newintobject((long)size);
486 #endif /* !__MWERKS__ */
488 #ifdef MALLOC_DEBUG
489 static object *
490 mac_mstats(self, args)
491 object*self;
492 object *args;
494 mstats("python");
495 INCREF(None);
496 return None;
498 #endif MALLOC_DEBUG
500 static struct methodlist mac_methods[] = {
501 {"chdir", mac_chdir},
502 #ifndef CW4
503 {"close", mac_close},
504 #endif
505 #ifdef MPW
506 {"dup", mac_dup},
507 #endif
508 #ifndef __MWERKS__
509 {"fdopen", mac_fdopen},
510 #endif
511 {"getbootvol", mac_getbootvol}, /* non-standard */
512 {"getcwd", mac_getcwd},
513 {"listdir", mac_listdir, 0},
514 #ifndef CW4
515 {"lseek", mac_lseek},
516 #endif
517 {"mkdir", mac_mkdir},
518 #ifndef CW4
519 {"open", mac_open},
520 {"read", mac_read},
521 #endif
522 {"rename", mac_rename},
523 {"rmdir", mac_rmdir},
524 {"stat", mac_stat},
525 {"xstat", mac_xstat},
526 {"sync", mac_sync},
527 {"unlink", mac_unlink},
528 #ifndef CW4
529 {"write", mac_write},
530 #endif
531 #ifdef MALLOC_DEBUG
532 {"mstats", mac_mstats},
533 #endif
535 {NULL, NULL} /* Sentinel */
539 void
540 initmac()
542 object *m, *d;
544 m = initmodule("mac", mac_methods);
545 d = getmoduledict(m);
547 /* Initialize mac.error exception */
548 MacError = newstringobject("mac.error");
549 if (MacError == NULL || dictinsert(d, "error", MacError) != 0)
550 fatal("can't define mac.error");