blob: 49c0f8b34ef5e12530fa318ed747b3ce67334cb9 [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
43#endif
44
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 Jansenb6971731996-02-20 16:24:37 +000055#include <GUSI.h>
Jack Jansenc743c8d1996-02-14 16:02:30 +000056#include <sys/types.h>
57#include <stat.h>
Jack Jansenc743c8d1996-02-14 16:02:30 +000058#else
Jack Jansen59b912a1996-10-15 16:13:33 +000059#define stat macstat
Jack Jansenc743c8d1996-02-14 16:02:30 +000060#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000061
Jack Jansen9eeb82d1995-01-26 16:34:53 +000062#ifdef __MWERKS__
Jack Jansen9eeb82d1995-01-26 16:34:53 +000063#include <unix.h>
64#else
Guido van Rossum87f223c1994-05-06 15:54:15 +000065#include <fcntl.h>
Jack Jansen0c097ea1994-12-14 13:48:38 +000066#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000067
Jack Jansenb6971731996-02-20 16:24:37 +000068/* Optional routines, for some compiler/runtime combinations */
Jack Jansenb6971731996-02-20 16:24:37 +000069#if defined(USE_GUSI) || !defined(__MWERKS__)
70#define WEHAVE_FDOPEN
71#endif
72#if defined(MPW) || defined(USE_GUSI)
73#define WEHAVE_DUP
74#endif
Jack Jansen7cbf4801995-01-22 16:52:38 +000075
Guido van Rossumd4d77281994-08-19 10:51:31 +000076#include "macdefs.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000077#ifdef USE_GUSI
78#include <dirent.h>
79#else
Guido van Rossumd4d77281994-08-19 10:51:31 +000080#include "dirent.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000081#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +000082
83#ifndef MAXPATHLEN
84#define MAXPATHLEN 1024
85#endif
86
87/* Prototypes for Unix simulation on Mac */
88
Jack Jansenb6971731996-02-20 16:24:37 +000089#ifndef USE_GUSI
90
Jack Jansenf5c20571997-01-30 15:48:07 +000091int chdir Py_PROTO((const char *path));
92int mkdir Py_PROTO((const char *path, int mode));
93DIR * opendir Py_PROTO((char *));
94void closedir Py_PROTO((DIR *));
95struct dirent * readdir Py_PROTO((DIR *));
96int rmdir Py_PROTO((const char *path));
97int sync Py_PROTO((void));
Jack Jansenb6971731996-02-20 16:24:37 +000098
99#if defined(THINK_C) || defined(__SC__)
Jack Jansenf5c20571997-01-30 15:48:07 +0000100int unlink Py_PROTO((char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000101#else
Jack Jansenf5c20571997-01-30 15:48:07 +0000102int unlink Py_PROTO((const char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000103#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000104
Jack Jansenb6971731996-02-20 16:24:37 +0000105#endif /* USE_GUSI */
106
Jack Jansenf5c20571997-01-30 15:48:07 +0000107char *getwd Py_PROTO((char *));
108char *getbootvol Py_PROTO((void));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000109
110
Jack Jansenf5c20571997-01-30 15:48:07 +0000111static PyObject *MacError; /* Exception mac.error */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000112
113/* Set a MAC-specific error from errno, and return NULL */
114
Jack Jansenf5c20571997-01-30 15:48:07 +0000115static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000116mac_error()
117{
Jack Jansenf5c20571997-01-30 15:48:07 +0000118 return PyErr_SetFromErrno(MacError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000119}
120
121/* MAC generic methods */
122
Jack Jansenf5c20571997-01-30 15:48:07 +0000123static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000124mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000125 PyObject *args;
126 int (*func) Py_FPROTO((const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000127{
128 char *path1;
129 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000130 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000131 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000132 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000133 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000134 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000135 if (res < 0)
136 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000137 Py_INCREF(Py_None);
138 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000139}
140
Jack Jansenf5c20571997-01-30 15:48:07 +0000141static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000142mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000143 PyObject *args;
144 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000145{
146 char *path1, *path2;
147 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000148 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000149 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000150 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000151 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000152 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000153 if (res < 0)
154 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000155 Py_INCREF(Py_None);
156 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000157}
158
Jack Jansenf5c20571997-01-30 15:48:07 +0000159static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000160mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000161 PyObject *args;
162 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000163{
164 char *path;
165 int i;
166 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000167 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000168 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000169 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000170 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000171 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000172 if (res < 0)
173 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000174 Py_INCREF(Py_None);
175 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000176}
177
Jack Jansenf5c20571997-01-30 15:48:07 +0000178static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000179mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000180 PyObject *self;
181 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000182{
Jack Jansen378815c1996-03-06 16:21:34 +0000183#ifdef USE_GUSI
Jack Jansenf5c20571997-01-30 15:48:07 +0000184 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000185
186 /* Change MacOS's idea of wd too */
187 rv = mac_1str(args, chdir);
188 PyMac_FixGUSIcd();
189 return rv;
190#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000191 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000192#endif
193
Guido van Rossum87f223c1994-05-06 15:54:15 +0000194}
195
Jack Jansenf5c20571997-01-30 15:48:07 +0000196static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000197mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000198 PyObject *self;
199 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000200{
201 int fd, res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000202 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000203 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000204 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000205 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000206 Py_END_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000207#ifndef USE_GUSI
208 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000209 if (res < 0)
210 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000211#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000212 Py_INCREF(Py_None);
213 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000214}
215
Jack Jansenb6971731996-02-20 16:24:37 +0000216#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000217
Jack Jansenf5c20571997-01-30 15:48:07 +0000218static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000219mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000220 PyObject *self;
221 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000222{
223 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000224 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000225 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000226 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000227 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000228 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000229 if (fd < 0)
230 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000231 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000232}
233
Jack Jansenb6971731996-02-20 16:24:37 +0000234#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000235
Jack Jansenb6971731996-02-20 16:24:37 +0000236#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000237static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000238mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000239 PyObject *self;
240 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000241{
Jack Jansenf5c20571997-01-30 15:48:07 +0000242 extern int fclose Py_PROTO((FILE *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000243 int fd;
244 char *mode;
245 FILE *fp;
Jack Jansenf5c20571997-01-30 15:48:07 +0000246 if (!PyArg_Parse(args, "(is)", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000247 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000248 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000249 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000250 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000251 if (fp == NULL)
252 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000253 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000254}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000255#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000256
Jack Jansenf5c20571997-01-30 15:48:07 +0000257static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000258mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000259 PyObject *self;
260 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000261{
262 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000263 if (!PyArg_NoArgs(args))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000264 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000265 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000266 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000267 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000268 if (res == NULL)
269 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000270 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000271}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000272
Jack Jansenf5c20571997-01-30 15:48:07 +0000273static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000274mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000275 PyObject *self;
276 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000277{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000278 char path[MAXPATHLEN];
279 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000280 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000281 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000282 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000283#ifdef USE_GUSI
284 res = getcwd(path, sizeof path);
285#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000286 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000287#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000288 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000289 if (res == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000290 PyErr_SetString(MacError, path);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000291 return NULL;
292 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000293 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000294}
295
Jack Jansenf5c20571997-01-30 15:48:07 +0000296static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000297mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000298 PyObject *self;
299 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000300{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000301 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000302 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000303 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000304 struct dirent *ep;
Jack Jansenf5c20571997-01-30 15:48:07 +0000305 if (!PyArg_Parse(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000306 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000307 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000308 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000309 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000310 return mac_error();
311 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000312 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000313 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000314 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000315 return NULL;
316 }
317 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000318 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000319 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000320 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000321 d = NULL;
322 break;
323 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000324 if (PyList_Append(d, v) != 0) {
325 Py_DECREF(v);
326 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000327 d = NULL;
328 break;
329 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000330 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000331 }
332 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000333 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000334
Guido van Rossumce9739b1994-01-05 16:17:15 +0000335 return d;
336}
337
Jack Jansenf5c20571997-01-30 15:48:07 +0000338static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000339mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 PyObject *self;
341 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000342{
343 int fd;
344 int where;
345 int how;
346 long res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000347 if (!PyArg_Parse(args, "(iii)", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000348 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000349 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000350 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000351 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000352 if (res < 0)
353 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000354 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000355}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000356
Jack Jansenf5c20571997-01-30 15:48:07 +0000357static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000358mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000359 PyObject *self;
360 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000361{
Jack Jansen243b29b1996-02-21 12:33:50 +0000362 int res;
363 char *path;
364 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000365 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000366 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000367 Py_BEGIN_ALLOW_THREADS
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000368#ifdef USE_GUSI
369 res = mkdir(path);
370#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000371 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000372#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000373 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000374 if (res < 0)
375 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000376 Py_INCREF(Py_None);
377 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000378}
379
Jack Jansenf5c20571997-01-30 15:48:07 +0000380static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000381mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000382 PyObject *self;
383 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000384{
385 char *path;
386 int mode;
387 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000388 if (!PyArg_Parse(args, "(si)", &path, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000389 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000390 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000391 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000392 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000393 if (fd < 0)
394 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000395 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000396}
397
Jack Jansenf5c20571997-01-30 15:48:07 +0000398static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000399mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000400 PyObject *self;
401 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000402{
403 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000404 PyObject *buffer;
405 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000406 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000407 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000408 if (buffer == NULL)
409 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000410 Py_BEGIN_ALLOW_THREADS
411 size = read(fd, PyString_AsString(buffer), size);
412 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000413 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000414 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000415 return mac_error();
416 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000418 return buffer;
419}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000420
Jack Jansenf5c20571997-01-30 15:48:07 +0000421static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000422mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000423 PyObject *self;
424 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000425{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000426 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000427}
428
Jack Jansenf5c20571997-01-30 15:48:07 +0000429static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000430mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000431 PyObject *self;
432 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000433{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000434 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000435}
436
Jack Jansenf5c20571997-01-30 15:48:07 +0000437static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000438mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000439 PyObject *self;
440 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000441{
Jack Jansen59b912a1996-10-15 16:13:33 +0000442 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000443 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000444 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000445 if (!PyArg_Parse(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000446 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000447 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000448 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000449 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000450 if (res != 0)
451 return mac_error();
Jack Jansen59b912a1996-10-15 16:13:33 +0000452#if 1
Jack Jansenf5c20571997-01-30 15:48:07 +0000453 return Py_BuildValue("(lllllllddd)",
Jack Jansen59b912a1996-10-15 16:13:33 +0000454 (long)st.st_mode,
455 (long)st.st_ino,
456 (long)st.st_dev,
457 (long)st.st_nlink,
458 (long)st.st_uid,
459 (long)st.st_gid,
460 (long)st.st_size,
461 (double)st.st_atime,
462 (double)st.st_mtime,
463 (double)st.st_ctime);
464#else
Jack Jansenf5c20571997-01-30 15:48:07 +0000465 return Py_BuildValue("(llllllllll)",
Guido van Rossum87f223c1994-05-06 15:54:15 +0000466 (long)st.st_mode,
Guido van Rossume7834441994-08-26 09:09:48 +0000467 (long)st.st_ino,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000468 (long)st.st_dev,
469 (long)st.st_nlink,
470 (long)st.st_uid,
471 (long)st.st_gid,
Guido van Rossum87f223c1994-05-06 15:54:15 +0000472 (long)st.st_size,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000473 (long)st.st_atime,
474 (long)st.st_mtime,
475 (long)st.st_ctime);
Jack Jansen59b912a1996-10-15 16:13:33 +0000476#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000477}
478
Jack Jansenf5c20571997-01-30 15:48:07 +0000479static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000480mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000481 PyObject *self;
482 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000483{
Jack Jansen59b912a1996-10-15 16:13:33 +0000484 struct macstat mst;
485 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000486 char *path;
487 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000488 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000489 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000490 /*
491 ** Convoluted: we want stat() and xstat() to agree, so we call both
492 ** stat and macstat, and use the latter only for values not provided by
493 ** the former.
494 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000495 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000496 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000497 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000498 if (res != 0)
499 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000500 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000501 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000502 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000503 if (res != 0)
504 return mac_error();
505#if 1
Jack Jansenf5c20571997-01-30 15:48:07 +0000506 return Py_BuildValue("(llllllldddls#s#)",
Jack Jansenc743c8d1996-02-14 16:02:30 +0000507 (long)st.st_mode,
508 (long)st.st_ino,
509 (long)st.st_dev,
510 (long)st.st_nlink,
511 (long)st.st_uid,
512 (long)st.st_gid,
513 (long)st.st_size,
Jack Jansen59b912a1996-10-15 16:13:33 +0000514 (double)st.st_atime,
515 (double)st.st_mtime,
516 (double)st.st_ctime,
517 (long)mst.st_rsize,
518 mst.st_creator, 4,
519 mst.st_type, 4);
Jack Jansenc743c8d1996-02-14 16:02:30 +0000520#else
Jack Jansenf5c20571997-01-30 15:48:07 +0000521 return Py_BuildValue("(llllllllllls#s#)",
Guido van Rossum222c8921995-08-08 14:10:22 +0000522 (long)st.st_mode,
523 (long)st.st_ino,
524 (long)st.st_dev,
525 (long)st.st_nlink,
526 (long)st.st_uid,
527 (long)st.st_gid,
528 (long)st.st_size,
529 (long)st.st_atime,
530 (long)st.st_mtime,
531 (long)st.st_ctime,
Jack Jansen59b912a1996-10-15 16:13:33 +0000532 (long)mst.st_rsize,
533 mst.st_creator, 4,
534 mst.st_type, 4);
Jack Jansenc743c8d1996-02-14 16:02:30 +0000535#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000536}
537
Jack Jansenf5c20571997-01-30 15:48:07 +0000538static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000539mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000540 PyObject *self;
541 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000542{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000543 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000544 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000545 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000546 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000547 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000548 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000549 if (res != 0)
550 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000551 Py_INCREF(Py_None);
552 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000553}
554
Jack Jansenf5c20571997-01-30 15:48:07 +0000555static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000556mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000557 PyObject *self;
558 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000559{
Guido van Rossum739267b1994-08-29 08:42:37 +0000560 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000561}
562
Jack Jansenf5c20571997-01-30 15:48:07 +0000563static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000564mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000565 PyObject *self;
566 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000567{
568 int fd, size;
569 char *buffer;
Jack Janseneeccca91997-05-07 15:46:31 +0000570 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000571 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000572 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000573 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000574 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000575 if (size < 0)
576 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000577 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000578}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000579
Jack Jansen46ed2761996-10-23 15:46:57 +0000580#ifdef USE_MALLOC_DEBUG
Jack Jansenf5c20571997-01-30 15:48:07 +0000581static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000582mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000583 PyObject*self;
584 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000585{
586 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000587 Py_INCREF(Py_None);
588 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000589}
Jack Jansen4f27a551997-02-20 15:22:17 +0000590#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000591
Jack Jansenf5c20571997-01-30 15:48:07 +0000592static struct PyMethodDef mac_methods[] = {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000593 {"chdir", mac_chdir},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000594 {"close", mac_close},
Jack Jansenb6971731996-02-20 16:24:37 +0000595#ifdef WEHAVE_DUP
Guido van Rossum87f223c1994-05-06 15:54:15 +0000596 {"dup", mac_dup},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000597#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000598#ifdef WEHAVE_FDOPEN
Guido van Rossume7834441994-08-26 09:09:48 +0000599 {"fdopen", mac_fdopen},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000600#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000601 {"getbootvol", mac_getbootvol}, /* non-standard */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000602 {"getcwd", mac_getcwd},
Guido van Rossumd1ef5961995-02-19 15:50:35 +0000603 {"listdir", mac_listdir, 0},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000604 {"lseek", mac_lseek},
Jack Jansen243b29b1996-02-21 12:33:50 +0000605 {"mkdir", mac_mkdir, 1},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000606 {"open", mac_open},
607 {"read", mac_read},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000608 {"rename", mac_rename},
609 {"rmdir", mac_rmdir},
610 {"stat", mac_stat},
Guido van Rossum222c8921995-08-08 14:10:22 +0000611 {"xstat", mac_xstat},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000612 {"sync", mac_sync},
Jack Jansen243b29b1996-02-21 12:33:50 +0000613 {"remove", mac_unlink},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000614 {"unlink", mac_unlink},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000615 {"write", mac_write},
Jack Jansen46ed2761996-10-23 15:46:57 +0000616#ifdef USE_MALLOC_DEBUG
Jack Jansend50e4e11995-01-18 13:58:04 +0000617 {"mstats", mac_mstats},
618#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000619
Guido van Rossumce9739b1994-01-05 16:17:15 +0000620 {NULL, NULL} /* Sentinel */
621};
622
623
624void
625initmac()
626{
Jack Jansenf5c20571997-01-30 15:48:07 +0000627 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000628
Jack Jansenf5c20571997-01-30 15:48:07 +0000629 m = Py_InitModule("mac", mac_methods);
630 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000631
632 /* Initialize mac.error exception */
Jack Jansenf5c20571997-01-30 15:48:07 +0000633 MacError = PyString_FromString("mac.error");
634 if (MacError == NULL || PyDict_SetItemString(d, "error", MacError) != 0)
635 Py_FatalError("can't define mac.error");
Guido van Rossumce9739b1994-01-05 16:17:15 +0000636}