blob: d7cc1bc5e38e75789d009ef749bf5ce9b6bc8823 [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{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000573 PyObject *v;
Jack Jansen59b912a1996-10-15 16:13:33 +0000574 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000575 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000576 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000577 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000578 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000579 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000580 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000581 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000582 if (res != 0)
583 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000584
585 return _pystat_from_struct_stat(st, NULL);
Jack Jansen40bd7701998-02-20 15:56:19 +0000586}
587
588#ifdef WEHAVE_FSTAT
589static PyObject *
590mac_fstat(self, args)
591 PyObject *self;
592 PyObject *args;
593{
594 struct stat st;
595 long fd;
596 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000597 if (!PyArg_ParseTuple(args, "l", &fd))
Jack Jansen40bd7701998-02-20 15:56:19 +0000598 return NULL;
599 Py_BEGIN_ALLOW_THREADS
600 res = fstat((int)fd, &st);
601 Py_END_ALLOW_THREADS
602 if (res != 0)
603 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000604
605 return _pystat_from_struct_stat(st, NULL);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000606}
Jack Jansen40bd7701998-02-20 15:56:19 +0000607#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000608
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000609#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000610static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000611mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000612 PyObject *self;
613 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000614{
Jack Jansen59b912a1996-10-15 16:13:33 +0000615 struct macstat mst;
616 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000617 char *path;
618 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000619 if (!PyArg_ParseTuple(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000620 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000621 /*
622 ** Convoluted: we want stat() and xstat() to agree, so we call both
623 ** stat and macstat, and use the latter only for values not provided by
624 ** the former.
625 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000626 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000627 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000628 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000629 if (res != 0)
630 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000631 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000632 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000633 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000634 if (res != 0)
635 return mac_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000636
637 return _pystat_from_struct_stat(st, (void*) &mst);
Guido van Rossum222c8921995-08-08 14:10:22 +0000638}
Jack Jansene79dc762000-06-02 21:35:07 +0000639#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000640
Jack Jansenf5c20571997-01-30 15:48:07 +0000641static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000642mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000643 PyObject *self;
644 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000645{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000646 int res;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000647 if (!PyArg_ParseTuple(args, ""))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000648 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000649 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000650 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000651 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000652 if (res != 0)
653 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000654 Py_INCREF(Py_None);
655 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000656}
657
Jack Jansenf5c20571997-01-30 15:48:07 +0000658static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000659mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000660 PyObject *self;
661 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000662{
Guido van Rossum739267b1994-08-29 08:42:37 +0000663 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000664}
665
Jack Jansenf5c20571997-01-30 15:48:07 +0000666static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000667mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000668 PyObject *self;
669 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000670{
671 int fd, size;
672 char *buffer;
Jack Jansenddafd2b2001-08-03 13:07:19 +0000673 if (!PyArg_ParseTuple(args, "is#", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000674 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000675 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000676 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000677 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000678 if (size < 0)
679 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000680 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000681}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000682
Jack Jansen46ed2761996-10-23 15:46:57 +0000683#ifdef USE_MALLOC_DEBUG
Jack Jansen8748a0b2000-08-25 22:18:20 +0000684void *mstats(char *);
685
Jack Jansenf5c20571997-01-30 15:48:07 +0000686static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000687mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000688 PyObject*self;
689 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000690{
691 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000692 Py_INCREF(Py_None);
693 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000694}
Jack Jansen4f27a551997-02-20 15:22:17 +0000695#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000696
Jack Jansenf5c20571997-01-30 15:48:07 +0000697static struct PyMethodDef mac_methods[] = {
Jack Jansenddafd2b2001-08-03 13:07:19 +0000698 {"chdir", mac_chdir, 1},
699 {"close", mac_close, 1},
Jack Jansenb6971731996-02-20 16:24:37 +0000700#ifdef WEHAVE_DUP
Jack Jansenddafd2b2001-08-03 13:07:19 +0000701 {"dup", mac_dup, 1},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000702#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000703#ifdef WEHAVE_FDOPEN
Jack Jansenddafd2b2001-08-03 13:07:19 +0000704 {"fdopen", mac_fdopen, 1},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000705#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000706#ifdef WEHAVE_FSTAT
Jack Jansenddafd2b2001-08-03 13:07:19 +0000707 {"fstat", mac_fstat, 1},
Jack Jansen40bd7701998-02-20 15:56:19 +0000708#endif
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000709#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000710 {"getbootvol", mac_getbootvol, 1}, /* non-standard */
Jack Jansene79dc762000-06-02 21:35:07 +0000711#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000712 {"getcwd", mac_getcwd, 1},
713 {"listdir", mac_listdir, 1},
714 {"lseek", mac_lseek, 1},
Jack Jansen243b29b1996-02-21 12:33:50 +0000715 {"mkdir", mac_mkdir, 1},
Jack Jansenddafd2b2001-08-03 13:07:19 +0000716 {"open", mac_open, 1},
717 {"read", mac_read, 1},
718 {"rename", mac_rename, 1},
719 {"rmdir", mac_rmdir, 1},
720 {"stat", mac_stat, 1},
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000721#if TARGET_API_MAC_OS8
Jack Jansenddafd2b2001-08-03 13:07:19 +0000722 {"xstat", mac_xstat, 1},
Jack Jansene79dc762000-06-02 21:35:07 +0000723#endif
Jack Jansenddafd2b2001-08-03 13:07:19 +0000724 {"sync", mac_sync, 1},
725 {"remove", mac_unlink, 1},
726 {"unlink", mac_unlink, 1},
727 {"write", mac_write, 1},
Jack Jansen46ed2761996-10-23 15:46:57 +0000728#ifdef USE_MALLOC_DEBUG
Jack Jansenddafd2b2001-08-03 13:07:19 +0000729 {"mstats", mac_mstats, 1},
Jack Jansend50e4e11995-01-18 13:58:04 +0000730#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000731
Guido van Rossumce9739b1994-01-05 16:17:15 +0000732 {NULL, NULL} /* Sentinel */
733};
734
Jack Jansenddafd2b2001-08-03 13:07:19 +0000735static int
736ins(PyObject *d, char *symbol, long value)
737{
738 PyObject* v = PyInt_FromLong(value);
739 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
740 return -1; /* triggers fatal error */
741
742 Py_DECREF(v);
743 return 0;
744}
745
746static int
747all_ins(PyObject *d)
748{
749#ifdef F_OK
750 if (ins(d, "F_OK", (long)F_OK)) return -1;
751#endif
752#ifdef R_OK
753 if (ins(d, "R_OK", (long)R_OK)) return -1;
754#endif
755#ifdef W_OK
756 if (ins(d, "W_OK", (long)W_OK)) return -1;
757#endif
758#ifdef X_OK
759 if (ins(d, "X_OK", (long)X_OK)) return -1;
760#endif
761#ifdef NGROUPS_MAX
762 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
763#endif
764#ifdef TMP_MAX
765 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
766#endif
767#ifdef WNOHANG
768 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
769#endif
770#ifdef O_RDONLY
771 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
772#endif
773#ifdef O_WRONLY
774 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
775#endif
776#ifdef O_RDWR
777 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
778#endif
779#ifdef O_NDELAY
780 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
781#endif
782#ifdef O_NONBLOCK
783 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
784#endif
785#ifdef O_APPEND
786 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
787#endif
788#ifdef O_DSYNC
789 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
790#endif
791#ifdef O_RSYNC
792 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
793#endif
794#ifdef O_SYNC
795 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
796#endif
797#ifdef O_NOCTTY
798 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
799#endif
800#ifdef O_CREAT
801 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
802#endif
803#ifdef O_EXCL
804 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
805#endif
806#ifdef O_TRUNC
807 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
808#endif
809#ifdef O_BINARY
810 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
811#endif
812#ifdef O_TEXT
813 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
814#endif
815
816#ifdef HAVE_SPAWNV
817 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
818 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
819 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
820 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
821 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
822#endif
823
824#if defined(PYOS_OS2)
825 if (insertvalues(d)) return -1;
826#endif
827 return 0;
828}
829
Guido van Rossumce9739b1994-01-05 16:17:15 +0000830
831void
832initmac()
833{
Jack Jansenf5c20571997-01-30 15:48:07 +0000834 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000835
Jack Jansenf5c20571997-01-30 15:48:07 +0000836 m = Py_InitModule("mac", mac_methods);
837 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000838
Jack Jansenddafd2b2001-08-03 13:07:19 +0000839 if (all_ins(d))
840 return;
841
Guido van Rossumce9739b1994-01-05 16:17:15 +0000842 /* Initialize mac.error exception */
Jack Jansen55e39271997-10-07 21:47:25 +0000843 MacError = PyErr_NewException("mac.error", NULL, NULL);
844 PyDict_SetItemString(d, "error", MacError);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000845
846 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
847 PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType);
848
849#if TARGET_API_MAC_OS8
850 PyStructSequence_InitType(&XStatResultType, &xstat_result_desc);
851 PyDict_SetItemString(d, "xstat_result", (PyObject*) &XStatResultType);
852#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000853}