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"
48 #include <sys/types.h>
61 /* Optional routines, for some compiler/runtime combinations */
62 #if defined(__MWERKS__) && defined(__powerc)
65 #if defined(USE_GUSI) || !defined(__MWERKS__)
68 #if defined(MPW) || defined(USE_GUSI)
80 #define MAXPATHLEN 1024
83 /* Prototypes for Unix simulation on Mac */
87 int chdir
PROTO((const char *path
));
88 int mkdir
PROTO((const char *path
, int mode
));
89 DIR * opendir
PROTO((char *));
90 void closedir
PROTO((DIR *));
91 struct dirent
* readdir
PROTO((DIR *));
92 int rmdir
PROTO((const char *path
));
93 int sync
PROTO((void));
95 #if defined(THINK_C) || defined(__SC__)
96 int unlink
PROTO((char *));
98 int unlink
PROTO((const char *));
101 #endif /* USE_GUSI */
103 char *getwd
PROTO((char *));
104 char *getbootvol
PROTO((void));
107 static object
*MacError
; /* Exception mac.error */
109 /* Set a MAC-specific error from errno, and return NULL */
114 return err_errno(MacError
);
117 /* MAC generic methods */
122 int (*func
) FPROTO((const char *));
126 if (!getargs(args
, "s", &path1
))
129 res
= (*func
)(path1
);
140 int (*func
) FPROTO((const char *, const char *));
144 if (!getargs(args
, "(ss)", &path1
, &path2
))
147 res
= (*func
)(path1
, path2
);
156 mac_strint(args
, func
)
158 int (*func
) FPROTO((const char *, int));
163 if (!getargs(args
, "(si)", &path
, &i
))
166 res
= (*func
)(path
, i
);
175 mac_chdir(self
, args
)
182 /* Change MacOS's idea of wd too */
183 rv
= mac_1str(args
, chdir
);
187 return mac_1str(args
, chdir
);
193 mac_close(self
, args
)
198 if (!getargs(args
, "i", &fd
))
204 /* GUSI gives surious errors here? */
220 if (!getargs(args
, "i", &fd
))
227 return newintobject((long)fd
);
234 mac_fdopen(self
, args
)
238 extern int fclose
PROTO((FILE *));
242 if (!getargs(args
, "(is)", &fd
, &mode
))
245 fp
= fdopen(fd
, mode
);
249 return newopenfileobject(fp
, "(fdopen)", mode
, fclose
);
254 mac_getbootvol(self
, args
)
266 return newstringobject(res
);
270 mac_getcwd(self
, args
)
274 char path
[MAXPATHLEN
];
280 res
= getcwd(path
, sizeof path
);
286 err_setstr(MacError
, path
);
289 return newstringobject(res
);
293 mac_listdir(self
, args
)
301 if (!getargs(args
, "s", &name
))
304 if ((dirp
= opendir(name
)) == NULL
) {
308 if ((d
= newlistobject(0)) == NULL
) {
313 while ((ep
= readdir(dirp
)) != NULL
) {
314 v
= newstringobject(ep
->d_name
);
320 if (addlistitem(d
, v
) != 0) {
335 mac_lseek(self
, args
)
343 if (!getargs(args
, "(iii)", &fd
, &where
, &how
))
346 res
= lseek(fd
, (long)where
, how
);
350 return newintobject(res
);
354 mac_mkdir(self
, args
)
360 int mode
= 0777; /* Unused */
361 if (!newgetargs(args
, "s|i", &path
, &mode
))
367 res
= mkdir(path
, mode
);
384 if (!getargs(args
, "(si)", &path
, &mode
))
387 fd
= open(path
, mode
);
391 return newintobject((long)fd
);
401 if (!getargs(args
, "(ii)", &fd
, &size
))
403 buffer
= newsizedstringobject((char *)NULL
, size
);
407 size
= read(fd
, getstringvalue(buffer
), size
);
413 resizestring(&buffer
, size
);
418 mac_rename(self
, args
)
422 return mac_2str(args
, rename
);
426 mac_rmdir(self
, args
)
430 return mac_1str(args
, rmdir
);
441 if (!getargs(args
, "s", &path
))
444 res
= macstat(path
, &st
);
448 return mkvalue("(llllllllll)",
462 mac_xstat(self
, args
)
469 if (!getargs(args
, "s", &path
))
472 res
= macstat(path
, &st
);
477 return mkvalue("(llllllllll)",
489 return mkvalue("(llllllllllls#s#)",
524 mac_unlink(self
, args
)
528 return mac_1str(args
, (int (*)(const char *))unlink
);
532 mac_write(self
, args
)
538 if (!getargs(args
, "(is#)", &fd
, &buffer
, &size
))
541 size
= write(fd
, buffer
, size
);
545 return newintobject((long)size
);
550 mac_mstats(self
, args
)
560 static struct methodlist mac_methods
[] = {
561 {"chdir", mac_chdir
},
562 {"close", mac_close
},
567 {"fdopen", mac_fdopen
},
569 {"getbootvol", mac_getbootvol
}, /* non-standard */
570 {"getcwd", mac_getcwd
},
571 {"listdir", mac_listdir
, 0},
572 {"lseek", mac_lseek
},
573 {"mkdir", mac_mkdir
, 1},
576 {"rename", mac_rename
},
577 {"rmdir", mac_rmdir
},
579 {"xstat", mac_xstat
},
581 {"remove", mac_unlink
},
582 {"unlink", mac_unlink
},
583 {"write", mac_write
},
585 {"mstats", mac_mstats
},
588 {NULL
, NULL
} /* Sentinel */
597 m
= initmodule("mac", mac_methods
);
598 d
= getmoduledict(m
);
600 /* Initialize mac.error exception */
601 MacError
= newstringobject("mac.error");
602 if (MacError
== NULL
|| dictinsert(d
, "error", MacError
) != 0)
603 fatal("can't define mac.error");