blob: 8b3bcc53148eaf4d5c5bdee1bfe76a5847c0b71b [file] [log] [blame]
Guido van Rossumce9739b1994-01-05 16:17:15 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossum99546991995-01-08 14:33:34 +00003The 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
Jack Jansenf5c20571997-01-30 15:48:07 +000027#include "Python.h"
Guido van Rossum98bf58f2001-10-18 20:34:25 +000028#include "structseq.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
Jack Jansen6e68a7e2001-05-12 21:31:34 +000035#if TARGET_API_MAC_OS8
Jack Jansend9d0b5f2001-01-12 23:37:14 +000036/* Skip for Carbon */
Jack Jansen59b912a1996-10-15 16:13:33 +000037#include "macstat.h"
Jack Jansene79dc762000-06-02 21:35:07 +000038#endif
39
Jack Jansenc743c8d1996-02-14 16:02:30 +000040#ifdef USE_GUSI
Jack Jansen59b912a1996-10-15 16:13:33 +000041/* Remove defines from macstat.h */
42#undef S_IFMT
43#undef S_IFDIR
44#undef S_IFREG
45#undef S_IREAD
46#undef S_IWRITE
47#undef S_IEXEC
48
Jack Jansenc743c8d1996-02-14 16:02:30 +000049#include <sys/types.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000050#include <sys/stat.h>
51#else /* USE_GUSI */
Jack Jansen6e68a7e2001-05-12 21:31:34 +000052#if TARGET_API_MAC_OS8
Jack Jansen59b912a1996-10-15 16:13:33 +000053#define stat macstat
Jack Jansene79dc762000-06-02 21:35:07 +000054#endif
Jack Jansen37d21e12000-04-07 09:25:06 +000055#endif /* USE_GUSI */
Guido van Rossumce9739b1994-01-05 16:17:15 +000056
Jack Jansen37d21e12000-04-07 09:25:06 +000057#ifdef USE_GUSI2
Jack Jansen37d21e12000-04-07 09:25:06 +000058#include <unistd.h>
59#include <fcntl.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000060#else
Jack Jansen12e89e42000-05-12 21:36:29 +000061#define mode_t int
Guido van Rossum87f223c1994-05-06 15:54:15 +000062#include <fcntl.h>
Jack Jansene79dc762000-06-02 21:35:07 +000063#ifdef _POSIX
64#include <unistd.h>
65#include <stat.h>
66#endif
Jack Jansen0c097ea1994-12-14 13:48:38 +000067#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000068
Jack Jansenb6971731996-02-20 16:24:37 +000069/* Optional routines, for some compiler/runtime combinations */
Jack Jansenb6971731996-02-20 16:24:37 +000070#if defined(USE_GUSI) || !defined(__MWERKS__)
71#define WEHAVE_FDOPEN
72#endif
73#if defined(MPW) || defined(USE_GUSI)
74#define WEHAVE_DUP
75#endif
Jack Jansen40bd7701998-02-20 15:56:19 +000076#if defined(USE_GUSI)
77#define WEHAVE_FSTAT
78#endif
Jack Jansen7cbf4801995-01-22 16:52:38 +000079
Guido van Rossumd4d77281994-08-19 10:51:31 +000080#include "macdefs.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000081#ifdef USE_GUSI
82#include <dirent.h>
83#else
Guido van Rossumd4d77281994-08-19 10:51:31 +000084#include "dirent.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000085#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +000086
87#ifndef MAXPATHLEN
88#define MAXPATHLEN 1024
89#endif
90
91/* Prototypes for Unix simulation on Mac */
92
Jack Jansenb6971731996-02-20 16:24:37 +000093#ifndef USE_GUSI
94
Jack Jansend88296d2000-07-11 19:51:05 +000095int chdir(const char *path);
96int mkdir(const char *path, int mode);
97DIR * opendir(char *);
98void closedir(DIR *);
99struct dirent * readdir(DIR *);
100int rmdir(const char *path);
101int sync(void);
Jack Jansenb6971731996-02-20 16:24:37 +0000102
Jack Jansend88296d2000-07-11 19:51:05 +0000103int unlink(const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000104
Jack Jansenb6971731996-02-20 16:24:37 +0000105#endif /* USE_GUSI */
106
Jack Jansend88296d2000-07-11 19:51:05 +0000107char *getwd(char *);
108char *getbootvol(void);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000109
110
Guido van Rossum87f223c1994-05-06 15:54:15 +0000111/* Set a MAC-specific error from errno, and return NULL */
112
Jack Jansenf5c20571997-01-30 15:48:07 +0000113static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000114mac_error()
115{
Jack Jansenb3be2162001-11-30 14:16:36 +0000116 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000117}
118
119/* MAC generic methods */
120
Jack Jansenf5c20571997-01-30 15:48:07 +0000121static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000122mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000123 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000124 int (*func)(const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000125{
126 char *path1;
127 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000128 if (!PyArg_ParseTuple(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000129 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000130 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000131 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000132 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000133 if (res < 0)
134 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000135 Py_INCREF(Py_None);
136 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000137}
138
Jack Jansenf5c20571997-01-30 15:48:07 +0000139static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000140mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000141 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000142 int (*func)(const char *, const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000143{
144 char *path1, *path2;
145 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000146 if (!PyArg_ParseTuple(args, "ss", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000147 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000148 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000149 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000150 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000151 if (res < 0)
152 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000153 Py_INCREF(Py_None);
154 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000155}
156
Jack Jansenf5c20571997-01-30 15:48:07 +0000157static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000158mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000159 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000160 int (*func)(const char *, int);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000161{
162 char *path;
163 int i;
164 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000165 if (!PyArg_ParseTuple(args, "si", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000166 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000167 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000168 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000169 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000170 if (res < 0)
171 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000172 Py_INCREF(Py_None);
173 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000174}
175
Jack Jansenf5c20571997-01-30 15:48:07 +0000176static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000177mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000178 PyObject *self;
179 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000180{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000181 return mac_1str(args, chdir);
182}
183
Jack Jansenf5c20571997-01-30 15:48:07 +0000184static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000185mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000186 PyObject *self;
187 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000188{
189 int fd, res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000190 if (!PyArg_ParseTuple(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000191 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000192 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000193 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000194 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000195 if (res < 0)
196 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000197 Py_INCREF(Py_None);
198 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000199}
200
Jack Jansenb6971731996-02-20 16:24:37 +0000201#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000202
Jack Jansenf5c20571997-01-30 15:48:07 +0000203static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000204mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000205 PyObject *self;
206 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000207{
208 int fd;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000209 if (!PyArg_ParseTuple(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000210 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000211 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000212 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000213 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000214 if (fd < 0)
215 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000216 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000217}
218
Jack Jansenb6971731996-02-20 16:24:37 +0000219#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000220
Jack Jansenb6971731996-02-20 16:24:37 +0000221#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000222static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000223mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000224 PyObject *self;
225 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000226{
Jack Jansend88296d2000-07-11 19:51:05 +0000227 extern int fclose(FILE *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000228 int fd;
229 char *mode;
230 FILE *fp;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000231 if (!PyArg_ParseTuple(args, "is", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000232 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000233 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000234 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000235 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000236 if (fp == NULL)
237 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000238 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000239}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000240#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000241
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000242#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000243static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000244mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000245 PyObject *self;
246 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000247{
248 char *res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000249 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000250 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000251 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000252 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000253 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000254 if (res == NULL)
255 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000256 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000257}
Jack Jansene79dc762000-06-02 21:35:07 +0000258#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000259
Jack Jansenf5c20571997-01-30 15:48:07 +0000260static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000261mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000262 PyObject *self;
263 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000264{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000265 char path[MAXPATHLEN];
266 char *res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000267 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000268 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000269 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000270#ifdef USE_GUSI
271 res = getcwd(path, sizeof path);
272#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000273 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000274#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000275 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000276 if (res == NULL) {
Jack Jansenb3be2162001-11-30 14:16:36 +0000277 return mac_error();
Guido van Rossumce9739b1994-01-05 16:17:15 +0000278 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000279 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000280}
281
Jack Jansenf5c20571997-01-30 15:48:07 +0000282static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000283mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000284 PyObject *self;
285 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000286{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000287 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000288 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000289 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000290 struct dirent *ep;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000291 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000292 return NULL;
Jack Jansend7b568a2001-08-11 23:18:55 +0000293#ifdef USE_GUSI
294 /* Work around a bug in GUSI: if you opendir() a file it will
295 ** actually opendir() the parent directory.
296 */
297 {
298 struct stat stb;
299 int res;
300
301 res = stat(name, &stb);
302 if ( res < 0 )
303 return mac_error();
304 if (!S_ISDIR(stb.st_mode) ) {
305 errno = ENOTDIR;
306 return mac_error();
307 }
308 }
309#endif
310
Jack Jansenf5c20571997-01-30 15:48:07 +0000311 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000312 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000313 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000314 return mac_error();
315 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000316 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000317 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000318 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000319 return NULL;
320 }
321 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000322 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000323 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000324 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000325 d = NULL;
326 break;
327 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000328 if (PyList_Append(d, v) != 0) {
329 Py_DECREF(v);
330 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000331 d = NULL;
332 break;
333 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000334 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000335 }
336 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000337 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000338
Guido van Rossumce9739b1994-01-05 16:17:15 +0000339 return d;
340}
341
Jack Jansenf5c20571997-01-30 15:48:07 +0000342static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000343mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000344 PyObject *self;
345 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000346{
347 int fd;
348 int where;
349 int how;
350 long res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000351 if (!PyArg_ParseTuple(args, "iii", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000352 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000353 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000354 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000355 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000356 if (res < 0)
357 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000358 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000359}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000360
Jack Jansenf5c20571997-01-30 15:48:07 +0000361static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000362mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000363 PyObject *self;
364 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000365{
Jack Jansen243b29b1996-02-21 12:33:50 +0000366 int res;
367 char *path;
368 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000369 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000370 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000371 Py_BEGIN_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000372 res = mkdir(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000373 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000374 if (res < 0)
375 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000376 Py_INCREF(Py_None);
377 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000378}
379
Jack Jansenf5c20571997-01-30 15:48:07 +0000380static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000381mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000382 PyObject *self;
383 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000384{
385 char *path;
386 int mode;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000387 int perm; /* Accepted but ignored */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000388 int fd;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000389 if (!PyArg_ParseTuple(args, "si|i", &path, &mode, &perm))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000390 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000391 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000392 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000393 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000394 if (fd < 0)
395 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000396 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000397}
398
Jack Jansenf5c20571997-01-30 15:48:07 +0000399static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000400mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000401 PyObject *self;
402 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000403{
404 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000405 PyObject *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000406 if (!PyArg_ParseTuple(args, "ii", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000407 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000408 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000409 if (buffer == NULL)
410 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000411 Py_BEGIN_ALLOW_THREADS
412 size = read(fd, PyString_AsString(buffer), size);
413 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000414 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000415 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000416 return mac_error();
417 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000418 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000419 return buffer;
420}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000421
Jack Jansenf5c20571997-01-30 15:48:07 +0000422static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000423mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000424 PyObject *self;
425 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000426{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000427 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000428}
429
Jack Jansenf5c20571997-01-30 15:48:07 +0000430static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000431mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000432 PyObject *self;
433 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000434{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000435 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000436}
437
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000438static char stat_result__doc__[] =
439"stat_result: Result from stat or lstat.\n\n\
440This object may be accessed either as a tuple of\n\
441 (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
442or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
443\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000444See os.stat for more information.\n";
445
446#define COMMON_STAT_RESULT_FIELDS \
447 { "st_mode", "protection bits" }, \
448 { "st_ino", "inode" }, \
Jack Jansene54968a2001-10-23 22:28:23 +0000449 { "st_dev", "device" }, \
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000450 { "st_nlink", "number of hard links" }, \
Jack Jansene54968a2001-10-23 22:28:23 +0000451 { "st_uid", "user ID of owner" }, \
452 { "st_gid", "group ID of owner" }, \
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000453 { "st_size", "total size, in bytes" }, \
454 { "st_atime", "time of last access" }, \
455 { "st_mtime", "time of last modification" }, \
456 { "st_ctime", "time of last change" },
457
458
459
460static PyStructSequence_Field stat_result_fields[] = {
461 COMMON_STAT_RESULT_FIELDS
462 {0}
463};
464
465static PyStructSequence_Desc stat_result_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000466 "mac.stat_result",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000467 stat_result__doc__,
468 stat_result_fields,
469 10
470};
471
472static PyTypeObject StatResultType;
473
474#ifdef TARGET_API_MAC_OS8
475static PyStructSequence_Field xstat_result_fields[] = {
Jack Jansene54968a2001-10-23 22:28:23 +0000476 COMMON_STAT_RESULT_FIELDS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000477 { "st_rsize" },
478 { "st_creator" },
479 { "st_type "},
480 {0}
481};
482
483static PyStructSequence_Desc xstat_result_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000484 "mac.xstat_result",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000485 stat_result__doc__,
486 xstat_result_fields,
487 13
488};
489
490static PyTypeObject XStatResultType;
491#endif
492
493static PyObject *
494_pystat_from_struct_stat(struct stat st, void* _mst)
495{
496 PyObject *v;
497
498#if TARGET_API_MAC_OS8
499 struct macstat *mst;
500
501 if (_mst != NULL)
502 v = PyStructSequence_New(&XStatResultType);
503 else
504#endif
505 v = PyStructSequence_New(&StatResultType);
506 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
507 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
508 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
509 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
510 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
511 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
512 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long)st.st_size));
513 PyStructSequence_SET_ITEM(v, 7,
514 PyFloat_FromDouble((double)st.st_atime));
515 PyStructSequence_SET_ITEM(v, 8,
516 PyFloat_FromDouble((double)st.st_mtime));
517 PyStructSequence_SET_ITEM(v, 9,
518 PyFloat_FromDouble((double)st.st_ctime));
519#if TARGET_API_MAC_OS8
520 if (_mst != NULL) {
521 mst = (struct macstat *) _mst;
522 PyStructSequence_SET_ITEM(v, 10,
523 PyInt_FromLong((long)mst->st_rsize));
524 PyStructSequence_SET_ITEM(v, 11,
525 PyString_FromStringAndSize(mst->st_creator,
526 4));
527 PyStructSequence_SET_ITEM(v, 12,
528 PyString_FromStringAndSize(mst->st_type,
529 4));
530 }
531#endif
532
533 if (PyErr_Occurred()) {
534 Py_DECREF(v);
535 return NULL;
536 }
537
538 return v;
539}
540
541
Jack Jansenf5c20571997-01-30 15:48:07 +0000542static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000543mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000544 PyObject *self;
545 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000546{
Jack Jansen59b912a1996-10-15 16:13:33 +0000547 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000548 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000549 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000550 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000551 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000552 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000553 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000554 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000555 if (res != 0)
556 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000557
558 return _pystat_from_struct_stat(st, NULL);
Jack Jansen40bd7701998-02-20 15:56:19 +0000559}
560
561#ifdef WEHAVE_FSTAT
562static PyObject *
563mac_fstat(self, args)
564 PyObject *self;
565 PyObject *args;
566{
567 struct stat st;
568 long fd;
569 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000570 if (!PyArg_ParseTuple(args, "l", &fd))
Jack Jansen40bd7701998-02-20 15:56:19 +0000571 return NULL;
572 Py_BEGIN_ALLOW_THREADS
573 res = fstat((int)fd, &st);
574 Py_END_ALLOW_THREADS
575 if (res != 0)
576 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000577
578 return _pystat_from_struct_stat(st, NULL);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000579}
Jack Jansen40bd7701998-02-20 15:56:19 +0000580#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000581
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000582#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000583static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000584mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000585 PyObject *self;
586 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000587{
Jack Jansen59b912a1996-10-15 16:13:33 +0000588 struct macstat mst;
589 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000590 char *path;
591 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000592 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000593 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000594 /*
595 ** Convoluted: we want stat() and xstat() to agree, so we call both
596 ** stat and macstat, and use the latter only for values not provided by
597 ** the former.
598 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000599 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000600 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000601 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000602 if (res != 0)
603 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000604 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000605 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000606 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000607 if (res != 0)
608 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000609
610 return _pystat_from_struct_stat(st, (void*) &mst);
Guido van Rossum222c8921995-08-08 14:10:22 +0000611}
Jack Jansene79dc762000-06-02 21:35:07 +0000612#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000613
Jack Jansenf5c20571997-01-30 15:48:07 +0000614static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000615mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000616 PyObject *self;
617 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000618{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000619 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000620 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000621 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000622 Py_BEGIN_ALLOW_THREADS
Jack Jansen0e5a7332002-03-21 21:09:36 +0000623#ifdef USE_GUSI2
624 sync();
625 res = 0;
626#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000627 res = sync();
Jack Jansen0e5a7332002-03-21 21:09:36 +0000628#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000629 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000630 if (res != 0)
631 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000632 Py_INCREF(Py_None);
633 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000634}
635
Jack Jansenf5c20571997-01-30 15:48:07 +0000636static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000637mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000638 PyObject *self;
639 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000640{
Guido van Rossum739267b1994-08-29 08:42:37 +0000641 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000642}
643
Jack Jansenf5c20571997-01-30 15:48:07 +0000644static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000645mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000646 PyObject *self;
647 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000648{
649 int fd, size;
650 char *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000651 if (!PyArg_ParseTuple(args, "is#", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000652 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000653 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000654 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000655 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000656 if (size < 0)
657 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000658 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000659}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000660
Jack Jansen46ed2761996-10-23 15:46:57 +0000661#ifdef USE_MALLOC_DEBUG
Jack Jansen8748a0b2000-08-25 22:18:20 +0000662void *mstats(char *);
663
Jack Jansenf5c20571997-01-30 15:48:07 +0000664static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000665mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000666 PyObject*self;
667 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000668{
669 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000670 Py_INCREF(Py_None);
671 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000672}
Jack Jansen4f27a551997-02-20 15:22:17 +0000673#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000674
Jack Jansenf5c20571997-01-30 15:48:07 +0000675static struct PyMethodDef mac_methods[] = {
Jack Jansenddafd2b2001-08-03 13:07:19 +0000676 {"chdir", mac_chdir, 1},
677 {"close", mac_close, 1},
Jack Jansenb6971731996-02-20 16:24:37 +0000678#ifdef WEHAVE_DUP
Jack Jansenddafd2b2001-08-03 13:07:19 +0000679 {"dup", mac_dup, 1},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000680#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000681#ifdef WEHAVE_FDOPEN
Jack Jansenddafd2b2001-08-03 13:07:19 +0000682 {"fdopen", mac_fdopen, 1},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000683#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000684#ifdef WEHAVE_FSTAT
Jack Jansenddafd2b2001-08-03 13:07:19 +0000685 {"fstat", mac_fstat, 1},
Jack Jansen40bd7701998-02-20 15:56:19 +0000686#endif
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000687#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000688 {"getbootvol", mac_getbootvol, 1}, /* non-standard */
Jack Jansene79dc762000-06-02 21:35:07 +0000689#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000690 {"getcwd", mac_getcwd, 1},
691 {"listdir", mac_listdir, 1},
692 {"lseek", mac_lseek, 1},
Jack Jansen243b29b1996-02-21 12:33:50 +0000693 {"mkdir", mac_mkdir, 1},
Jack Jansenddafd2b2001-08-03 13:07:19 +0000694 {"open", mac_open, 1},
695 {"read", mac_read, 1},
696 {"rename", mac_rename, 1},
697 {"rmdir", mac_rmdir, 1},
698 {"stat", mac_stat, 1},
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000699#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000700 {"xstat", mac_xstat, 1},
Jack Jansene79dc762000-06-02 21:35:07 +0000701#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000702 {"sync", mac_sync, 1},
703 {"remove", mac_unlink, 1},
704 {"unlink", mac_unlink, 1},
705 {"write", mac_write, 1},
Jack Jansen46ed2761996-10-23 15:46:57 +0000706#ifdef USE_MALLOC_DEBUG
Jack Jansenddafd2b2001-08-03 13:07:19 +0000707 {"mstats", mac_mstats, 1},
Jack Jansend50e4e11995-01-18 13:58:04 +0000708#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000709
Guido van Rossumce9739b1994-01-05 16:17:15 +0000710 {NULL, NULL} /* Sentinel */
711};
712
Jack Jansenddafd2b2001-08-03 13:07:19 +0000713static int
714ins(PyObject *d, char *symbol, long value)
715{
716 PyObject* v = PyInt_FromLong(value);
717 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
718 return -1; /* triggers fatal error */
719
720 Py_DECREF(v);
721 return 0;
722}
723
724static int
725all_ins(PyObject *d)
726{
727#ifdef F_OK
728 if (ins(d, "F_OK", (long)F_OK)) return -1;
729#endif
730#ifdef R_OK
731 if (ins(d, "R_OK", (long)R_OK)) return -1;
732#endif
733#ifdef W_OK
734 if (ins(d, "W_OK", (long)W_OK)) return -1;
735#endif
736#ifdef X_OK
737 if (ins(d, "X_OK", (long)X_OK)) return -1;
738#endif
739#ifdef NGROUPS_MAX
740 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
741#endif
742#ifdef TMP_MAX
743 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
744#endif
745#ifdef WNOHANG
746 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
747#endif
748#ifdef O_RDONLY
749 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
750#endif
751#ifdef O_WRONLY
752 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
753#endif
754#ifdef O_RDWR
755 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
756#endif
757#ifdef O_NDELAY
758 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
759#endif
760#ifdef O_NONBLOCK
761 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
762#endif
763#ifdef O_APPEND
764 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
765#endif
766#ifdef O_DSYNC
767 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
768#endif
769#ifdef O_RSYNC
770 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
771#endif
772#ifdef O_SYNC
773 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
774#endif
775#ifdef O_NOCTTY
776 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
777#endif
778#ifdef O_CREAT
779 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
780#endif
781#ifdef O_EXCL
782 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
783#endif
784#ifdef O_TRUNC
785 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
786#endif
787#ifdef O_BINARY
788 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
789#endif
790#ifdef O_TEXT
791 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
792#endif
793
794#ifdef HAVE_SPAWNV
795 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
796 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
797 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
798 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
799 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
800#endif
801
802#if defined(PYOS_OS2)
803 if (insertvalues(d)) return -1;
804#endif
805 return 0;
806}
807
Guido van Rossumce9739b1994-01-05 16:17:15 +0000808
809void
810initmac()
811{
Jack Jansenf5c20571997-01-30 15:48:07 +0000812 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000813
Jack Jansenf5c20571997-01-30 15:48:07 +0000814 m = Py_InitModule("mac", mac_methods);
815 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000816
Jack Jansenddafd2b2001-08-03 13:07:19 +0000817 if (all_ins(d))
818 return;
819
Guido van Rossumce9739b1994-01-05 16:17:15 +0000820 /* Initialize mac.error exception */
Jack Jansenb3be2162001-11-30 14:16:36 +0000821 PyDict_SetItemString(d, "error", PyExc_OSError);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000822
823 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
824 PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType);
825
826#if TARGET_API_MAC_OS8
827 PyStructSequence_InitType(&XStatResultType, &xstat_result_desc);
828 PyDict_SetItemString(d, "xstat_result", (PyObject*) &XStatResultType);
829#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000830}