1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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"
49 /* For CodeWarrior 4 also define CW4 */
55 #if defined(__MWERKS__) && defined(__powerc)
63 #define MAXPATHLEN 1024
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 *));
80 int unlink
PROTO((const char *));
85 static object
*MacError
; /* Exception mac.error */
87 /* Set a MAC-specific error from errno, and return NULL */
92 return err_errno(MacError
);
95 /* MAC generic methods */
100 int (*func
) FPROTO((const char *));
104 if (!getargs(args
, "s", &path1
))
107 res
= (*func
)(path1
);
118 int (*func
) FPROTO((const char *, const char *));
122 if (!getargs(args
, "(ss)", &path1
, &path2
))
125 res
= (*func
)(path1
, path2
);
134 mac_strint(args
, func
)
136 int (*func
) FPROTO((const char *, int));
141 if (!getargs(args
, "(si)", &path
, &i
))
144 res
= (*func
)(path
, i
);
153 mac_chdir(self
, args
)
157 return mac_1str(args
, chdir
);
162 mac_close(self
, args
)
167 if (!getargs(args
, "i", &fd
))
177 #endif /* !__MWERKS__ */
187 if (!getargs(args
, "i", &fd
))
194 return newintobject((long)fd
);
201 mac_fdopen(self
, args
)
205 extern int fclose
PROTO((FILE *));
209 if (!getargs(args
, "(is)", &fd
, &mode
))
212 fp
= fdopen(fd
, mode
);
216 return newopenfileobject(fp
, "(fdopen)", mode
, fclose
);
221 mac_getbootvol(self
, args
)
233 return newstringobject(res
);
237 mac_getcwd(self
, args
)
241 char path
[MAXPATHLEN
];
249 err_setstr(MacError
, path
);
252 return newstringobject(res
);
256 mac_listdir(self
, args
)
264 if (!getargs(args
, "s", &name
))
267 if ((dirp
= opendir(name
)) == NULL
) {
271 if ((d
= newlistobject(0)) == NULL
) {
276 while ((ep
= readdir(dirp
)) != NULL
) {
277 v
= newstringobject(ep
->d_name
);
283 if (addlistitem(d
, v
) != 0) {
299 mac_lseek(self
, args
)
307 if (!getargs(args
, "(iii)", &fd
, &where
, &how
))
310 res
= lseek(fd
, (long)where
, how
);
314 return newintobject(res
);
319 mac_mkdir(self
, args
)
323 return mac_strint(args
, mkdir
);
335 if (!getargs(args
, "(si)", &path
, &mode
))
338 fd
= open(path
, mode
);
342 return newintobject((long)fd
);
352 if (!getargs(args
, "(ii)", &fd
, &size
))
354 buffer
= newsizedstringobject((char *)NULL
, size
);
358 size
= read(fd
, getstringvalue(buffer
), size
);
364 resizestring(&buffer
, size
);
367 #endif /* !__MWERKS */
370 mac_rename(self
, args
)
374 return mac_2str(args
, rename
);
378 mac_rmdir(self
, args
)
382 return mac_1str(args
, rmdir
);
393 if (!getargs(args
, "s", &path
))
396 res
= macstat(path
, &st
);
400 return mkvalue("(llllllllll)",
414 mac_xstat(self
, args
)
421 if (!getargs(args
, "s", &path
))
424 res
= macstat(path
, &st
);
428 return mkvalue("(llllllllllls#s#)",
462 mac_unlink(self
, args
)
466 return mac_1str(args
, (int (*)(const char *))unlink
);
471 mac_write(self
, args
)
477 if (!getargs(args
, "(is#)", &fd
, &buffer
, &size
))
480 size
= write(fd
, buffer
, size
);
484 return newintobject((long)size
);
486 #endif /* !__MWERKS__ */
490 mac_mstats(self
, args
)
500 static struct methodlist mac_methods
[] = {
501 {"chdir", mac_chdir
},
503 {"close", mac_close
},
509 {"fdopen", mac_fdopen
},
511 {"getbootvol", mac_getbootvol
}, /* non-standard */
512 {"getcwd", mac_getcwd
},
513 {"listdir", mac_listdir
, 0},
515 {"lseek", mac_lseek
},
517 {"mkdir", mac_mkdir
},
522 {"rename", mac_rename
},
523 {"rmdir", mac_rmdir
},
525 {"xstat", mac_xstat
},
527 {"unlink", mac_unlink
},
529 {"write", mac_write
},
532 {"mstats", mac_mstats
},
535 {NULL
, NULL
} /* Sentinel */
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");