blob: b2849121a5e6a2d2182813b9951df64f3fa5ca0a [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 Rossum87f223c1994-05-06 15:54:15 +000028#include "ceval.h"
Guido van Rossumce9739b1994-01-05 16:17:15 +000029
Guido van Rossum87f223c1994-05-06 15:54:15 +000030#include <stdio.h>
31#include <string.h>
32#include <errno.h>
Guido van Rossum739267b1994-08-29 08:42:37 +000033
34#ifdef THINK_C
35#include "unix.h"
36#undef S_IFMT
37#undef S_IFDIR
38#undef S_IFCHR
39#undef S_IFBLK
40#undef S_IFREG
41#undef S_ISDIR
42#undef S_ISREG
Jack Jansen37d21e12000-04-07 09:25:06 +000043#endif /* THINK_C */
Guido van Rossum739267b1994-08-29 08:42:37 +000044
Jack Jansen59b912a1996-10-15 16:13:33 +000045#include "macstat.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000046#ifdef USE_GUSI
Jack Jansen59b912a1996-10-15 16:13:33 +000047/* Remove defines from macstat.h */
48#undef S_IFMT
49#undef S_IFDIR
50#undef S_IFREG
51#undef S_IREAD
52#undef S_IWRITE
53#undef S_IEXEC
54
Jack Jansen37d21e12000-04-07 09:25:06 +000055#ifdef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +000056#include <GUSI.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000057#endif /* USE_GUSI1 */
Jack Jansenc743c8d1996-02-14 16:02:30 +000058#include <sys/types.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000059#include <sys/stat.h>
60#else /* USE_GUSI */
Jack Jansen59b912a1996-10-15 16:13:33 +000061#define stat macstat
Jack Jansen37d21e12000-04-07 09:25:06 +000062#endif /* USE_GUSI */
Guido van Rossumce9739b1994-01-05 16:17:15 +000063
Jack Jansen37d21e12000-04-07 09:25:06 +000064#ifdef USE_GUSI2
65#define sync bad_sync
66#include <unistd.h>
67#include <fcntl.h>
68#undef sync
69int sync(void);
70#else
71#ifdef x__MWERKS__
Jack Jansen9eeb82d1995-01-26 16:34:53 +000072#include <unix.h>
73#else
Guido van Rossum87f223c1994-05-06 15:54:15 +000074#include <fcntl.h>
Jack Jansen0c097ea1994-12-14 13:48:38 +000075#endif
Jack Jansen37d21e12000-04-07 09:25:06 +000076#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000077
Jack Jansenb6971731996-02-20 16:24:37 +000078/* Optional routines, for some compiler/runtime combinations */
Jack Jansenb6971731996-02-20 16:24:37 +000079#if defined(USE_GUSI) || !defined(__MWERKS__)
80#define WEHAVE_FDOPEN
81#endif
82#if defined(MPW) || defined(USE_GUSI)
83#define WEHAVE_DUP
84#endif
Jack Jansen40bd7701998-02-20 15:56:19 +000085#if defined(USE_GUSI)
86#define WEHAVE_FSTAT
87#endif
Jack Jansen7cbf4801995-01-22 16:52:38 +000088
Guido van Rossumd4d77281994-08-19 10:51:31 +000089#include "macdefs.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000090#ifdef USE_GUSI
91#include <dirent.h>
92#else
Guido van Rossumd4d77281994-08-19 10:51:31 +000093#include "dirent.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000094#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +000095
96#ifndef MAXPATHLEN
97#define MAXPATHLEN 1024
98#endif
99
100/* Prototypes for Unix simulation on Mac */
101
Jack Jansenb6971731996-02-20 16:24:37 +0000102#ifndef USE_GUSI
103
Jack Jansenf5c20571997-01-30 15:48:07 +0000104int chdir Py_PROTO((const char *path));
105int mkdir Py_PROTO((const char *path, int mode));
106DIR * opendir Py_PROTO((char *));
107void closedir Py_PROTO((DIR *));
108struct dirent * readdir Py_PROTO((DIR *));
109int rmdir Py_PROTO((const char *path));
110int sync Py_PROTO((void));
Jack Jansenb6971731996-02-20 16:24:37 +0000111
112#if defined(THINK_C) || defined(__SC__)
Jack Jansenf5c20571997-01-30 15:48:07 +0000113int unlink Py_PROTO((char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000114#else
Jack Jansenf5c20571997-01-30 15:48:07 +0000115int unlink Py_PROTO((const char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000116#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000117
Jack Jansenb6971731996-02-20 16:24:37 +0000118#endif /* USE_GUSI */
119
Jack Jansenf5c20571997-01-30 15:48:07 +0000120char *getwd Py_PROTO((char *));
121char *getbootvol Py_PROTO((void));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000122
123
Jack Jansenf5c20571997-01-30 15:48:07 +0000124static PyObject *MacError; /* Exception mac.error */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000125
126/* Set a MAC-specific error from errno, and return NULL */
127
Jack Jansenf5c20571997-01-30 15:48:07 +0000128static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000129mac_error()
130{
Jack Jansenf5c20571997-01-30 15:48:07 +0000131 return PyErr_SetFromErrno(MacError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000132}
133
134/* MAC generic methods */
135
Jack Jansenf5c20571997-01-30 15:48:07 +0000136static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000137mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000138 PyObject *args;
139 int (*func) Py_FPROTO((const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000140{
141 char *path1;
142 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000143 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000144 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000145 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000146 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000147 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000148 if (res < 0)
149 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000150 Py_INCREF(Py_None);
151 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000152}
153
Jack Jansenf5c20571997-01-30 15:48:07 +0000154static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000155mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000156 PyObject *args;
157 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000158{
159 char *path1, *path2;
160 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000161 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000162 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000163 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000164 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000165 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000166 if (res < 0)
167 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000168 Py_INCREF(Py_None);
169 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000170}
171
Jack Jansenf5c20571997-01-30 15:48:07 +0000172static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000173mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000174 PyObject *args;
175 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000176{
177 char *path;
178 int i;
179 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000180 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000181 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000182 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000183 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000184 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000185 if (res < 0)
186 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000187 Py_INCREF(Py_None);
188 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000189}
190
Jack Jansenf5c20571997-01-30 15:48:07 +0000191static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000192mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000193 PyObject *self;
194 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000195{
Jack Jansen37d21e12000-04-07 09:25:06 +0000196#ifdef USE_GUSI1
Jack Jansenf5c20571997-01-30 15:48:07 +0000197 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000198
199 /* Change MacOS's idea of wd too */
200 rv = mac_1str(args, chdir);
201 PyMac_FixGUSIcd();
202 return rv;
203#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000204 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000205#endif
206
Guido van Rossum87f223c1994-05-06 15:54:15 +0000207}
208
Jack Jansenf5c20571997-01-30 15:48:07 +0000209static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000210mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000211 PyObject *self;
212 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000213{
214 int fd, res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000215 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000216 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000217 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000218 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000219 Py_END_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000220#ifndef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +0000221 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000222 if (res < 0)
223 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000224#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000225 Py_INCREF(Py_None);
226 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000227}
228
Jack Jansenb6971731996-02-20 16:24:37 +0000229#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000230
Jack Jansenf5c20571997-01-30 15:48:07 +0000231static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000232mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000233 PyObject *self;
234 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000235{
236 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000237 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000238 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000239 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000240 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000241 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000242 if (fd < 0)
243 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000244 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000245}
246
Jack Jansenb6971731996-02-20 16:24:37 +0000247#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000248
Jack Jansenb6971731996-02-20 16:24:37 +0000249#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000250static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000251mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000252 PyObject *self;
253 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000254{
Jack Jansenf5c20571997-01-30 15:48:07 +0000255 extern int fclose Py_PROTO((FILE *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000256 int fd;
257 char *mode;
258 FILE *fp;
Jack Jansenf5c20571997-01-30 15:48:07 +0000259 if (!PyArg_Parse(args, "(is)", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000260 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000261 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000262 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000263 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000264 if (fp == NULL)
265 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000266 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000267}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000268#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000269
Jack Jansenf5c20571997-01-30 15:48:07 +0000270static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000271mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000272 PyObject *self;
273 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000274{
275 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000276 if (!PyArg_NoArgs(args))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000277 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000278 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000279 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000280 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000281 if (res == NULL)
282 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000283 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000284}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000285
Jack Jansenf5c20571997-01-30 15:48:07 +0000286static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000287mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000288 PyObject *self;
289 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000290{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000291 char path[MAXPATHLEN];
292 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000293 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000294 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000295 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000296#ifdef USE_GUSI
297 res = getcwd(path, sizeof path);
298#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000299 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000300#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000301 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000302 if (res == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000303 PyErr_SetString(MacError, path);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000304 return NULL;
305 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000306 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000307}
308
Jack Jansenf5c20571997-01-30 15:48:07 +0000309static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000310mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000311 PyObject *self;
312 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000313{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000314 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000315 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000316 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000317 struct dirent *ep;
Jack Jansenf5c20571997-01-30 15:48:07 +0000318 if (!PyArg_Parse(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000319 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000320 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000321 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000322 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000323 return mac_error();
324 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000325 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000326 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000327 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000328 return NULL;
329 }
330 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000331 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000332 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000333 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000334 d = NULL;
335 break;
336 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000337 if (PyList_Append(d, v) != 0) {
338 Py_DECREF(v);
339 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000340 d = NULL;
341 break;
342 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000343 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000344 }
345 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000346 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000347
Guido van Rossumce9739b1994-01-05 16:17:15 +0000348 return d;
349}
350
Jack Jansenf5c20571997-01-30 15:48:07 +0000351static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000352mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000353 PyObject *self;
354 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000355{
356 int fd;
357 int where;
358 int how;
359 long res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000360 if (!PyArg_Parse(args, "(iii)", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000361 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000362 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000363 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000364 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000365 if (res < 0)
366 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000367 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000368}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000369
Jack Jansenf5c20571997-01-30 15:48:07 +0000370static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000371mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000372 PyObject *self;
373 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000374{
Jack Jansen243b29b1996-02-21 12:33:50 +0000375 int res;
376 char *path;
377 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000378 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000379 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000380 Py_BEGIN_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000381#ifdef USE_GUSI1
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000382 res = mkdir(path);
383#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000384 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000385#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000386 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000387 if (res < 0)
388 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000389 Py_INCREF(Py_None);
390 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000391}
392
Jack Jansenf5c20571997-01-30 15:48:07 +0000393static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000394mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000395 PyObject *self;
396 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000397{
398 char *path;
399 int mode;
400 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000401 if (!PyArg_Parse(args, "(si)", &path, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000402 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000403 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000404 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000405 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000406 if (fd < 0)
407 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000408 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000409}
410
Jack Jansenf5c20571997-01-30 15:48:07 +0000411static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000412mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000413 PyObject *self;
414 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000415{
416 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 PyObject *buffer;
418 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000419 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000420 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000421 if (buffer == NULL)
422 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000423 Py_BEGIN_ALLOW_THREADS
424 size = read(fd, PyString_AsString(buffer), size);
425 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000426 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000427 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000428 return mac_error();
429 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000430 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000431 return buffer;
432}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000433
Jack Jansenf5c20571997-01-30 15:48:07 +0000434static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000435mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000436 PyObject *self;
437 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000438{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000439 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000440}
441
Jack Jansenf5c20571997-01-30 15:48:07 +0000442static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000443mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000444 PyObject *self;
445 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000446{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000447 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000448}
449
Jack Jansenf5c20571997-01-30 15:48:07 +0000450static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000451mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000452 PyObject *self;
453 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000454{
Jack Jansen59b912a1996-10-15 16:13:33 +0000455 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000456 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000457 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000458 if (!PyArg_Parse(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000459 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000460 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000461 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000462 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000463 if (res != 0)
464 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000465 return Py_BuildValue("(lllllllddd)",
Jack Jansen59b912a1996-10-15 16:13:33 +0000466 (long)st.st_mode,
467 (long)st.st_ino,
468 (long)st.st_dev,
469 (long)st.st_nlink,
470 (long)st.st_uid,
471 (long)st.st_gid,
472 (long)st.st_size,
473 (double)st.st_atime,
474 (double)st.st_mtime,
475 (double)st.st_ctime);
Jack Jansen40bd7701998-02-20 15:56:19 +0000476}
477
478#ifdef WEHAVE_FSTAT
479static PyObject *
480mac_fstat(self, args)
481 PyObject *self;
482 PyObject *args;
483{
484 struct stat st;
485 long fd;
486 int res;
487 if (!PyArg_Parse(args, "l", &fd))
488 return NULL;
489 Py_BEGIN_ALLOW_THREADS
490 res = fstat((int)fd, &st);
491 Py_END_ALLOW_THREADS
492 if (res != 0)
493 return mac_error();
494 return Py_BuildValue("(lllllllddd)",
Guido van Rossum87f223c1994-05-06 15:54:15 +0000495 (long)st.st_mode,
Guido van Rossume7834441994-08-26 09:09:48 +0000496 (long)st.st_ino,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000497 (long)st.st_dev,
498 (long)st.st_nlink,
499 (long)st.st_uid,
500 (long)st.st_gid,
Guido van Rossum87f223c1994-05-06 15:54:15 +0000501 (long)st.st_size,
Jack Jansen40bd7701998-02-20 15:56:19 +0000502 (double)st.st_atime,
503 (double)st.st_mtime,
504 (double)st.st_ctime);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000505}
Jack Jansen40bd7701998-02-20 15:56:19 +0000506#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000507
Jack Jansenf5c20571997-01-30 15:48:07 +0000508static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000509mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000510 PyObject *self;
511 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000512{
Jack Jansen59b912a1996-10-15 16:13:33 +0000513 struct macstat mst;
514 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000515 char *path;
516 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000517 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000518 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000519 /*
520 ** Convoluted: we want stat() and xstat() to agree, so we call both
521 ** stat and macstat, and use the latter only for values not provided by
522 ** the former.
523 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000524 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000525 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000526 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000527 if (res != 0)
528 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000529 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000530 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000531 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000532 if (res != 0)
533 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000534 return Py_BuildValue("(llllllldddls#s#)",
Jack Jansenc743c8d1996-02-14 16:02:30 +0000535 (long)st.st_mode,
536 (long)st.st_ino,
537 (long)st.st_dev,
538 (long)st.st_nlink,
539 (long)st.st_uid,
540 (long)st.st_gid,
541 (long)st.st_size,
Jack Jansen59b912a1996-10-15 16:13:33 +0000542 (double)st.st_atime,
543 (double)st.st_mtime,
544 (double)st.st_ctime,
545 (long)mst.st_rsize,
546 mst.st_creator, 4,
547 mst.st_type, 4);
Guido van Rossum222c8921995-08-08 14:10:22 +0000548}
549
Jack Jansenf5c20571997-01-30 15:48:07 +0000550static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000551mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000552 PyObject *self;
553 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000554{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000555 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000556 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000557 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000558 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000559 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000560 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000561 if (res != 0)
562 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000563 Py_INCREF(Py_None);
564 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000565}
566
Jack Jansenf5c20571997-01-30 15:48:07 +0000567static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000568mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000569 PyObject *self;
570 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000571{
Guido van Rossum739267b1994-08-29 08:42:37 +0000572 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000573}
574
Jack Jansenf5c20571997-01-30 15:48:07 +0000575static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000576mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000577 PyObject *self;
578 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000579{
580 int fd, size;
581 char *buffer;
Jack Janseneeccca91997-05-07 15:46:31 +0000582 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000583 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000584 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000585 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000586 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000587 if (size < 0)
588 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000589 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000590}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000591
Jack Jansen46ed2761996-10-23 15:46:57 +0000592#ifdef USE_MALLOC_DEBUG
Jack Jansenf5c20571997-01-30 15:48:07 +0000593static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000594mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000595 PyObject*self;
596 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000597{
598 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000599 Py_INCREF(Py_None);
600 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000601}
Jack Jansen4f27a551997-02-20 15:22:17 +0000602#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000603
Jack Jansenf5c20571997-01-30 15:48:07 +0000604static struct PyMethodDef mac_methods[] = {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000605 {"chdir", mac_chdir},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000606 {"close", mac_close},
Jack Jansenb6971731996-02-20 16:24:37 +0000607#ifdef WEHAVE_DUP
Guido van Rossum87f223c1994-05-06 15:54:15 +0000608 {"dup", mac_dup},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000609#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000610#ifdef WEHAVE_FDOPEN
Guido van Rossume7834441994-08-26 09:09:48 +0000611 {"fdopen", mac_fdopen},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000612#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000613#ifdef WEHAVE_FSTAT
614 {"fstat", mac_fstat},
615#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000616 {"getbootvol", mac_getbootvol}, /* non-standard */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000617 {"getcwd", mac_getcwd},
Guido van Rossumd1ef5961995-02-19 15:50:35 +0000618 {"listdir", mac_listdir, 0},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000619 {"lseek", mac_lseek},
Jack Jansen243b29b1996-02-21 12:33:50 +0000620 {"mkdir", mac_mkdir, 1},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000621 {"open", mac_open},
622 {"read", mac_read},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000623 {"rename", mac_rename},
624 {"rmdir", mac_rmdir},
625 {"stat", mac_stat},
Guido van Rossum222c8921995-08-08 14:10:22 +0000626 {"xstat", mac_xstat},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000627 {"sync", mac_sync},
Jack Jansen243b29b1996-02-21 12:33:50 +0000628 {"remove", mac_unlink},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000629 {"unlink", mac_unlink},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000630 {"write", mac_write},
Jack Jansen46ed2761996-10-23 15:46:57 +0000631#ifdef USE_MALLOC_DEBUG
Jack Jansend50e4e11995-01-18 13:58:04 +0000632 {"mstats", mac_mstats},
633#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000634
Guido van Rossumce9739b1994-01-05 16:17:15 +0000635 {NULL, NULL} /* Sentinel */
636};
637
638
639void
640initmac()
641{
Jack Jansenf5c20571997-01-30 15:48:07 +0000642 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000643
Jack Jansenf5c20571997-01-30 15:48:07 +0000644 m = Py_InitModule("mac", mac_methods);
645 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000646
647 /* Initialize mac.error exception */
Jack Jansen55e39271997-10-07 21:47:25 +0000648 MacError = PyErr_NewException("mac.error", NULL, NULL);
649 PyDict_SetItemString(d, "error", MacError);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000650}