blob: 6b5668b98cd59c95193733d4c5d6727be82b708d [file] [log] [blame]
Guido van Rossumce9739b1994-01-05 16:17:15 +00001/***********************************************************
Guido van Rossum99546991995-01-08 14:33:34 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumce9739b1994-01-05 16:17:15 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum87f223c1994-05-06 15:54:15 +000025/* Mac module implementation */
Guido van Rossumce9739b1994-01-05 16:17:15 +000026
27#include "allobjects.h"
Guido van Rossumce9739b1994-01-05 16:17:15 +000028#include "modsupport.h"
Guido van Rossum87f223c1994-05-06 15:54:15 +000029#include "ceval.h"
Guido van Rossumce9739b1994-01-05 16:17:15 +000030
Guido van Rossum87f223c1994-05-06 15:54:15 +000031#include <stdio.h>
32#include <string.h>
33#include <errno.h>
Guido van Rossum739267b1994-08-29 08:42:37 +000034
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 Rossumce9739b1994-01-05 16:17:15 +000047
Jack Jansen9eeb82d1995-01-26 16:34:53 +000048#ifdef __MWERKS__
49/* For CodeWarrior 4 also define CW4 */
50#include <unix.h>
51#else
Guido van Rossum87f223c1994-05-06 15:54:15 +000052#include <fcntl.h>
Jack Jansen0c097ea1994-12-14 13:48:38 +000053#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000054
Guido van Rossum56934121995-02-13 16:19:04 +000055#if defined(__MWERKS__) && defined(__powerc)
Jack Jansen7cbf4801995-01-22 16:52:38 +000056#define MALLOC_DEBUG
57#endif
58
Guido van Rossumd4d77281994-08-19 10:51:31 +000059#include "macdefs.h"
60#include "dirent.h"
Guido van Rossum87f223c1994-05-06 15:54:15 +000061
62#ifndef MAXPATHLEN
63#define MAXPATHLEN 1024
64#endif
65
66/* Prototypes for Unix simulation on Mac */
67
Guido van Rossum921a08f1994-05-06 15:56:22 +000068int chdir PROTO((const char *path));
Guido van Rossum87f223c1994-05-06 15:54:15 +000069char *getbootvol PROTO((void));
70char *getwd PROTO((char *));
Guido van Rossum921a08f1994-05-06 15:56:22 +000071int mkdir PROTO((const char *path, int mode));
72DIR * opendir PROTO((char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +000073void closedir PROTO((DIR *));
Guido van Rossumd4d77281994-08-19 10:51:31 +000074struct dirent * readdir PROTO((DIR *));
Guido van Rossum921a08f1994-05-06 15:56:22 +000075int rmdir PROTO((const char *path));
Guido van Rossum87f223c1994-05-06 15:54:15 +000076int sync PROTO((void));
Guido van Rossum739267b1994-08-29 08:42:37 +000077#ifdef THINK_C
78int unlink PROTO((char *));
79#else
Guido van Rossum921a08f1994-05-06 15:56:22 +000080int unlink PROTO((const char *));
Guido van Rossum739267b1994-08-29 08:42:37 +000081#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +000082
83
84
85static object *MacError; /* Exception mac.error */
86
87/* Set a MAC-specific error from errno, and return NULL */
88
89static object *
90mac_error()
91{
Guido van Rossum921a08f1994-05-06 15:56:22 +000092 return err_errno(MacError);
Guido van Rossum87f223c1994-05-06 15:54:15 +000093}
94
95/* MAC generic methods */
96
97static object *
98mac_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
115static object *
116mac_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
133static object *
134mac_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
152static object *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000153mac_chdir(self, args)
154 object *self;
155 object *args;
156{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000157 return mac_1str(args, chdir);
158}
159
Jack Jansen9eeb82d1995-01-26 16:34:53 +0000160#ifndef CW4
Guido van Rossum87f223c1994-05-06 15:54:15 +0000161static object *
162mac_close(self, args)
163 object *self;
164 object *args;
165{
166 int fd, res;
167 if (!getargs(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000168 return NULL;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000169 BGN_SAVE
170 res = close(fd);
171 END_SAVE
172 if (res < 0)
173 return mac_error();
Guido van Rossumce9739b1994-01-05 16:17:15 +0000174 INCREF(None);
175 return None;
176}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000177#endif /* !__MWERKS__ */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000178
Guido van Rossum921a08f1994-05-06 15:56:22 +0000179#ifdef MPW
180
Guido van Rossum87f223c1994-05-06 15:54:15 +0000181static object *
182mac_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 Rossume7834441994-08-26 09:09:48 +0000197#endif /* MPW */
198
Jack Jansen0c097ea1994-12-14 13:48:38 +0000199#ifndef __MWERKS__
Guido van Rossum87f223c1994-05-06 15:54:15 +0000200static object *
201mac_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 Jansen0c097ea1994-12-14 13:48:38 +0000218#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000219
220static object *
221mac_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 Rossumce9739b1994-01-05 16:17:15 +0000235
236static object *
237mac_getcwd(self, args)
238 object *self;
239 object *args;
240{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000241 char path[MAXPATHLEN];
242 char *res;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000243 if (!getnoarg(args))
244 return NULL;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000245 BGN_SAVE
246 res = getwd(path);
247 END_SAVE
248 if (res == NULL) {
249 err_setstr(MacError, path);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000250 return NULL;
251 }
Guido van Rossum87f223c1994-05-06 15:54:15 +0000252 return newstringobject(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000253}
254
Guido van Rossumce9739b1994-01-05 16:17:15 +0000255static object *
256mac_listdir(self, args)
257 object *self;
258 object *args;
259{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000260 char *name;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000261 object *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000262 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000263 struct dirent *ep;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000264 if (!getargs(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000265 return NULL;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000266 BGN_SAVE
267 if ((dirp = opendir(name)) == NULL) {
268 RET_SAVE
269 return mac_error();
270 }
Guido van Rossumce9739b1994-01-05 16:17:15 +0000271 if ((d = newlistobject(0)) == NULL) {
272 closedir(dirp);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000273 RET_SAVE
Guido van Rossumce9739b1994-01-05 16:17:15 +0000274 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 Rossum87f223c1994-05-06 15:54:15 +0000292 END_SAVE
293
Guido van Rossumce9739b1994-01-05 16:17:15 +0000294 return d;
295}
296
Jack Jansen9eeb82d1995-01-26 16:34:53 +0000297#ifndef CW4
Guido van Rossum87f223c1994-05-06 15:54:15 +0000298static object *
299mac_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 Jansen9eeb82d1995-01-26 16:34:53 +0000316#endif /* !CW4 */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000317
318static object *
319mac_mkdir(self, args)
320 object *self;
321 object *args;
322{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000323 return mac_strint(args, mkdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000324}
325
Jack Jansen9eeb82d1995-01-26 16:34:53 +0000326#ifndef CW4
Guido van Rossum87f223c1994-05-06 15:54:15 +0000327static object *
328mac_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
345static object *
346mac_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 Jansen0c097ea1994-12-14 13:48:38 +0000367#endif /* !__MWERKS */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000368
369static object *
370mac_rename(self, args)
371 object *self;
372 object *args;
373{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000374 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000375}
376
Guido van Rossumce9739b1994-01-05 16:17:15 +0000377static object *
378mac_rmdir(self, args)
379 object *self;
380 object *args;
381{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000382 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000383}
384
Guido van Rossumce9739b1994-01-05 16:17:15 +0000385static object *
386mac_stat(self, args)
387 object *self;
388 object *args;
389{
Guido van Rossum739267b1994-08-29 08:42:37 +0000390 struct macstat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000391 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000392 int res;
393 if (!getargs(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000394 return NULL;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000395 BGN_SAVE
Guido van Rossum739267b1994-08-29 08:42:37 +0000396 res = macstat(path, &st);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000397 END_SAVE
398 if (res != 0)
399 return mac_error();
Guido van Rossumd4d77281994-08-19 10:51:31 +0000400 return mkvalue("(llllllllll)",
Guido van Rossum87f223c1994-05-06 15:54:15 +0000401 (long)st.st_mode,
Guido van Rossume7834441994-08-26 09:09:48 +0000402 (long)st.st_ino,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000403 (long)st.st_dev,
404 (long)st.st_nlink,
405 (long)st.st_uid,
406 (long)st.st_gid,
Guido van Rossum87f223c1994-05-06 15:54:15 +0000407 (long)st.st_size,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000408 (long)st.st_atime,
409 (long)st.st_mtime,
410 (long)st.st_ctime);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000411}
412
Guido van Rossumce9739b1994-01-05 16:17:15 +0000413static object *
414mac_sync(self, args)
415 object *self;
416 object *args;
417{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000418 int res;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000419 if (!getnoarg(args))
420 return NULL;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000421 BGN_SAVE
422 res = sync();
423 END_SAVE
424 if (res != 0)
425 return mac_error();
Guido van Rossumce9739b1994-01-05 16:17:15 +0000426 INCREF(None);
427 return None;
428}
429
Guido van Rossumce9739b1994-01-05 16:17:15 +0000430static object *
431mac_unlink(self, args)
432 object *self;
433 object *args;
434{
Guido van Rossum739267b1994-08-29 08:42:37 +0000435 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000436}
437
Jack Jansen9eeb82d1995-01-26 16:34:53 +0000438#ifndef CW4
Guido van Rossum87f223c1994-05-06 15:54:15 +0000439static object *
440mac_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 Jansen0c097ea1994-12-14 13:48:38 +0000455#endif /* !__MWERKS__ */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000456
Jack Jansend50e4e11995-01-18 13:58:04 +0000457#ifdef MALLOC_DEBUG
458static object *
459mac_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 Rossumce9739b1994-01-05 16:17:15 +0000469static struct methodlist mac_methods[] = {
470 {"chdir", mac_chdir},
Jack Jansen9eeb82d1995-01-26 16:34:53 +0000471#ifndef CW4
Guido van Rossum87f223c1994-05-06 15:54:15 +0000472 {"close", mac_close},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000473#endif
Guido van Rossum921a08f1994-05-06 15:56:22 +0000474#ifdef MPW
Guido van Rossum87f223c1994-05-06 15:54:15 +0000475 {"dup", mac_dup},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000476#endif
Jack Jansen0c097ea1994-12-14 13:48:38 +0000477#ifndef __MWERKS__
Guido van Rossume7834441994-08-26 09:09:48 +0000478 {"fdopen", mac_fdopen},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000479#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000480 {"getbootvol", mac_getbootvol}, /* non-standard */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000481 {"getcwd", mac_getcwd},
482 {"listdir", mac_listdir},
Jack Jansen9eeb82d1995-01-26 16:34:53 +0000483#ifndef CW4
Guido van Rossum87f223c1994-05-06 15:54:15 +0000484 {"lseek", mac_lseek},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000485#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000486 {"mkdir", mac_mkdir},
Jack Jansen9eeb82d1995-01-26 16:34:53 +0000487#ifndef CW4
Guido van Rossum87f223c1994-05-06 15:54:15 +0000488 {"open", mac_open},
489 {"read", mac_read},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000490#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000491 {"rename", mac_rename},
492 {"rmdir", mac_rmdir},
493 {"stat", mac_stat},
494 {"sync", mac_sync},
495 {"unlink", mac_unlink},
Jack Jansen9eeb82d1995-01-26 16:34:53 +0000496#ifndef CW4
Guido van Rossum87f223c1994-05-06 15:54:15 +0000497 {"write", mac_write},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000498#endif
Jack Jansend50e4e11995-01-18 13:58:04 +0000499#ifdef MALLOC_DEBUG
500 {"mstats", mac_mstats},
501#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000502
Guido van Rossumce9739b1994-01-05 16:17:15 +0000503 {NULL, NULL} /* Sentinel */
504};
505
506
507void
508initmac()
509{
Jack Jansend50e4e11995-01-18 13:58:04 +0000510 object *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000511
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}