blob: c71d8a9bb86aa996e3be1df1245af37ecd9620fd [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 Jansen37d21e12000-04-07 09:25:06 +000049#ifdef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +000050#include <GUSI.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000051#endif /* USE_GUSI1 */
Jack Jansenc743c8d1996-02-14 16:02:30 +000052#include <sys/types.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000053#include <sys/stat.h>
54#else /* USE_GUSI */
Jack Jansen6e68a7e2001-05-12 21:31:34 +000055#if TARGET_API_MAC_OS8
Jack Jansen59b912a1996-10-15 16:13:33 +000056#define stat macstat
Jack Jansene79dc762000-06-02 21:35:07 +000057#endif
Jack Jansen37d21e12000-04-07 09:25:06 +000058#endif /* USE_GUSI */
Guido van Rossumce9739b1994-01-05 16:17:15 +000059
Jack Jansen37d21e12000-04-07 09:25:06 +000060#ifdef USE_GUSI2
61#define sync bad_sync
62#include <unistd.h>
63#include <fcntl.h>
64#undef sync
65int sync(void);
66#else
Jack Jansen12e89e42000-05-12 21:36:29 +000067#define mode_t int
Guido van Rossum87f223c1994-05-06 15:54:15 +000068#include <fcntl.h>
Jack Jansene79dc762000-06-02 21:35:07 +000069#ifdef _POSIX
70#include <unistd.h>
71#include <stat.h>
72#endif
Jack Jansen0c097ea1994-12-14 13:48:38 +000073#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000074
Jack Jansenb6971731996-02-20 16:24:37 +000075/* Optional routines, for some compiler/runtime combinations */
Jack Jansenb6971731996-02-20 16:24:37 +000076#if defined(USE_GUSI) || !defined(__MWERKS__)
77#define WEHAVE_FDOPEN
78#endif
79#if defined(MPW) || defined(USE_GUSI)
80#define WEHAVE_DUP
81#endif
Jack Jansen40bd7701998-02-20 15:56:19 +000082#if defined(USE_GUSI)
83#define WEHAVE_FSTAT
84#endif
Jack Jansen7cbf4801995-01-22 16:52:38 +000085
Guido van Rossumd4d77281994-08-19 10:51:31 +000086#include "macdefs.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000087#ifdef USE_GUSI
88#include <dirent.h>
89#else
Guido van Rossumd4d77281994-08-19 10:51:31 +000090#include "dirent.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000091#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +000092
93#ifndef MAXPATHLEN
94#define MAXPATHLEN 1024
95#endif
96
97/* Prototypes for Unix simulation on Mac */
98
Jack Jansenb6971731996-02-20 16:24:37 +000099#ifndef USE_GUSI
100
Jack Jansend88296d2000-07-11 19:51:05 +0000101int chdir(const char *path);
102int mkdir(const char *path, int mode);
103DIR * opendir(char *);
104void closedir(DIR *);
105struct dirent * readdir(DIR *);
106int rmdir(const char *path);
107int sync(void);
Jack Jansenb6971731996-02-20 16:24:37 +0000108
Jack Jansend88296d2000-07-11 19:51:05 +0000109int unlink(const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000110
Jack Jansenb6971731996-02-20 16:24:37 +0000111#endif /* USE_GUSI */
112
Jack Jansend88296d2000-07-11 19:51:05 +0000113char *getwd(char *);
114char *getbootvol(void);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000115
116
Guido van Rossum87f223c1994-05-06 15:54:15 +0000117/* Set a MAC-specific error from errno, and return NULL */
118
Jack Jansenf5c20571997-01-30 15:48:07 +0000119static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000120mac_error()
121{
Jack Jansenb3be2162001-11-30 14:16:36 +0000122 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000123}
124
125/* MAC generic methods */
126
Jack Jansenf5c20571997-01-30 15:48:07 +0000127static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000128mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000129 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000130 int (*func)(const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000131{
132 char *path1;
133 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000134 if (!PyArg_ParseTuple(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000135 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000136 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000137 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000138 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000139 if (res < 0)
140 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000141 Py_INCREF(Py_None);
142 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000143}
144
Jack Jansenf5c20571997-01-30 15:48:07 +0000145static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000146mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000147 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000148 int (*func)(const char *, const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000149{
150 char *path1, *path2;
151 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000152 if (!PyArg_ParseTuple(args, "ss", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000153 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000154 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000155 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000156 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000157 if (res < 0)
158 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000159 Py_INCREF(Py_None);
160 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000161}
162
Jack Jansenf5c20571997-01-30 15:48:07 +0000163static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000164mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000165 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000166 int (*func)(const char *, int);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000167{
168 char *path;
169 int i;
170 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000171 if (!PyArg_ParseTuple(args, "si", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000172 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000173 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000174 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000175 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000176 if (res < 0)
177 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000178 Py_INCREF(Py_None);
179 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000180}
181
Jack Jansenf5c20571997-01-30 15:48:07 +0000182static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000183mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000184 PyObject *self;
185 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000186{
Jack Jansen37d21e12000-04-07 09:25:06 +0000187#ifdef USE_GUSI1
Jack Jansenf5c20571997-01-30 15:48:07 +0000188 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000189
190 /* Change MacOS's idea of wd too */
191 rv = mac_1str(args, chdir);
192 PyMac_FixGUSIcd();
193 return rv;
194#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000195 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000196#endif
197
Guido van Rossum87f223c1994-05-06 15:54:15 +0000198}
199
Jack Jansenf5c20571997-01-30 15:48:07 +0000200static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000201mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000202 PyObject *self;
203 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000204{
205 int fd, res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000206 if (!PyArg_ParseTuple(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000207 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000208 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000209 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000210 Py_END_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000211#ifndef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +0000212 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000213 if (res < 0)
214 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000215#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000216 Py_INCREF(Py_None);
217 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000218}
219
Jack Jansenb6971731996-02-20 16:24:37 +0000220#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000221
Jack Jansenf5c20571997-01-30 15:48:07 +0000222static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000223mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000224 PyObject *self;
225 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000226{
227 int fd;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000228 if (!PyArg_ParseTuple(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000229 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000230 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000231 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000232 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000233 if (fd < 0)
234 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000235 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000236}
237
Jack Jansenb6971731996-02-20 16:24:37 +0000238#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000239
Jack Jansenb6971731996-02-20 16:24:37 +0000240#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000241static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000242mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000243 PyObject *self;
244 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000245{
Jack Jansend88296d2000-07-11 19:51:05 +0000246 extern int fclose(FILE *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000247 int fd;
248 char *mode;
249 FILE *fp;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000250 if (!PyArg_ParseTuple(args, "is", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000251 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000252 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000253 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000254 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000255 if (fp == NULL)
256 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000257 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000258}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000259#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000260
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000261#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000262static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000263mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000264 PyObject *self;
265 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000266{
267 char *res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000268 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000269 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000270 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000271 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000272 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000273 if (res == NULL)
274 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000275 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000276}
Jack Jansene79dc762000-06-02 21:35:07 +0000277#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000278
Jack Jansenf5c20571997-01-30 15:48:07 +0000279static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000280mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000281 PyObject *self;
282 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000283{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000284 char path[MAXPATHLEN];
285 char *res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000286 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000287 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000288 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000289#ifdef USE_GUSI
290 res = getcwd(path, sizeof path);
291#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000292 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000293#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000294 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000295 if (res == NULL) {
Jack Jansenb3be2162001-11-30 14:16:36 +0000296 return mac_error();
Guido van Rossumce9739b1994-01-05 16:17:15 +0000297 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000298 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000299}
300
Jack Jansenf5c20571997-01-30 15:48:07 +0000301static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000302mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000303 PyObject *self;
304 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000305{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000306 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000307 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000308 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000309 struct dirent *ep;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000310 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000311 return NULL;
Jack Jansend7b568a2001-08-11 23:18:55 +0000312#ifdef USE_GUSI
313 /* Work around a bug in GUSI: if you opendir() a file it will
314 ** actually opendir() the parent directory.
315 */
316 {
317 struct stat stb;
318 int res;
319
320 res = stat(name, &stb);
321 if ( res < 0 )
322 return mac_error();
323 if (!S_ISDIR(stb.st_mode) ) {
324 errno = ENOTDIR;
325 return mac_error();
326 }
327 }
328#endif
329
Jack Jansenf5c20571997-01-30 15:48:07 +0000330 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000331 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000332 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000333 return mac_error();
334 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000335 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000336 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000337 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000338 return NULL;
339 }
340 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000341 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000342 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000343 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000344 d = NULL;
345 break;
346 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000347 if (PyList_Append(d, v) != 0) {
348 Py_DECREF(v);
349 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000350 d = NULL;
351 break;
352 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000353 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000354 }
355 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000356 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000357
Guido van Rossumce9739b1994-01-05 16:17:15 +0000358 return d;
359}
360
Jack Jansenf5c20571997-01-30 15:48:07 +0000361static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000362mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000363 PyObject *self;
364 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000365{
366 int fd;
367 int where;
368 int how;
369 long res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000370 if (!PyArg_ParseTuple(args, "iii", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000371 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000372 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000373 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000374 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000375 if (res < 0)
376 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000377 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000378}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000379
Jack Jansenf5c20571997-01-30 15:48:07 +0000380static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000381mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000382 PyObject *self;
383 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000384{
Jack Jansen243b29b1996-02-21 12:33:50 +0000385 int res;
386 char *path;
387 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000388 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000389 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000390 Py_BEGIN_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000391#ifdef USE_GUSI1
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000392 res = mkdir(path);
393#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000394 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000395#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000396 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000397 if (res < 0)
398 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000399 Py_INCREF(Py_None);
400 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000401}
402
Jack Jansenf5c20571997-01-30 15:48:07 +0000403static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000404mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000405 PyObject *self;
406 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000407{
408 char *path;
409 int mode;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000410 int perm; /* Accepted but ignored */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000411 int fd;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000412 if (!PyArg_ParseTuple(args, "si|i", &path, &mode, &perm))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000413 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000414 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000415 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000416 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000417 if (fd < 0)
418 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000419 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000420}
421
Jack Jansenf5c20571997-01-30 15:48:07 +0000422static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000423mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000424 PyObject *self;
425 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000426{
427 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000428 PyObject *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000429 if (!PyArg_ParseTuple(args, "ii", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000430 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000431 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000432 if (buffer == NULL)
433 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000434 Py_BEGIN_ALLOW_THREADS
435 size = read(fd, PyString_AsString(buffer), size);
436 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000437 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000438 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000439 return mac_error();
440 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000441 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000442 return buffer;
443}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000444
Jack Jansenf5c20571997-01-30 15:48:07 +0000445static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000446mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000447 PyObject *self;
448 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000449{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000450 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000451}
452
Jack Jansenf5c20571997-01-30 15:48:07 +0000453static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000454mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000455 PyObject *self;
456 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000457{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000458 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000459}
460
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000461static char stat_result__doc__[] =
462"stat_result: Result from stat or lstat.\n\n\
463This object may be accessed either as a tuple of\n\
464 (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
465or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
466\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000467See os.stat for more information.\n";
468
469#define COMMON_STAT_RESULT_FIELDS \
470 { "st_mode", "protection bits" }, \
471 { "st_ino", "inode" }, \
Jack Jansene54968a2001-10-23 22:28:23 +0000472 { "st_dev", "device" }, \
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000473 { "st_nlink", "number of hard links" }, \
Jack Jansene54968a2001-10-23 22:28:23 +0000474 { "st_uid", "user ID of owner" }, \
475 { "st_gid", "group ID of owner" }, \
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000476 { "st_size", "total size, in bytes" }, \
477 { "st_atime", "time of last access" }, \
478 { "st_mtime", "time of last modification" }, \
479 { "st_ctime", "time of last change" },
480
481
482
483static PyStructSequence_Field stat_result_fields[] = {
484 COMMON_STAT_RESULT_FIELDS
485 {0}
486};
487
488static PyStructSequence_Desc stat_result_desc = {
489 "stat_result",
490 stat_result__doc__,
491 stat_result_fields,
492 10
493};
494
495static PyTypeObject StatResultType;
496
497#ifdef TARGET_API_MAC_OS8
498static PyStructSequence_Field xstat_result_fields[] = {
Jack Jansene54968a2001-10-23 22:28:23 +0000499 COMMON_STAT_RESULT_FIELDS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000500 { "st_rsize" },
501 { "st_creator" },
502 { "st_type "},
503 {0}
504};
505
506static PyStructSequence_Desc xstat_result_desc = {
507 "xstat_result",
508 stat_result__doc__,
509 xstat_result_fields,
510 13
511};
512
513static PyTypeObject XStatResultType;
514#endif
515
516static PyObject *
517_pystat_from_struct_stat(struct stat st, void* _mst)
518{
519 PyObject *v;
520
521#if TARGET_API_MAC_OS8
522 struct macstat *mst;
523
524 if (_mst != NULL)
525 v = PyStructSequence_New(&XStatResultType);
526 else
527#endif
528 v = PyStructSequence_New(&StatResultType);
529 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
530 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
531 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
532 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
533 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
534 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
535 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long)st.st_size));
536 PyStructSequence_SET_ITEM(v, 7,
537 PyFloat_FromDouble((double)st.st_atime));
538 PyStructSequence_SET_ITEM(v, 8,
539 PyFloat_FromDouble((double)st.st_mtime));
540 PyStructSequence_SET_ITEM(v, 9,
541 PyFloat_FromDouble((double)st.st_ctime));
542#if TARGET_API_MAC_OS8
543 if (_mst != NULL) {
544 mst = (struct macstat *) _mst;
545 PyStructSequence_SET_ITEM(v, 10,
546 PyInt_FromLong((long)mst->st_rsize));
547 PyStructSequence_SET_ITEM(v, 11,
548 PyString_FromStringAndSize(mst->st_creator,
549 4));
550 PyStructSequence_SET_ITEM(v, 12,
551 PyString_FromStringAndSize(mst->st_type,
552 4));
553 }
554#endif
555
556 if (PyErr_Occurred()) {
557 Py_DECREF(v);
558 return NULL;
559 }
560
561 return v;
562}
563
564
Jack Jansenf5c20571997-01-30 15:48:07 +0000565static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000566mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000567 PyObject *self;
568 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000569{
Jack Jansen59b912a1996-10-15 16:13:33 +0000570 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000571 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000572 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000573 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000574 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000575 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000576 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000577 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000578 if (res != 0)
579 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000580
581 return _pystat_from_struct_stat(st, NULL);
Jack Jansen40bd7701998-02-20 15:56:19 +0000582}
583
584#ifdef WEHAVE_FSTAT
585static PyObject *
586mac_fstat(self, args)
587 PyObject *self;
588 PyObject *args;
589{
590 struct stat st;
591 long fd;
592 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000593 if (!PyArg_ParseTuple(args, "l", &fd))
Jack Jansen40bd7701998-02-20 15:56:19 +0000594 return NULL;
595 Py_BEGIN_ALLOW_THREADS
596 res = fstat((int)fd, &st);
597 Py_END_ALLOW_THREADS
598 if (res != 0)
599 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000600
601 return _pystat_from_struct_stat(st, NULL);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000602}
Jack Jansen40bd7701998-02-20 15:56:19 +0000603#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000604
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000605#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000606static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000607mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000608 PyObject *self;
609 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000610{
Jack Jansen59b912a1996-10-15 16:13:33 +0000611 struct macstat mst;
612 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000613 char *path;
614 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000615 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000616 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000617 /*
618 ** Convoluted: we want stat() and xstat() to agree, so we call both
619 ** stat and macstat, and use the latter only for values not provided by
620 ** the former.
621 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000622 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000623 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000624 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000625 if (res != 0)
626 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000627 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000628 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000629 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000630 if (res != 0)
631 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000632
633 return _pystat_from_struct_stat(st, (void*) &mst);
Guido van Rossum222c8921995-08-08 14:10:22 +0000634}
Jack Jansene79dc762000-06-02 21:35:07 +0000635#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000636
Jack Jansenf5c20571997-01-30 15:48:07 +0000637static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000638mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000639 PyObject *self;
640 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000641{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000642 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000643 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000644 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000645 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000646 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000647 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000648 if (res != 0)
649 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000650 Py_INCREF(Py_None);
651 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000652}
653
Jack Jansenf5c20571997-01-30 15:48:07 +0000654static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000655mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000656 PyObject *self;
657 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000658{
Guido van Rossum739267b1994-08-29 08:42:37 +0000659 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000660}
661
Jack Jansenf5c20571997-01-30 15:48:07 +0000662static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000663mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000664 PyObject *self;
665 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000666{
667 int fd, size;
668 char *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000669 if (!PyArg_ParseTuple(args, "is#", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000670 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000671 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000672 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000673 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000674 if (size < 0)
675 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000676 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000677}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000678
Jack Jansen46ed2761996-10-23 15:46:57 +0000679#ifdef USE_MALLOC_DEBUG
Jack Jansen8748a0b2000-08-25 22:18:20 +0000680void *mstats(char *);
681
Jack Jansenf5c20571997-01-30 15:48:07 +0000682static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000683mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000684 PyObject*self;
685 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000686{
687 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000688 Py_INCREF(Py_None);
689 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000690}
Jack Jansen4f27a551997-02-20 15:22:17 +0000691#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000692
Jack Jansenf5c20571997-01-30 15:48:07 +0000693static struct PyMethodDef mac_methods[] = {
Jack Jansenddafd2b2001-08-03 13:07:19 +0000694 {"chdir", mac_chdir, 1},
695 {"close", mac_close, 1},
Jack Jansenb6971731996-02-20 16:24:37 +0000696#ifdef WEHAVE_DUP
Jack Jansenddafd2b2001-08-03 13:07:19 +0000697 {"dup", mac_dup, 1},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000698#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000699#ifdef WEHAVE_FDOPEN
Jack Jansenddafd2b2001-08-03 13:07:19 +0000700 {"fdopen", mac_fdopen, 1},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000701#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000702#ifdef WEHAVE_FSTAT
Jack Jansenddafd2b2001-08-03 13:07:19 +0000703 {"fstat", mac_fstat, 1},
Jack Jansen40bd7701998-02-20 15:56:19 +0000704#endif
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000705#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000706 {"getbootvol", mac_getbootvol, 1}, /* non-standard */
Jack Jansene79dc762000-06-02 21:35:07 +0000707#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000708 {"getcwd", mac_getcwd, 1},
709 {"listdir", mac_listdir, 1},
710 {"lseek", mac_lseek, 1},
Jack Jansen243b29b1996-02-21 12:33:50 +0000711 {"mkdir", mac_mkdir, 1},
Jack Jansenddafd2b2001-08-03 13:07:19 +0000712 {"open", mac_open, 1},
713 {"read", mac_read, 1},
714 {"rename", mac_rename, 1},
715 {"rmdir", mac_rmdir, 1},
716 {"stat", mac_stat, 1},
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000717#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000718 {"xstat", mac_xstat, 1},
Jack Jansene79dc762000-06-02 21:35:07 +0000719#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000720 {"sync", mac_sync, 1},
721 {"remove", mac_unlink, 1},
722 {"unlink", mac_unlink, 1},
723 {"write", mac_write, 1},
Jack Jansen46ed2761996-10-23 15:46:57 +0000724#ifdef USE_MALLOC_DEBUG
Jack Jansenddafd2b2001-08-03 13:07:19 +0000725 {"mstats", mac_mstats, 1},
Jack Jansend50e4e11995-01-18 13:58:04 +0000726#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000727
Guido van Rossumce9739b1994-01-05 16:17:15 +0000728 {NULL, NULL} /* Sentinel */
729};
730
Jack Jansenddafd2b2001-08-03 13:07:19 +0000731static int
732ins(PyObject *d, char *symbol, long value)
733{
734 PyObject* v = PyInt_FromLong(value);
735 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
736 return -1; /* triggers fatal error */
737
738 Py_DECREF(v);
739 return 0;
740}
741
742static int
743all_ins(PyObject *d)
744{
745#ifdef F_OK
746 if (ins(d, "F_OK", (long)F_OK)) return -1;
747#endif
748#ifdef R_OK
749 if (ins(d, "R_OK", (long)R_OK)) return -1;
750#endif
751#ifdef W_OK
752 if (ins(d, "W_OK", (long)W_OK)) return -1;
753#endif
754#ifdef X_OK
755 if (ins(d, "X_OK", (long)X_OK)) return -1;
756#endif
757#ifdef NGROUPS_MAX
758 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
759#endif
760#ifdef TMP_MAX
761 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
762#endif
763#ifdef WNOHANG
764 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
765#endif
766#ifdef O_RDONLY
767 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
768#endif
769#ifdef O_WRONLY
770 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
771#endif
772#ifdef O_RDWR
773 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
774#endif
775#ifdef O_NDELAY
776 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
777#endif
778#ifdef O_NONBLOCK
779 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
780#endif
781#ifdef O_APPEND
782 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
783#endif
784#ifdef O_DSYNC
785 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
786#endif
787#ifdef O_RSYNC
788 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
789#endif
790#ifdef O_SYNC
791 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
792#endif
793#ifdef O_NOCTTY
794 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
795#endif
796#ifdef O_CREAT
797 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
798#endif
799#ifdef O_EXCL
800 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
801#endif
802#ifdef O_TRUNC
803 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
804#endif
805#ifdef O_BINARY
806 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
807#endif
808#ifdef O_TEXT
809 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
810#endif
811
812#ifdef HAVE_SPAWNV
813 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
814 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
815 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
816 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
817 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
818#endif
819
820#if defined(PYOS_OS2)
821 if (insertvalues(d)) return -1;
822#endif
823 return 0;
824}
825
Guido van Rossumce9739b1994-01-05 16:17:15 +0000826
827void
828initmac()
829{
Jack Jansenf5c20571997-01-30 15:48:07 +0000830 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000831
Jack Jansenf5c20571997-01-30 15:48:07 +0000832 m = Py_InitModule("mac", mac_methods);
833 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000834
Jack Jansenddafd2b2001-08-03 13:07:19 +0000835 if (all_ins(d))
836 return;
837
Guido van Rossumce9739b1994-01-05 16:17:15 +0000838 /* Initialize mac.error exception */
Jack Jansenb3be2162001-11-30 14:16:36 +0000839 PyDict_SetItemString(d, "error", PyExc_OSError);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000840
841 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
842 PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType);
843
844#if TARGET_API_MAC_OS8
845 PyStructSequence_InitType(&XStatResultType, &xstat_result_desc);
846 PyDict_SetItemString(d, "xstat_result", (PyObject*) &XStatResultType);
847#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000848}