blob: 93dfaee1bf87566a97b8ce35d9d600d4471a9b24 [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
Jack Jansen37d21e12000-04-07 09:25:06 +000061#include <unistd.h>
62#include <fcntl.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000063#else
Jack Jansen12e89e42000-05-12 21:36:29 +000064#define mode_t int
Guido van Rossum87f223c1994-05-06 15:54:15 +000065#include <fcntl.h>
Jack Jansene79dc762000-06-02 21:35:07 +000066#ifdef _POSIX
67#include <unistd.h>
68#include <stat.h>
69#endif
Jack Jansen0c097ea1994-12-14 13:48:38 +000070#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000071
Jack Jansenb6971731996-02-20 16:24:37 +000072/* Optional routines, for some compiler/runtime combinations */
Jack Jansenb6971731996-02-20 16:24:37 +000073#if defined(USE_GUSI) || !defined(__MWERKS__)
74#define WEHAVE_FDOPEN
75#endif
76#if defined(MPW) || defined(USE_GUSI)
77#define WEHAVE_DUP
78#endif
Jack Jansen40bd7701998-02-20 15:56:19 +000079#if defined(USE_GUSI)
80#define WEHAVE_FSTAT
81#endif
Jack Jansen7cbf4801995-01-22 16:52:38 +000082
Guido van Rossumd4d77281994-08-19 10:51:31 +000083#include "macdefs.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000084#ifdef USE_GUSI
85#include <dirent.h>
86#else
Guido van Rossumd4d77281994-08-19 10:51:31 +000087#include "dirent.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000088#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +000089
90#ifndef MAXPATHLEN
91#define MAXPATHLEN 1024
92#endif
93
94/* Prototypes for Unix simulation on Mac */
95
Jack Jansenb6971731996-02-20 16:24:37 +000096#ifndef USE_GUSI
97
Jack Jansend88296d2000-07-11 19:51:05 +000098int chdir(const char *path);
99int mkdir(const char *path, int mode);
100DIR * opendir(char *);
101void closedir(DIR *);
102struct dirent * readdir(DIR *);
103int rmdir(const char *path);
104int sync(void);
Jack Jansenb6971731996-02-20 16:24:37 +0000105
Jack Jansend88296d2000-07-11 19:51:05 +0000106int unlink(const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000107
Jack Jansenb6971731996-02-20 16:24:37 +0000108#endif /* USE_GUSI */
109
Jack Jansend88296d2000-07-11 19:51:05 +0000110char *getwd(char *);
111char *getbootvol(void);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000112
113
Guido van Rossum87f223c1994-05-06 15:54:15 +0000114/* Set a MAC-specific error from errno, and return NULL */
115
Jack Jansenf5c20571997-01-30 15:48:07 +0000116static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000117mac_error()
118{
Jack Jansenb3be2162001-11-30 14:16:36 +0000119 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000120}
121
122/* MAC generic methods */
123
Jack Jansenf5c20571997-01-30 15:48:07 +0000124static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000125mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000126 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000127 int (*func)(const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000128{
129 char *path1;
130 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000131 if (!PyArg_ParseTuple(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000132 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000133 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000134 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000135 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000136 if (res < 0)
137 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000138 Py_INCREF(Py_None);
139 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000140}
141
Jack Jansenf5c20571997-01-30 15:48:07 +0000142static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000143mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000144 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000145 int (*func)(const char *, const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000146{
147 char *path1, *path2;
148 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000149 if (!PyArg_ParseTuple(args, "ss", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000150 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000151 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000152 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000153 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000154 if (res < 0)
155 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000156 Py_INCREF(Py_None);
157 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000158}
159
Jack Jansenf5c20571997-01-30 15:48:07 +0000160static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000161mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000162 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000163 int (*func)(const char *, int);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000164{
165 char *path;
166 int i;
167 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000168 if (!PyArg_ParseTuple(args, "si", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000169 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000170 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000171 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000172 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000173 if (res < 0)
174 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000175 Py_INCREF(Py_None);
176 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000177}
178
Jack Jansenf5c20571997-01-30 15:48:07 +0000179static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000180mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000181 PyObject *self;
182 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000183{
Jack Jansen37d21e12000-04-07 09:25:06 +0000184#ifdef USE_GUSI1
Jack Jansenf5c20571997-01-30 15:48:07 +0000185 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000186
187 /* Change MacOS's idea of wd too */
188 rv = mac_1str(args, chdir);
189 PyMac_FixGUSIcd();
190 return rv;
191#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000192 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000193#endif
194
Guido van Rossum87f223c1994-05-06 15:54:15 +0000195}
196
Jack Jansenf5c20571997-01-30 15:48:07 +0000197static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000198mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000199 PyObject *self;
200 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000201{
202 int fd, res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000203 if (!PyArg_ParseTuple(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000204 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000205 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000206 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000207 Py_END_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000208#ifndef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +0000209 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000210 if (res < 0)
211 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000212#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000213 Py_INCREF(Py_None);
214 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000215}
216
Jack Jansenb6971731996-02-20 16:24:37 +0000217#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000218
Jack Jansenf5c20571997-01-30 15:48:07 +0000219static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000220mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000221 PyObject *self;
222 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000223{
224 int fd;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000225 if (!PyArg_ParseTuple(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000226 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000227 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000228 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000229 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000230 if (fd < 0)
231 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000232 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000233}
234
Jack Jansenb6971731996-02-20 16:24:37 +0000235#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000236
Jack Jansenb6971731996-02-20 16:24:37 +0000237#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000238static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000239mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000240 PyObject *self;
241 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000242{
Jack Jansend88296d2000-07-11 19:51:05 +0000243 extern int fclose(FILE *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000244 int fd;
245 char *mode;
246 FILE *fp;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000247 if (!PyArg_ParseTuple(args, "is", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000248 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000249 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000250 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000251 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000252 if (fp == NULL)
253 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000254 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000255}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000256#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000257
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000258#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000259static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000260mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000261 PyObject *self;
262 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000263{
264 char *res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000265 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000266 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000267 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000268 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000269 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000270 if (res == NULL)
271 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000272 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000273}
Jack Jansene79dc762000-06-02 21:35:07 +0000274#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000275
Jack Jansenf5c20571997-01-30 15:48:07 +0000276static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000277mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000278 PyObject *self;
279 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000280{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000281 char path[MAXPATHLEN];
282 char *res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000283 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000284 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000285 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000286#ifdef USE_GUSI
287 res = getcwd(path, sizeof path);
288#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000289 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000290#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000291 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000292 if (res == NULL) {
Jack Jansenb3be2162001-11-30 14:16:36 +0000293 return mac_error();
Guido van Rossumce9739b1994-01-05 16:17:15 +0000294 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000295 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000296}
297
Jack Jansenf5c20571997-01-30 15:48:07 +0000298static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000299mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000300 PyObject *self;
301 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000302{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000303 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000304 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000305 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000306 struct dirent *ep;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000307 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000308 return NULL;
Jack Jansend7b568a2001-08-11 23:18:55 +0000309#ifdef USE_GUSI
310 /* Work around a bug in GUSI: if you opendir() a file it will
311 ** actually opendir() the parent directory.
312 */
313 {
314 struct stat stb;
315 int res;
316
317 res = stat(name, &stb);
318 if ( res < 0 )
319 return mac_error();
320 if (!S_ISDIR(stb.st_mode) ) {
321 errno = ENOTDIR;
322 return mac_error();
323 }
324 }
325#endif
326
Jack Jansenf5c20571997-01-30 15:48:07 +0000327 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000328 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000329 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000330 return mac_error();
331 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000332 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000333 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000334 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000335 return NULL;
336 }
337 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000338 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000339 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000341 d = NULL;
342 break;
343 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000344 if (PyList_Append(d, v) != 0) {
345 Py_DECREF(v);
346 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000347 d = NULL;
348 break;
349 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000350 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000351 }
352 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000353 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000354
Guido van Rossumce9739b1994-01-05 16:17:15 +0000355 return d;
356}
357
Jack Jansenf5c20571997-01-30 15:48:07 +0000358static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000359mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000360 PyObject *self;
361 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000362{
363 int fd;
364 int where;
365 int how;
366 long res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000367 if (!PyArg_ParseTuple(args, "iii", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000368 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000369 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000370 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000371 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000372 if (res < 0)
373 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000374 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000375}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000376
Jack Jansenf5c20571997-01-30 15:48:07 +0000377static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000378mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000379 PyObject *self;
380 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000381{
Jack Jansen243b29b1996-02-21 12:33:50 +0000382 int res;
383 char *path;
384 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000385 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000386 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000387 Py_BEGIN_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000388#ifdef USE_GUSI1
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000389 res = mkdir(path);
390#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000391 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000392#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000393 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000394 if (res < 0)
395 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000396 Py_INCREF(Py_None);
397 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000398}
399
Jack Jansenf5c20571997-01-30 15:48:07 +0000400static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000401mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000402 PyObject *self;
403 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000404{
405 char *path;
406 int mode;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000407 int perm; /* Accepted but ignored */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000408 int fd;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000409 if (!PyArg_ParseTuple(args, "si|i", &path, &mode, &perm))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000410 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000411 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000412 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000413 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000414 if (fd < 0)
415 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000416 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000417}
418
Jack Jansenf5c20571997-01-30 15:48:07 +0000419static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000420mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000421 PyObject *self;
422 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000423{
424 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000425 PyObject *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000426 if (!PyArg_ParseTuple(args, "ii", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000427 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000428 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000429 if (buffer == NULL)
430 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000431 Py_BEGIN_ALLOW_THREADS
432 size = read(fd, PyString_AsString(buffer), size);
433 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000434 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000435 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000436 return mac_error();
437 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000438 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000439 return buffer;
440}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000441
Jack Jansenf5c20571997-01-30 15:48:07 +0000442static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000443mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000444 PyObject *self;
445 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000446{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000447 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000448}
449
Jack Jansenf5c20571997-01-30 15:48:07 +0000450static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000451mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000452 PyObject *self;
453 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000454{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000455 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000456}
457
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000458static char stat_result__doc__[] =
459"stat_result: Result from stat or lstat.\n\n\
460This object may be accessed either as a tuple of\n\
461 (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
462or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
463\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000464See os.stat for more information.\n";
465
466#define COMMON_STAT_RESULT_FIELDS \
467 { "st_mode", "protection bits" }, \
468 { "st_ino", "inode" }, \
Jack Jansene54968a2001-10-23 22:28:23 +0000469 { "st_dev", "device" }, \
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000470 { "st_nlink", "number of hard links" }, \
Jack Jansene54968a2001-10-23 22:28:23 +0000471 { "st_uid", "user ID of owner" }, \
472 { "st_gid", "group ID of owner" }, \
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000473 { "st_size", "total size, in bytes" }, \
474 { "st_atime", "time of last access" }, \
475 { "st_mtime", "time of last modification" }, \
476 { "st_ctime", "time of last change" },
477
478
479
480static PyStructSequence_Field stat_result_fields[] = {
481 COMMON_STAT_RESULT_FIELDS
482 {0}
483};
484
485static PyStructSequence_Desc stat_result_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000486 "mac.stat_result",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000487 stat_result__doc__,
488 stat_result_fields,
489 10
490};
491
492static PyTypeObject StatResultType;
493
494#ifdef TARGET_API_MAC_OS8
495static PyStructSequence_Field xstat_result_fields[] = {
Jack Jansene54968a2001-10-23 22:28:23 +0000496 COMMON_STAT_RESULT_FIELDS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000497 { "st_rsize" },
498 { "st_creator" },
499 { "st_type "},
500 {0}
501};
502
503static PyStructSequence_Desc xstat_result_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000504 "mac.xstat_result",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000505 stat_result__doc__,
506 xstat_result_fields,
507 13
508};
509
510static PyTypeObject XStatResultType;
511#endif
512
513static PyObject *
514_pystat_from_struct_stat(struct stat st, void* _mst)
515{
516 PyObject *v;
517
518#if TARGET_API_MAC_OS8
519 struct macstat *mst;
520
521 if (_mst != NULL)
522 v = PyStructSequence_New(&XStatResultType);
523 else
524#endif
525 v = PyStructSequence_New(&StatResultType);
526 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
527 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
528 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
529 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
530 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
531 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
532 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long)st.st_size));
533 PyStructSequence_SET_ITEM(v, 7,
534 PyFloat_FromDouble((double)st.st_atime));
535 PyStructSequence_SET_ITEM(v, 8,
536 PyFloat_FromDouble((double)st.st_mtime));
537 PyStructSequence_SET_ITEM(v, 9,
538 PyFloat_FromDouble((double)st.st_ctime));
539#if TARGET_API_MAC_OS8
540 if (_mst != NULL) {
541 mst = (struct macstat *) _mst;
542 PyStructSequence_SET_ITEM(v, 10,
543 PyInt_FromLong((long)mst->st_rsize));
544 PyStructSequence_SET_ITEM(v, 11,
545 PyString_FromStringAndSize(mst->st_creator,
546 4));
547 PyStructSequence_SET_ITEM(v, 12,
548 PyString_FromStringAndSize(mst->st_type,
549 4));
550 }
551#endif
552
553 if (PyErr_Occurred()) {
554 Py_DECREF(v);
555 return NULL;
556 }
557
558 return v;
559}
560
561
Jack Jansenf5c20571997-01-30 15:48:07 +0000562static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000563mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000564 PyObject *self;
565 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000566{
Jack Jansen59b912a1996-10-15 16:13:33 +0000567 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000568 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000569 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000570 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000571 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000572 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000573 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000574 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000575 if (res != 0)
576 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000577
578 return _pystat_from_struct_stat(st, NULL);
Jack Jansen40bd7701998-02-20 15:56:19 +0000579}
580
581#ifdef WEHAVE_FSTAT
582static PyObject *
583mac_fstat(self, args)
584 PyObject *self;
585 PyObject *args;
586{
587 struct stat st;
588 long fd;
589 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000590 if (!PyArg_ParseTuple(args, "l", &fd))
Jack Jansen40bd7701998-02-20 15:56:19 +0000591 return NULL;
592 Py_BEGIN_ALLOW_THREADS
593 res = fstat((int)fd, &st);
594 Py_END_ALLOW_THREADS
595 if (res != 0)
596 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000597
598 return _pystat_from_struct_stat(st, NULL);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000599}
Jack Jansen40bd7701998-02-20 15:56:19 +0000600#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000601
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000602#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000603static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000604mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000605 PyObject *self;
606 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000607{
Jack Jansen59b912a1996-10-15 16:13:33 +0000608 struct macstat mst;
609 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000610 char *path;
611 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000612 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000613 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000614 /*
615 ** Convoluted: we want stat() and xstat() to agree, so we call both
616 ** stat and macstat, and use the latter only for values not provided by
617 ** the former.
618 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000619 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000620 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000621 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000622 if (res != 0)
623 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000624 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000625 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000626 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000627 if (res != 0)
628 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000629
630 return _pystat_from_struct_stat(st, (void*) &mst);
Guido van Rossum222c8921995-08-08 14:10:22 +0000631}
Jack Jansene79dc762000-06-02 21:35:07 +0000632#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000633
Jack Jansenf5c20571997-01-30 15:48:07 +0000634static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000635mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000636 PyObject *self;
637 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000638{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000639 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000640 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000641 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000642 Py_BEGIN_ALLOW_THREADS
Jack Jansen0e5a7332002-03-21 21:09:36 +0000643#ifdef USE_GUSI2
644 sync();
645 res = 0;
646#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000647 res = sync();
Jack Jansen0e5a7332002-03-21 21:09:36 +0000648#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000649 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000650 if (res != 0)
651 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000652 Py_INCREF(Py_None);
653 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000654}
655
Jack Jansenf5c20571997-01-30 15:48:07 +0000656static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000657mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000658 PyObject *self;
659 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000660{
Guido van Rossum739267b1994-08-29 08:42:37 +0000661 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000662}
663
Jack Jansenf5c20571997-01-30 15:48:07 +0000664static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000665mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000666 PyObject *self;
667 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000668{
669 int fd, size;
670 char *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000671 if (!PyArg_ParseTuple(args, "is#", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000672 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000673 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000674 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000675 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000676 if (size < 0)
677 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000678 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000679}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000680
Jack Jansen46ed2761996-10-23 15:46:57 +0000681#ifdef USE_MALLOC_DEBUG
Jack Jansen8748a0b2000-08-25 22:18:20 +0000682void *mstats(char *);
683
Jack Jansenf5c20571997-01-30 15:48:07 +0000684static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000685mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000686 PyObject*self;
687 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000688{
689 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000690 Py_INCREF(Py_None);
691 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000692}
Jack Jansen4f27a551997-02-20 15:22:17 +0000693#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000694
Jack Jansenf5c20571997-01-30 15:48:07 +0000695static struct PyMethodDef mac_methods[] = {
Jack Jansenddafd2b2001-08-03 13:07:19 +0000696 {"chdir", mac_chdir, 1},
697 {"close", mac_close, 1},
Jack Jansenb6971731996-02-20 16:24:37 +0000698#ifdef WEHAVE_DUP
Jack Jansenddafd2b2001-08-03 13:07:19 +0000699 {"dup", mac_dup, 1},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000700#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000701#ifdef WEHAVE_FDOPEN
Jack Jansenddafd2b2001-08-03 13:07:19 +0000702 {"fdopen", mac_fdopen, 1},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000703#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000704#ifdef WEHAVE_FSTAT
Jack Jansenddafd2b2001-08-03 13:07:19 +0000705 {"fstat", mac_fstat, 1},
Jack Jansen40bd7701998-02-20 15:56:19 +0000706#endif
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000707#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000708 {"getbootvol", mac_getbootvol, 1}, /* non-standard */
Jack Jansene79dc762000-06-02 21:35:07 +0000709#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000710 {"getcwd", mac_getcwd, 1},
711 {"listdir", mac_listdir, 1},
712 {"lseek", mac_lseek, 1},
Jack Jansen243b29b1996-02-21 12:33:50 +0000713 {"mkdir", mac_mkdir, 1},
Jack Jansenddafd2b2001-08-03 13:07:19 +0000714 {"open", mac_open, 1},
715 {"read", mac_read, 1},
716 {"rename", mac_rename, 1},
717 {"rmdir", mac_rmdir, 1},
718 {"stat", mac_stat, 1},
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000719#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000720 {"xstat", mac_xstat, 1},
Jack Jansene79dc762000-06-02 21:35:07 +0000721#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000722 {"sync", mac_sync, 1},
723 {"remove", mac_unlink, 1},
724 {"unlink", mac_unlink, 1},
725 {"write", mac_write, 1},
Jack Jansen46ed2761996-10-23 15:46:57 +0000726#ifdef USE_MALLOC_DEBUG
Jack Jansenddafd2b2001-08-03 13:07:19 +0000727 {"mstats", mac_mstats, 1},
Jack Jansend50e4e11995-01-18 13:58:04 +0000728#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000729
Guido van Rossumce9739b1994-01-05 16:17:15 +0000730 {NULL, NULL} /* Sentinel */
731};
732
Jack Jansenddafd2b2001-08-03 13:07:19 +0000733static int
734ins(PyObject *d, char *symbol, long value)
735{
736 PyObject* v = PyInt_FromLong(value);
737 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
738 return -1; /* triggers fatal error */
739
740 Py_DECREF(v);
741 return 0;
742}
743
744static int
745all_ins(PyObject *d)
746{
747#ifdef F_OK
748 if (ins(d, "F_OK", (long)F_OK)) return -1;
749#endif
750#ifdef R_OK
751 if (ins(d, "R_OK", (long)R_OK)) return -1;
752#endif
753#ifdef W_OK
754 if (ins(d, "W_OK", (long)W_OK)) return -1;
755#endif
756#ifdef X_OK
757 if (ins(d, "X_OK", (long)X_OK)) return -1;
758#endif
759#ifdef NGROUPS_MAX
760 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
761#endif
762#ifdef TMP_MAX
763 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
764#endif
765#ifdef WNOHANG
766 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
767#endif
768#ifdef O_RDONLY
769 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
770#endif
771#ifdef O_WRONLY
772 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
773#endif
774#ifdef O_RDWR
775 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
776#endif
777#ifdef O_NDELAY
778 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
779#endif
780#ifdef O_NONBLOCK
781 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
782#endif
783#ifdef O_APPEND
784 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
785#endif
786#ifdef O_DSYNC
787 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
788#endif
789#ifdef O_RSYNC
790 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
791#endif
792#ifdef O_SYNC
793 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
794#endif
795#ifdef O_NOCTTY
796 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
797#endif
798#ifdef O_CREAT
799 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
800#endif
801#ifdef O_EXCL
802 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
803#endif
804#ifdef O_TRUNC
805 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
806#endif
807#ifdef O_BINARY
808 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
809#endif
810#ifdef O_TEXT
811 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
812#endif
813
814#ifdef HAVE_SPAWNV
815 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
816 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
817 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
818 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
819 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
820#endif
821
822#if defined(PYOS_OS2)
823 if (insertvalues(d)) return -1;
824#endif
825 return 0;
826}
827
Guido van Rossumce9739b1994-01-05 16:17:15 +0000828
829void
830initmac()
831{
Jack Jansenf5c20571997-01-30 15:48:07 +0000832 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000833
Jack Jansenf5c20571997-01-30 15:48:07 +0000834 m = Py_InitModule("mac", mac_methods);
835 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000836
Jack Jansenddafd2b2001-08-03 13:07:19 +0000837 if (all_ins(d))
838 return;
839
Guido van Rossumce9739b1994-01-05 16:17:15 +0000840 /* Initialize mac.error exception */
Jack Jansenb3be2162001-11-30 14:16:36 +0000841 PyDict_SetItemString(d, "error", PyExc_OSError);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000842
843 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
844 PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType);
845
846#if TARGET_API_MAC_OS8
847 PyStructSequence_InitType(&XStatResultType, &xstat_result_desc);
848 PyDict_SetItemString(d, "xstat_result", (PyObject*) &XStatResultType);
849#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000850}