blob: 3606b93e52789dd0c2616e2f23e8d95121ccd78d [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
Jack Jansenf5c20571997-01-30 15:48:07 +0000117static PyObject *MacError; /* Exception mac.error */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000118
119/* Set a MAC-specific error from errno, and return NULL */
120
Jack Jansenf5c20571997-01-30 15:48:07 +0000121static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000122mac_error()
123{
Jack Jansenf5c20571997-01-30 15:48:07 +0000124 return PyErr_SetFromErrno(MacError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000125}
126
127/* MAC generic methods */
128
Jack Jansenf5c20571997-01-30 15:48:07 +0000129static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000130mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000131 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000132 int (*func)(const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000133{
134 char *path1;
135 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000136 if (!PyArg_ParseTuple(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000137 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000138 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000139 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000140 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000141 if (res < 0)
142 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000143 Py_INCREF(Py_None);
144 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000145}
146
Jack Jansenf5c20571997-01-30 15:48:07 +0000147static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000148mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000149 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000150 int (*func)(const char *, const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000151{
152 char *path1, *path2;
153 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000154 if (!PyArg_ParseTuple(args, "ss", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000155 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000156 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000157 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000158 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000159 if (res < 0)
160 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000161 Py_INCREF(Py_None);
162 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000163}
164
Jack Jansenf5c20571997-01-30 15:48:07 +0000165static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000166mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000167 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000168 int (*func)(const char *, int);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000169{
170 char *path;
171 int i;
172 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000173 if (!PyArg_ParseTuple(args, "si", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000174 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000175 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000176 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000177 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000178 if (res < 0)
179 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000180 Py_INCREF(Py_None);
181 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000182}
183
Jack Jansenf5c20571997-01-30 15:48:07 +0000184static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000185mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000186 PyObject *self;
187 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000188{
Jack Jansen37d21e12000-04-07 09:25:06 +0000189#ifdef USE_GUSI1
Jack Jansenf5c20571997-01-30 15:48:07 +0000190 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000191
192 /* Change MacOS's idea of wd too */
193 rv = mac_1str(args, chdir);
194 PyMac_FixGUSIcd();
195 return rv;
196#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000197 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000198#endif
199
Guido van Rossum87f223c1994-05-06 15:54:15 +0000200}
201
Jack Jansenf5c20571997-01-30 15:48:07 +0000202static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000203mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000204 PyObject *self;
205 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000206{
207 int fd, res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000208 if (!PyArg_ParseTuple(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000209 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000210 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000211 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000212 Py_END_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000213#ifndef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +0000214 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000215 if (res < 0)
216 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000217#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000218 Py_INCREF(Py_None);
219 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000220}
221
Jack Jansenb6971731996-02-20 16:24:37 +0000222#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000223
Jack Jansenf5c20571997-01-30 15:48:07 +0000224static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000225mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000226 PyObject *self;
227 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000228{
229 int fd;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000230 if (!PyArg_ParseTuple(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000231 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000232 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000233 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000234 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000235 if (fd < 0)
236 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000237 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000238}
239
Jack Jansenb6971731996-02-20 16:24:37 +0000240#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000241
Jack Jansenb6971731996-02-20 16:24:37 +0000242#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000243static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000244mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000245 PyObject *self;
246 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000247{
Jack Jansend88296d2000-07-11 19:51:05 +0000248 extern int fclose(FILE *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000249 int fd;
250 char *mode;
251 FILE *fp;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000252 if (!PyArg_ParseTuple(args, "is", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000253 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000254 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000255 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000256 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000257 if (fp == NULL)
258 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000259 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000260}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000261#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000262
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000263#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000264static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000265mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000266 PyObject *self;
267 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000268{
269 char *res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000270 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000271 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000272 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000273 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000274 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000275 if (res == NULL)
276 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000277 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000278}
Jack Jansene79dc762000-06-02 21:35:07 +0000279#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000280
Jack Jansenf5c20571997-01-30 15:48:07 +0000281static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000282mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000283 PyObject *self;
284 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000285{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000286 char path[MAXPATHLEN];
287 char *res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000288 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000289 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000290 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000291#ifdef USE_GUSI
292 res = getcwd(path, sizeof path);
293#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000294 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000295#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000296 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000297 if (res == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000298 PyErr_SetString(MacError, path);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000299 return NULL;
300 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000301 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000302}
303
Jack Jansenf5c20571997-01-30 15:48:07 +0000304static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000305mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000306 PyObject *self;
307 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000308{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000309 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000310 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000311 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000312 struct dirent *ep;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000313 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000314 return NULL;
Jack Jansend7b568a2001-08-11 23:18:55 +0000315#ifdef USE_GUSI
316 /* Work around a bug in GUSI: if you opendir() a file it will
317 ** actually opendir() the parent directory.
318 */
319 {
320 struct stat stb;
321 int res;
322
323 res = stat(name, &stb);
324 if ( res < 0 )
325 return mac_error();
326 if (!S_ISDIR(stb.st_mode) ) {
327 errno = ENOTDIR;
328 return mac_error();
329 }
330 }
331#endif
332
Jack Jansenf5c20571997-01-30 15:48:07 +0000333 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000334 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000335 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000336 return mac_error();
337 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000338 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000339 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000341 return NULL;
342 }
343 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000344 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000345 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000346 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 if (PyList_Append(d, v) != 0) {
351 Py_DECREF(v);
352 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000353 d = NULL;
354 break;
355 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000356 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000357 }
358 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000359 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000360
Guido van Rossumce9739b1994-01-05 16:17:15 +0000361 return d;
362}
363
Jack Jansenf5c20571997-01-30 15:48:07 +0000364static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000365mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000366 PyObject *self;
367 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000368{
369 int fd;
370 int where;
371 int how;
372 long res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000373 if (!PyArg_ParseTuple(args, "iii", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000374 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000375 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000376 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000377 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000378 if (res < 0)
379 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000380 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000381}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000382
Jack Jansenf5c20571997-01-30 15:48:07 +0000383static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000384mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000385 PyObject *self;
386 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000387{
Jack Jansen243b29b1996-02-21 12:33:50 +0000388 int res;
389 char *path;
390 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000391 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000392 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000393 Py_BEGIN_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000394#ifdef USE_GUSI1
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000395 res = mkdir(path);
396#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000397 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000398#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000399 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000400 if (res < 0)
401 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000402 Py_INCREF(Py_None);
403 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000404}
405
Jack Jansenf5c20571997-01-30 15:48:07 +0000406static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000407mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000408 PyObject *self;
409 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000410{
411 char *path;
412 int mode;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000413 int perm; /* Accepted but ignored */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000414 int fd;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000415 if (!PyArg_ParseTuple(args, "si|i", &path, &mode, &perm))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000416 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000418 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000419 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000420 if (fd < 0)
421 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000422 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000423}
424
Jack Jansenf5c20571997-01-30 15:48:07 +0000425static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000426mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000427 PyObject *self;
428 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000429{
430 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000431 PyObject *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000432 if (!PyArg_ParseTuple(args, "ii", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000433 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000434 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000435 if (buffer == NULL)
436 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000437 Py_BEGIN_ALLOW_THREADS
438 size = read(fd, PyString_AsString(buffer), size);
439 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000440 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000441 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000442 return mac_error();
443 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000444 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000445 return buffer;
446}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000447
Jack Jansenf5c20571997-01-30 15:48:07 +0000448static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000449mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000450 PyObject *self;
451 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000452{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000453 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000454}
455
Jack Jansenf5c20571997-01-30 15:48:07 +0000456static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000457mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000458 PyObject *self;
459 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000460{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000461 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000462}
463
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000464static char stat_result__doc__[] =
465"stat_result: Result from stat or lstat.\n\n\
466This object may be accessed either as a tuple of\n\
467 (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
468or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
469\n\
470Macintosh: The fields st_rsize, st_creator, and st_type are available from\n\
471os.xstat.\n\
472\n\
473See os.stat for more information.\n";
474
475#define COMMON_STAT_RESULT_FIELDS \
476 { "st_mode", "protection bits" }, \
477 { "st_ino", "inode" }, \
478 { "st_dev", "device" }, \
479 { "st_nlink", "number of hard links" }, \
480 { "st_uid", "user ID of owner" }, \
481 { "st_gid", "group ID of owner" }, \
482 { "st_size", "total size, in bytes" }, \
483 { "st_atime", "time of last access" }, \
484 { "st_mtime", "time of last modification" }, \
485 { "st_ctime", "time of last change" },
486
487
488
489static PyStructSequence_Field stat_result_fields[] = {
490 COMMON_STAT_RESULT_FIELDS
491 {0}
492};
493
494static PyStructSequence_Desc stat_result_desc = {
495 "stat_result",
496 stat_result__doc__,
497 stat_result_fields,
498 10
499};
500
501static PyTypeObject StatResultType;
502
503#ifdef TARGET_API_MAC_OS8
504static PyStructSequence_Field xstat_result_fields[] = {
505 COMMON_XSTAT_RESULT_FIELDS
506 { "st_rsize" },
507 { "st_creator" },
508 { "st_type "},
509 {0}
510};
511
512static PyStructSequence_Desc xstat_result_desc = {
513 "xstat_result",
514 stat_result__doc__,
515 xstat_result_fields,
516 13
517};
518
519static PyTypeObject XStatResultType;
520#endif
521
522static PyObject *
523_pystat_from_struct_stat(struct stat st, void* _mst)
524{
525 PyObject *v;
526
527#if TARGET_API_MAC_OS8
528 struct macstat *mst;
529
530 if (_mst != NULL)
531 v = PyStructSequence_New(&XStatResultType);
532 else
533#endif
534 v = PyStructSequence_New(&StatResultType);
535 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
536 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
537 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
538 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
539 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
540 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
541 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long)st.st_size));
542 PyStructSequence_SET_ITEM(v, 7,
543 PyFloat_FromDouble((double)st.st_atime));
544 PyStructSequence_SET_ITEM(v, 8,
545 PyFloat_FromDouble((double)st.st_mtime));
546 PyStructSequence_SET_ITEM(v, 9,
547 PyFloat_FromDouble((double)st.st_ctime));
548#if TARGET_API_MAC_OS8
549 if (_mst != NULL) {
550 mst = (struct macstat *) _mst;
551 PyStructSequence_SET_ITEM(v, 10,
552 PyInt_FromLong((long)mst->st_rsize));
553 PyStructSequence_SET_ITEM(v, 11,
554 PyString_FromStringAndSize(mst->st_creator,
555 4));
556 PyStructSequence_SET_ITEM(v, 12,
557 PyString_FromStringAndSize(mst->st_type,
558 4));
559 }
560#endif
561
562 if (PyErr_Occurred()) {
563 Py_DECREF(v);
564 return NULL;
565 }
566
567 return v;
568}
569
570
Jack Jansenf5c20571997-01-30 15:48:07 +0000571static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000572mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000573 PyObject *self;
574 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000575{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000576 PyObject *v;
Jack Jansen59b912a1996-10-15 16:13:33 +0000577 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000578 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000579 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000580 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000581 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000582 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000583 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000584 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000585 if (res != 0)
586 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000587
588 return _pystat_from_struct_stat(st, NULL);
Jack Jansen40bd7701998-02-20 15:56:19 +0000589}
590
591#ifdef WEHAVE_FSTAT
592static PyObject *
593mac_fstat(self, args)
594 PyObject *self;
595 PyObject *args;
596{
597 struct stat st;
598 long fd;
599 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000600 if (!PyArg_ParseTuple(args, "l", &fd))
Jack Jansen40bd7701998-02-20 15:56:19 +0000601 return NULL;
602 Py_BEGIN_ALLOW_THREADS
603 res = fstat((int)fd, &st);
604 Py_END_ALLOW_THREADS
605 if (res != 0)
606 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000607
608 return _pystat_from_struct_stat(st, NULL);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000609}
Jack Jansen40bd7701998-02-20 15:56:19 +0000610#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000611
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000612#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000613static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000614mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000615 PyObject *self;
616 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000617{
Jack Jansen59b912a1996-10-15 16:13:33 +0000618 struct macstat mst;
619 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000620 char *path;
621 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000622 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000623 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000624 /*
625 ** Convoluted: we want stat() and xstat() to agree, so we call both
626 ** stat and macstat, and use the latter only for values not provided by
627 ** the former.
628 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000629 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000630 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000631 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000632 if (res != 0)
633 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000634 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000635 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000636 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000637 if (res != 0)
638 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000639
640 return _pystat_from_struct_stat(st, (void*) &mst);
Guido van Rossum222c8921995-08-08 14:10:22 +0000641}
Jack Jansene79dc762000-06-02 21:35:07 +0000642#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000643
Jack Jansenf5c20571997-01-30 15:48:07 +0000644static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000645mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000646 PyObject *self;
647 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000648{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000649 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000650 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000651 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000652 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000653 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000654 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000655 if (res != 0)
656 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000657 Py_INCREF(Py_None);
658 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000659}
660
Jack Jansenf5c20571997-01-30 15:48:07 +0000661static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000662mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000663 PyObject *self;
664 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000665{
Guido van Rossum739267b1994-08-29 08:42:37 +0000666 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000667}
668
Jack Jansenf5c20571997-01-30 15:48:07 +0000669static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000670mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000671 PyObject *self;
672 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000673{
674 int fd, size;
675 char *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000676 if (!PyArg_ParseTuple(args, "is#", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000677 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000678 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000679 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000680 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000681 if (size < 0)
682 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000683 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000684}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000685
Jack Jansen46ed2761996-10-23 15:46:57 +0000686#ifdef USE_MALLOC_DEBUG
Jack Jansen8748a0b2000-08-25 22:18:20 +0000687void *mstats(char *);
688
Jack Jansenf5c20571997-01-30 15:48:07 +0000689static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000690mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000691 PyObject*self;
692 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000693{
694 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000695 Py_INCREF(Py_None);
696 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000697}
Jack Jansen4f27a551997-02-20 15:22:17 +0000698#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000699
Jack Jansenf5c20571997-01-30 15:48:07 +0000700static struct PyMethodDef mac_methods[] = {
Jack Jansenddafd2b2001-08-03 13:07:19 +0000701 {"chdir", mac_chdir, 1},
702 {"close", mac_close, 1},
Jack Jansenb6971731996-02-20 16:24:37 +0000703#ifdef WEHAVE_DUP
Jack Jansenddafd2b2001-08-03 13:07:19 +0000704 {"dup", mac_dup, 1},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000705#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000706#ifdef WEHAVE_FDOPEN
Jack Jansenddafd2b2001-08-03 13:07:19 +0000707 {"fdopen", mac_fdopen, 1},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000708#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000709#ifdef WEHAVE_FSTAT
Jack Jansenddafd2b2001-08-03 13:07:19 +0000710 {"fstat", mac_fstat, 1},
Jack Jansen40bd7701998-02-20 15:56:19 +0000711#endif
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000712#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000713 {"getbootvol", mac_getbootvol, 1}, /* non-standard */
Jack Jansene79dc762000-06-02 21:35:07 +0000714#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000715 {"getcwd", mac_getcwd, 1},
716 {"listdir", mac_listdir, 1},
717 {"lseek", mac_lseek, 1},
Jack Jansen243b29b1996-02-21 12:33:50 +0000718 {"mkdir", mac_mkdir, 1},
Jack Jansenddafd2b2001-08-03 13:07:19 +0000719 {"open", mac_open, 1},
720 {"read", mac_read, 1},
721 {"rename", mac_rename, 1},
722 {"rmdir", mac_rmdir, 1},
723 {"stat", mac_stat, 1},
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000724#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000725 {"xstat", mac_xstat, 1},
Jack Jansene79dc762000-06-02 21:35:07 +0000726#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000727 {"sync", mac_sync, 1},
728 {"remove", mac_unlink, 1},
729 {"unlink", mac_unlink, 1},
730 {"write", mac_write, 1},
Jack Jansen46ed2761996-10-23 15:46:57 +0000731#ifdef USE_MALLOC_DEBUG
Jack Jansenddafd2b2001-08-03 13:07:19 +0000732 {"mstats", mac_mstats, 1},
Jack Jansend50e4e11995-01-18 13:58:04 +0000733#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000734
Guido van Rossumce9739b1994-01-05 16:17:15 +0000735 {NULL, NULL} /* Sentinel */
736};
737
Jack Jansenddafd2b2001-08-03 13:07:19 +0000738static int
739ins(PyObject *d, char *symbol, long value)
740{
741 PyObject* v = PyInt_FromLong(value);
742 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
743 return -1; /* triggers fatal error */
744
745 Py_DECREF(v);
746 return 0;
747}
748
749static int
750all_ins(PyObject *d)
751{
752#ifdef F_OK
753 if (ins(d, "F_OK", (long)F_OK)) return -1;
754#endif
755#ifdef R_OK
756 if (ins(d, "R_OK", (long)R_OK)) return -1;
757#endif
758#ifdef W_OK
759 if (ins(d, "W_OK", (long)W_OK)) return -1;
760#endif
761#ifdef X_OK
762 if (ins(d, "X_OK", (long)X_OK)) return -1;
763#endif
764#ifdef NGROUPS_MAX
765 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
766#endif
767#ifdef TMP_MAX
768 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
769#endif
770#ifdef WNOHANG
771 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
772#endif
773#ifdef O_RDONLY
774 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
775#endif
776#ifdef O_WRONLY
777 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
778#endif
779#ifdef O_RDWR
780 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
781#endif
782#ifdef O_NDELAY
783 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
784#endif
785#ifdef O_NONBLOCK
786 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
787#endif
788#ifdef O_APPEND
789 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
790#endif
791#ifdef O_DSYNC
792 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
793#endif
794#ifdef O_RSYNC
795 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
796#endif
797#ifdef O_SYNC
798 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
799#endif
800#ifdef O_NOCTTY
801 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
802#endif
803#ifdef O_CREAT
804 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
805#endif
806#ifdef O_EXCL
807 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
808#endif
809#ifdef O_TRUNC
810 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
811#endif
812#ifdef O_BINARY
813 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
814#endif
815#ifdef O_TEXT
816 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
817#endif
818
819#ifdef HAVE_SPAWNV
820 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
821 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
822 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
823 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
824 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
825#endif
826
827#if defined(PYOS_OS2)
828 if (insertvalues(d)) return -1;
829#endif
830 return 0;
831}
832
Guido van Rossumce9739b1994-01-05 16:17:15 +0000833
834void
835initmac()
836{
Jack Jansenf5c20571997-01-30 15:48:07 +0000837 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000838
Jack Jansenf5c20571997-01-30 15:48:07 +0000839 m = Py_InitModule("mac", mac_methods);
840 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000841
Jack Jansenddafd2b2001-08-03 13:07:19 +0000842 if (all_ins(d))
843 return;
844
Guido van Rossumce9739b1994-01-05 16:17:15 +0000845 /* Initialize mac.error exception */
Jack Jansen55e39271997-10-07 21:47:25 +0000846 MacError = PyErr_NewException("mac.error", NULL, NULL);
847 PyDict_SetItemString(d, "error", MacError);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000848
849 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
850 PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType);
851
852#if TARGET_API_MAC_OS8
853 PyStructSequence_InitType(&XStatResultType, &xstat_result_desc);
854 PyDict_SetItemString(d, "xstat_result", (PyObject*) &XStatResultType);
855#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000856}