blob: f827d9cf895b274b2c55e7f093d3a3169e3b4759 [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\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000470See os.stat for more information.\n";
471
472#define COMMON_STAT_RESULT_FIELDS \
473 { "st_mode", "protection bits" }, \
474 { "st_ino", "inode" }, \
Jack Jansene54968a2001-10-23 22:28:23 +0000475 { "st_dev", "device" }, \
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000476 { "st_nlink", "number of hard links" }, \
Jack Jansene54968a2001-10-23 22:28:23 +0000477 { "st_uid", "user ID of owner" }, \
478 { "st_gid", "group ID of owner" }, \
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000479 { "st_size", "total size, in bytes" }, \
480 { "st_atime", "time of last access" }, \
481 { "st_mtime", "time of last modification" }, \
482 { "st_ctime", "time of last change" },
483
484
485
486static PyStructSequence_Field stat_result_fields[] = {
487 COMMON_STAT_RESULT_FIELDS
488 {0}
489};
490
491static PyStructSequence_Desc stat_result_desc = {
492 "stat_result",
493 stat_result__doc__,
494 stat_result_fields,
495 10
496};
497
498static PyTypeObject StatResultType;
499
500#ifdef TARGET_API_MAC_OS8
501static PyStructSequence_Field xstat_result_fields[] = {
Jack Jansene54968a2001-10-23 22:28:23 +0000502 COMMON_STAT_RESULT_FIELDS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000503 { "st_rsize" },
504 { "st_creator" },
505 { "st_type "},
506 {0}
507};
508
509static PyStructSequence_Desc xstat_result_desc = {
510 "xstat_result",
511 stat_result__doc__,
512 xstat_result_fields,
513 13
514};
515
516static PyTypeObject XStatResultType;
517#endif
518
519static PyObject *
520_pystat_from_struct_stat(struct stat st, void* _mst)
521{
522 PyObject *v;
523
524#if TARGET_API_MAC_OS8
525 struct macstat *mst;
526
527 if (_mst != NULL)
528 v = PyStructSequence_New(&XStatResultType);
529 else
530#endif
531 v = PyStructSequence_New(&StatResultType);
532 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
533 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
534 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
535 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
536 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
537 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
538 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long)st.st_size));
539 PyStructSequence_SET_ITEM(v, 7,
540 PyFloat_FromDouble((double)st.st_atime));
541 PyStructSequence_SET_ITEM(v, 8,
542 PyFloat_FromDouble((double)st.st_mtime));
543 PyStructSequence_SET_ITEM(v, 9,
544 PyFloat_FromDouble((double)st.st_ctime));
545#if TARGET_API_MAC_OS8
546 if (_mst != NULL) {
547 mst = (struct macstat *) _mst;
548 PyStructSequence_SET_ITEM(v, 10,
549 PyInt_FromLong((long)mst->st_rsize));
550 PyStructSequence_SET_ITEM(v, 11,
551 PyString_FromStringAndSize(mst->st_creator,
552 4));
553 PyStructSequence_SET_ITEM(v, 12,
554 PyString_FromStringAndSize(mst->st_type,
555 4));
556 }
557#endif
558
559 if (PyErr_Occurred()) {
560 Py_DECREF(v);
561 return NULL;
562 }
563
564 return v;
565}
566
567
Jack Jansenf5c20571997-01-30 15:48:07 +0000568static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000569mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000570 PyObject *self;
571 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000572{
Jack Jansen59b912a1996-10-15 16:13:33 +0000573 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000574 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000575 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000576 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000577 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000578 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000579 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000580 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000581 if (res != 0)
582 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000583
584 return _pystat_from_struct_stat(st, NULL);
Jack Jansen40bd7701998-02-20 15:56:19 +0000585}
586
587#ifdef WEHAVE_FSTAT
588static PyObject *
589mac_fstat(self, args)
590 PyObject *self;
591 PyObject *args;
592{
593 struct stat st;
594 long fd;
595 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000596 if (!PyArg_ParseTuple(args, "l", &fd))
Jack Jansen40bd7701998-02-20 15:56:19 +0000597 return NULL;
598 Py_BEGIN_ALLOW_THREADS
599 res = fstat((int)fd, &st);
600 Py_END_ALLOW_THREADS
601 if (res != 0)
602 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000603
604 return _pystat_from_struct_stat(st, NULL);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000605}
Jack Jansen40bd7701998-02-20 15:56:19 +0000606#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000607
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000608#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000609static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000610mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000611 PyObject *self;
612 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000613{
Jack Jansen59b912a1996-10-15 16:13:33 +0000614 struct macstat mst;
615 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000616 char *path;
617 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000618 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000619 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000620 /*
621 ** Convoluted: we want stat() and xstat() to agree, so we call both
622 ** stat and macstat, and use the latter only for values not provided by
623 ** the former.
624 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000625 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000626 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000627 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000628 if (res != 0)
629 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000630 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000631 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000632 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000633 if (res != 0)
634 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000635
636 return _pystat_from_struct_stat(st, (void*) &mst);
Guido van Rossum222c8921995-08-08 14:10:22 +0000637}
Jack Jansene79dc762000-06-02 21:35:07 +0000638#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000639
Jack Jansenf5c20571997-01-30 15:48:07 +0000640static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000641mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000642 PyObject *self;
643 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000644{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000645 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000646 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000647 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000648 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000649 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000650 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000651 if (res != 0)
652 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000653 Py_INCREF(Py_None);
654 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000655}
656
Jack Jansenf5c20571997-01-30 15:48:07 +0000657static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000658mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000659 PyObject *self;
660 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000661{
Guido van Rossum739267b1994-08-29 08:42:37 +0000662 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000663}
664
Jack Jansenf5c20571997-01-30 15:48:07 +0000665static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000666mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000667 PyObject *self;
668 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000669{
670 int fd, size;
671 char *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000672 if (!PyArg_ParseTuple(args, "is#", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000673 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000674 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000675 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000676 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000677 if (size < 0)
678 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000679 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000680}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000681
Jack Jansen46ed2761996-10-23 15:46:57 +0000682#ifdef USE_MALLOC_DEBUG
Jack Jansen8748a0b2000-08-25 22:18:20 +0000683void *mstats(char *);
684
Jack Jansenf5c20571997-01-30 15:48:07 +0000685static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000686mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000687 PyObject*self;
688 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000689{
690 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000691 Py_INCREF(Py_None);
692 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000693}
Jack Jansen4f27a551997-02-20 15:22:17 +0000694#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000695
Jack Jansenf5c20571997-01-30 15:48:07 +0000696static struct PyMethodDef mac_methods[] = {
Jack Jansenddafd2b2001-08-03 13:07:19 +0000697 {"chdir", mac_chdir, 1},
698 {"close", mac_close, 1},
Jack Jansenb6971731996-02-20 16:24:37 +0000699#ifdef WEHAVE_DUP
Jack Jansenddafd2b2001-08-03 13:07:19 +0000700 {"dup", mac_dup, 1},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000701#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000702#ifdef WEHAVE_FDOPEN
Jack Jansenddafd2b2001-08-03 13:07:19 +0000703 {"fdopen", mac_fdopen, 1},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000704#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000705#ifdef WEHAVE_FSTAT
Jack Jansenddafd2b2001-08-03 13:07:19 +0000706 {"fstat", mac_fstat, 1},
Jack Jansen40bd7701998-02-20 15:56:19 +0000707#endif
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000708#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000709 {"getbootvol", mac_getbootvol, 1}, /* non-standard */
Jack Jansene79dc762000-06-02 21:35:07 +0000710#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000711 {"getcwd", mac_getcwd, 1},
712 {"listdir", mac_listdir, 1},
713 {"lseek", mac_lseek, 1},
Jack Jansen243b29b1996-02-21 12:33:50 +0000714 {"mkdir", mac_mkdir, 1},
Jack Jansenddafd2b2001-08-03 13:07:19 +0000715 {"open", mac_open, 1},
716 {"read", mac_read, 1},
717 {"rename", mac_rename, 1},
718 {"rmdir", mac_rmdir, 1},
719 {"stat", mac_stat, 1},
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000720#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000721 {"xstat", mac_xstat, 1},
Jack Jansene79dc762000-06-02 21:35:07 +0000722#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000723 {"sync", mac_sync, 1},
724 {"remove", mac_unlink, 1},
725 {"unlink", mac_unlink, 1},
726 {"write", mac_write, 1},
Jack Jansen46ed2761996-10-23 15:46:57 +0000727#ifdef USE_MALLOC_DEBUG
Jack Jansenddafd2b2001-08-03 13:07:19 +0000728 {"mstats", mac_mstats, 1},
Jack Jansend50e4e11995-01-18 13:58:04 +0000729#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000730
Guido van Rossumce9739b1994-01-05 16:17:15 +0000731 {NULL, NULL} /* Sentinel */
732};
733
Jack Jansenddafd2b2001-08-03 13:07:19 +0000734static int
735ins(PyObject *d, char *symbol, long value)
736{
737 PyObject* v = PyInt_FromLong(value);
738 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
739 return -1; /* triggers fatal error */
740
741 Py_DECREF(v);
742 return 0;
743}
744
745static int
746all_ins(PyObject *d)
747{
748#ifdef F_OK
749 if (ins(d, "F_OK", (long)F_OK)) return -1;
750#endif
751#ifdef R_OK
752 if (ins(d, "R_OK", (long)R_OK)) return -1;
753#endif
754#ifdef W_OK
755 if (ins(d, "W_OK", (long)W_OK)) return -1;
756#endif
757#ifdef X_OK
758 if (ins(d, "X_OK", (long)X_OK)) return -1;
759#endif
760#ifdef NGROUPS_MAX
761 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
762#endif
763#ifdef TMP_MAX
764 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
765#endif
766#ifdef WNOHANG
767 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
768#endif
769#ifdef O_RDONLY
770 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
771#endif
772#ifdef O_WRONLY
773 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
774#endif
775#ifdef O_RDWR
776 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
777#endif
778#ifdef O_NDELAY
779 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
780#endif
781#ifdef O_NONBLOCK
782 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
783#endif
784#ifdef O_APPEND
785 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
786#endif
787#ifdef O_DSYNC
788 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
789#endif
790#ifdef O_RSYNC
791 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
792#endif
793#ifdef O_SYNC
794 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
795#endif
796#ifdef O_NOCTTY
797 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
798#endif
799#ifdef O_CREAT
800 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
801#endif
802#ifdef O_EXCL
803 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
804#endif
805#ifdef O_TRUNC
806 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
807#endif
808#ifdef O_BINARY
809 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
810#endif
811#ifdef O_TEXT
812 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
813#endif
814
815#ifdef HAVE_SPAWNV
816 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
817 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
818 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
819 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
820 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
821#endif
822
823#if defined(PYOS_OS2)
824 if (insertvalues(d)) return -1;
825#endif
826 return 0;
827}
828
Guido van Rossumce9739b1994-01-05 16:17:15 +0000829
830void
831initmac()
832{
Jack Jansenf5c20571997-01-30 15:48:07 +0000833 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000834
Jack Jansenf5c20571997-01-30 15:48:07 +0000835 m = Py_InitModule("mac", mac_methods);
836 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000837
Jack Jansenddafd2b2001-08-03 13:07:19 +0000838 if (all_ins(d))
839 return;
840
Guido van Rossumce9739b1994-01-05 16:17:15 +0000841 /* Initialize mac.error exception */
Jack Jansen55e39271997-10-07 21:47:25 +0000842 MacError = PyErr_NewException("mac.error", NULL, NULL);
843 PyDict_SetItemString(d, "error", MacError);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000844
845 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
846 PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType);
847
848#if TARGET_API_MAC_OS8
849 PyStructSequence_InitType(&XStatResultType, &xstat_result_desc);
850 PyDict_SetItemString(d, "xstat_result", (PyObject*) &XStatResultType);
851#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000852}