1 /***********************************************************
2 Copyright 1991-1997 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 */
34 #if !TARGET_API_MAC_CARBON_NOTYET
35 /* XXXX Skip for Carbon, for now */
40 /* Remove defines from macstat.h */
50 #endif /* USE_GUSI1 */
51 #include <sys/types.h>
54 #if !TARGET_API_MAC_CARBON_NOTYET
74 /* Optional routines, for some compiler/runtime combinations */
75 #if defined(USE_GUSI) || !defined(__MWERKS__)
78 #if defined(MPW) || defined(USE_GUSI)
93 #define MAXPATHLEN 1024
96 /* Prototypes for Unix simulation on Mac */
100 int chdir(const char *path
);
101 int mkdir(const char *path
, int mode
);
102 DIR * opendir(char *);
103 void closedir(DIR *);
104 struct dirent
* readdir(DIR *);
105 int rmdir(const char *path
);
108 int unlink(const char *);
110 #endif /* USE_GUSI */
113 char *getbootvol(void);
116 static PyObject
*MacError
; /* Exception mac.error */
118 /* Set a MAC-specific error from errno, and return NULL */
123 return PyErr_SetFromErrno(MacError
);
126 /* MAC generic methods */
131 int (*func
)(const char *);
135 if (!PyArg_Parse(args
, "s", &path1
))
137 Py_BEGIN_ALLOW_THREADS
138 res
= (*func
)(path1
);
149 int (*func
)(const char *, const char *);
153 if (!PyArg_Parse(args
, "(ss)", &path1
, &path2
))
155 Py_BEGIN_ALLOW_THREADS
156 res
= (*func
)(path1
, path2
);
165 mac_strint(args
, func
)
167 int (*func
)(const char *, int);
172 if (!PyArg_Parse(args
, "(si)", &path
, &i
))
174 Py_BEGIN_ALLOW_THREADS
175 res
= (*func
)(path
, i
);
184 mac_chdir(self
, args
)
191 /* Change MacOS's idea of wd too */
192 rv
= mac_1str(args
, chdir
);
196 return mac_1str(args
, chdir
);
202 mac_close(self
, args
)
207 if (!PyArg_Parse(args
, "i", &fd
))
209 Py_BEGIN_ALLOW_THREADS
213 /* GUSI gives surious errors here? */
229 if (!PyArg_Parse(args
, "i", &fd
))
231 Py_BEGIN_ALLOW_THREADS
236 return PyInt_FromLong((long)fd
);
243 mac_fdopen(self
, args
)
247 extern int fclose(FILE *);
251 if (!PyArg_Parse(args
, "(is)", &fd
, &mode
))
253 Py_BEGIN_ALLOW_THREADS
254 fp
= fdopen(fd
, mode
);
258 return PyFile_FromFile(fp
, "(fdopen)", mode
, fclose
);
262 #if !TARGET_API_MAC_CARBON
264 mac_getbootvol(self
, args
)
269 if (!PyArg_NoArgs(args
))
271 Py_BEGIN_ALLOW_THREADS
276 return PyString_FromString(res
);
281 mac_getcwd(self
, args
)
285 char path
[MAXPATHLEN
];
287 if (!PyArg_NoArgs(args
))
289 Py_BEGIN_ALLOW_THREADS
291 res
= getcwd(path
, sizeof path
);
297 PyErr_SetString(MacError
, path
);
300 return PyString_FromString(res
);
304 mac_listdir(self
, args
)
312 if (!PyArg_Parse(args
, "s", &name
))
314 Py_BEGIN_ALLOW_THREADS
315 if ((dirp
= opendir(name
)) == NULL
) {
319 if ((d
= PyList_New(0)) == NULL
) {
324 while ((ep
= readdir(dirp
)) != NULL
) {
325 v
= PyString_FromString(ep
->d_name
);
331 if (PyList_Append(d
, v
) != 0) {
346 mac_lseek(self
, args
)
354 if (!PyArg_Parse(args
, "(iii)", &fd
, &where
, &how
))
356 Py_BEGIN_ALLOW_THREADS
357 res
= lseek(fd
, (long)where
, how
);
361 return PyInt_FromLong(res
);
365 mac_mkdir(self
, args
)
371 int mode
= 0777; /* Unused */
372 if (!PyArg_ParseTuple(args
, "s|i", &path
, &mode
))
374 Py_BEGIN_ALLOW_THREADS
378 res
= mkdir(path
, mode
);
395 if (!PyArg_Parse(args
, "(si)", &path
, &mode
))
397 Py_BEGIN_ALLOW_THREADS
398 fd
= open(path
, mode
);
402 return PyInt_FromLong((long)fd
);
412 if (!PyArg_Parse(args
, "(ii)", &fd
, &size
))
414 buffer
= PyString_FromStringAndSize((char *)NULL
, size
);
417 Py_BEGIN_ALLOW_THREADS
418 size
= read(fd
, PyString_AsString(buffer
), size
);
424 _PyString_Resize(&buffer
, size
);
429 mac_rename(self
, args
)
433 return mac_2str(args
, rename
);
437 mac_rmdir(self
, args
)
441 return mac_1str(args
, rmdir
);
452 if (!PyArg_Parse(args
, "s", &path
))
454 Py_BEGIN_ALLOW_THREADS
455 res
= stat(path
, &st
);
459 return Py_BuildValue("(lllllllddd)",
469 (double)st
.st_ctime
);
474 mac_fstat(self
, args
)
481 if (!PyArg_Parse(args
, "l", &fd
))
483 Py_BEGIN_ALLOW_THREADS
484 res
= fstat((int)fd
, &st
);
488 return Py_BuildValue("(lllllllddd)",
498 (double)st
.st_ctime
);
500 #endif /* WEHAVE_FSTAT */
502 #if !TARGET_API_MAC_CARBON_NOTYET
504 mac_xstat(self
, args
)
512 if (!PyArg_Parse(args
, "s", &path
))
515 ** Convoluted: we want stat() and xstat() to agree, so we call both
516 ** stat and macstat, and use the latter only for values not provided by
519 Py_BEGIN_ALLOW_THREADS
520 res
= macstat(path
, &mst
);
524 Py_BEGIN_ALLOW_THREADS
525 res
= stat(path
, &st
);
529 return Py_BuildValue("(llllllldddls#s#)",
552 if (!PyArg_NoArgs(args
))
554 Py_BEGIN_ALLOW_THREADS
564 mac_unlink(self
, args
)
568 return mac_1str(args
, (int (*)(const char *))unlink
);
572 mac_write(self
, args
)
578 if (!PyArg_Parse(args
, "(is#)", &fd
, &buffer
, &size
))
580 Py_BEGIN_ALLOW_THREADS
581 size
= write(fd
, buffer
, size
);
585 return PyInt_FromLong((long)size
);
588 #ifdef USE_MALLOC_DEBUG
590 mac_mstats(self
, args
)
598 #endif /* USE_MALLOC_DEBUG */
600 static struct PyMethodDef mac_methods
[] = {
601 {"chdir", mac_chdir
},
602 {"close", mac_close
},
607 {"fdopen", mac_fdopen
},
610 {"fstat", mac_fstat
},
612 #if !TARGET_API_MAC_CARBON
613 {"getbootvol", mac_getbootvol
}, /* non-standard */
615 {"getcwd", mac_getcwd
},
616 {"listdir", mac_listdir
, 0},
617 {"lseek", mac_lseek
},
618 {"mkdir", mac_mkdir
, 1},
621 {"rename", mac_rename
},
622 {"rmdir", mac_rmdir
},
624 #if !TARGET_API_MAC_CARBON_NOTYET
625 {"xstat", mac_xstat
},
628 {"remove", mac_unlink
},
629 {"unlink", mac_unlink
},
630 {"write", mac_write
},
631 #ifdef USE_MALLOC_DEBUG
632 {"mstats", mac_mstats
},
635 {NULL
, NULL
} /* Sentinel */
644 m
= Py_InitModule("mac", mac_methods
);
645 d
= PyModule_GetDict(m
);
647 /* Initialize mac.error exception */
648 MacError
= PyErr_NewException("mac.error", NULL
, NULL
);
649 PyDict_SetItemString(d
, "error", MacError
);