blob: 910f69051ce0796096be796f1d541887971f2227 [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 Jansen40bd7701998-02-20 15:56:19 +000075#if defined(USE_GUSI)
76#define WEHAVE_FSTAT
77#endif
Jack Jansen7cbf4801995-01-22 16:52:38 +000078
Guido van Rossumd4d77281994-08-19 10:51:31 +000079#include "macdefs.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000080#ifdef USE_GUSI
81#include <dirent.h>
82#else
Guido van Rossumd4d77281994-08-19 10:51:31 +000083#include "dirent.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000084#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +000085
86#ifndef MAXPATHLEN
87#define MAXPATHLEN 1024
88#endif
89
90/* Prototypes for Unix simulation on Mac */
91
Jack Jansenb6971731996-02-20 16:24:37 +000092#ifndef USE_GUSI
93
Jack Jansenf5c20571997-01-30 15:48:07 +000094int chdir Py_PROTO((const char *path));
95int mkdir Py_PROTO((const char *path, int mode));
96DIR * opendir Py_PROTO((char *));
97void closedir Py_PROTO((DIR *));
98struct dirent * readdir Py_PROTO((DIR *));
99int rmdir Py_PROTO((const char *path));
100int sync Py_PROTO((void));
Jack Jansenb6971731996-02-20 16:24:37 +0000101
102#if defined(THINK_C) || defined(__SC__)
Jack Jansenf5c20571997-01-30 15:48:07 +0000103int unlink Py_PROTO((char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000104#else
Jack Jansenf5c20571997-01-30 15:48:07 +0000105int unlink Py_PROTO((const char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000106#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000107
Jack Jansenb6971731996-02-20 16:24:37 +0000108#endif /* USE_GUSI */
109
Jack Jansenf5c20571997-01-30 15:48:07 +0000110char *getwd Py_PROTO((char *));
111char *getbootvol Py_PROTO((void));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000112
113
Jack Jansenf5c20571997-01-30 15:48:07 +0000114static PyObject *MacError; /* Exception mac.error */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000115
116/* Set a MAC-specific error from errno, and return NULL */
117
Jack Jansenf5c20571997-01-30 15:48:07 +0000118static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000119mac_error()
120{
Jack Jansenf5c20571997-01-30 15:48:07 +0000121 return PyErr_SetFromErrno(MacError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000122}
123
124/* MAC generic methods */
125
Jack Jansenf5c20571997-01-30 15:48:07 +0000126static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000127mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000128 PyObject *args;
129 int (*func) Py_FPROTO((const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000130{
131 char *path1;
132 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000133 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000134 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000135 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000136 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000137 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000138 if (res < 0)
139 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000140 Py_INCREF(Py_None);
141 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000142}
143
Jack Jansenf5c20571997-01-30 15:48:07 +0000144static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000145mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000146 PyObject *args;
147 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000148{
149 char *path1, *path2;
150 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000151 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000152 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000153 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000154 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000155 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000156 if (res < 0)
157 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000158 Py_INCREF(Py_None);
159 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000160}
161
Jack Jansenf5c20571997-01-30 15:48:07 +0000162static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000163mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000164 PyObject *args;
165 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000166{
167 char *path;
168 int i;
169 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000170 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000171 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000172 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000173 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000174 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000175 if (res < 0)
176 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000177 Py_INCREF(Py_None);
178 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000179}
180
Jack Jansenf5c20571997-01-30 15:48:07 +0000181static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000182mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000183 PyObject *self;
184 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000185{
Jack Jansen378815c1996-03-06 16:21:34 +0000186#ifdef USE_GUSI
Jack Jansenf5c20571997-01-30 15:48:07 +0000187 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000188
189 /* Change MacOS's idea of wd too */
190 rv = mac_1str(args, chdir);
191 PyMac_FixGUSIcd();
192 return rv;
193#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000194 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000195#endif
196
Guido van Rossum87f223c1994-05-06 15:54:15 +0000197}
198
Jack Jansenf5c20571997-01-30 15:48:07 +0000199static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000200mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000201 PyObject *self;
202 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000203{
204 int fd, res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000205 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000206 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000207 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000208 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000209 Py_END_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000210#ifndef USE_GUSI
211 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000212 if (res < 0)
213 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000214#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000215 Py_INCREF(Py_None);
216 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000217}
218
Jack Jansenb6971731996-02-20 16:24:37 +0000219#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000220
Jack Jansenf5c20571997-01-30 15:48:07 +0000221static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000222mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000223 PyObject *self;
224 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000225{
226 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000227 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000228 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000229 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000230 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000231 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000232 if (fd < 0)
233 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000234 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000235}
236
Jack Jansenb6971731996-02-20 16:24:37 +0000237#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000238
Jack Jansenb6971731996-02-20 16:24:37 +0000239#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000240static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000241mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000242 PyObject *self;
243 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000244{
Jack Jansenf5c20571997-01-30 15:48:07 +0000245 extern int fclose Py_PROTO((FILE *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000246 int fd;
247 char *mode;
248 FILE *fp;
Jack Jansenf5c20571997-01-30 15:48:07 +0000249 if (!PyArg_Parse(args, "(is)", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000250 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000251 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000252 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000253 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000254 if (fp == NULL)
255 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000256 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000257}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000258#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000259
Jack Jansenf5c20571997-01-30 15:48:07 +0000260static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000261mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000262 PyObject *self;
263 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000264{
265 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000266 if (!PyArg_NoArgs(args))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000267 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000268 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000269 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000270 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000271 if (res == NULL)
272 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000273 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000274}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000275
Jack Jansenf5c20571997-01-30 15:48:07 +0000276static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000277mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000278 PyObject *self;
279 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000280{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000281 char path[MAXPATHLEN];
282 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000283 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000284 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000285 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000286#ifdef USE_GUSI
287 res = getcwd(path, sizeof path);
288#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000289 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000290#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000291 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000292 if (res == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000293 PyErr_SetString(MacError, path);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000294 return NULL;
295 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000296 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000297}
298
Jack Jansenf5c20571997-01-30 15:48:07 +0000299static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000300mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000301 PyObject *self;
302 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000303{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000304 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000305 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000306 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000307 struct dirent *ep;
Jack Jansenf5c20571997-01-30 15:48:07 +0000308 if (!PyArg_Parse(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000309 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000310 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000311 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000312 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000313 return mac_error();
314 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000315 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000316 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000317 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000318 return NULL;
319 }
320 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000321 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000322 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000323 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000324 d = NULL;
325 break;
326 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000327 if (PyList_Append(d, v) != 0) {
328 Py_DECREF(v);
329 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000330 d = NULL;
331 break;
332 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000333 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000334 }
335 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000336 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000337
Guido van Rossumce9739b1994-01-05 16:17:15 +0000338 return d;
339}
340
Jack Jansenf5c20571997-01-30 15:48:07 +0000341static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000342mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000343 PyObject *self;
344 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000345{
346 int fd;
347 int where;
348 int how;
349 long res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000350 if (!PyArg_Parse(args, "(iii)", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000351 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000352 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000353 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000354 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000355 if (res < 0)
356 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000357 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000358}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000359
Jack Jansenf5c20571997-01-30 15:48:07 +0000360static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000361mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000362 PyObject *self;
363 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000364{
Jack Jansen243b29b1996-02-21 12:33:50 +0000365 int res;
366 char *path;
367 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000368 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000369 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000370 Py_BEGIN_ALLOW_THREADS
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000371#ifdef USE_GUSI
372 res = mkdir(path);
373#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000374 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000375#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000376 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000377 if (res < 0)
378 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000379 Py_INCREF(Py_None);
380 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000381}
382
Jack Jansenf5c20571997-01-30 15:48:07 +0000383static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000384mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000385 PyObject *self;
386 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000387{
388 char *path;
389 int mode;
390 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000391 if (!PyArg_Parse(args, "(si)", &path, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000392 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000393 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000394 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000395 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000396 if (fd < 0)
397 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000398 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000399}
400
Jack Jansenf5c20571997-01-30 15:48:07 +0000401static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000402mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000403 PyObject *self;
404 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000405{
406 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000407 PyObject *buffer;
408 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000409 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000410 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000411 if (buffer == NULL)
412 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000413 Py_BEGIN_ALLOW_THREADS
414 size = read(fd, PyString_AsString(buffer), size);
415 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000416 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000418 return mac_error();
419 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000420 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000421 return buffer;
422}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000423
Jack Jansenf5c20571997-01-30 15:48:07 +0000424static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000425mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000426 PyObject *self;
427 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000428{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000429 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000430}
431
Jack Jansenf5c20571997-01-30 15:48:07 +0000432static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000433mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000434 PyObject *self;
435 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000436{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000437 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000438}
439
Jack Jansenf5c20571997-01-30 15:48:07 +0000440static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000441mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000442 PyObject *self;
443 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000444{
Jack Jansen59b912a1996-10-15 16:13:33 +0000445 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000446 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000447 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000448 if (!PyArg_Parse(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000449 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000450 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000451 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000452 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000453 if (res != 0)
454 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000455 return Py_BuildValue("(lllllllddd)",
Jack Jansen59b912a1996-10-15 16:13:33 +0000456 (long)st.st_mode,
457 (long)st.st_ino,
458 (long)st.st_dev,
459 (long)st.st_nlink,
460 (long)st.st_uid,
461 (long)st.st_gid,
462 (long)st.st_size,
463 (double)st.st_atime,
464 (double)st.st_mtime,
465 (double)st.st_ctime);
Jack Jansen40bd7701998-02-20 15:56:19 +0000466}
467
468#ifdef WEHAVE_FSTAT
469static PyObject *
470mac_fstat(self, args)
471 PyObject *self;
472 PyObject *args;
473{
474 struct stat st;
475 long fd;
476 int res;
477 if (!PyArg_Parse(args, "l", &fd))
478 return NULL;
479 Py_BEGIN_ALLOW_THREADS
480 res = fstat((int)fd, &st);
481 Py_END_ALLOW_THREADS
482 if (res != 0)
483 return mac_error();
484 return Py_BuildValue("(lllllllddd)",
Guido van Rossum87f223c1994-05-06 15:54:15 +0000485 (long)st.st_mode,
Guido van Rossume7834441994-08-26 09:09:48 +0000486 (long)st.st_ino,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000487 (long)st.st_dev,
488 (long)st.st_nlink,
489 (long)st.st_uid,
490 (long)st.st_gid,
Guido van Rossum87f223c1994-05-06 15:54:15 +0000491 (long)st.st_size,
Jack Jansen40bd7701998-02-20 15:56:19 +0000492 (double)st.st_atime,
493 (double)st.st_mtime,
494 (double)st.st_ctime);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000495}
Jack Jansen40bd7701998-02-20 15:56:19 +0000496#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000497
Jack Jansenf5c20571997-01-30 15:48:07 +0000498static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000499mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000500 PyObject *self;
501 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000502{
Jack Jansen59b912a1996-10-15 16:13:33 +0000503 struct macstat mst;
504 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000505 char *path;
506 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000507 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000508 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000509 /*
510 ** Convoluted: we want stat() and xstat() to agree, so we call both
511 ** stat and macstat, and use the latter only for values not provided by
512 ** the former.
513 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000514 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000515 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000516 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000517 if (res != 0)
518 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000519 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000520 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000521 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000522 if (res != 0)
523 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000524 return Py_BuildValue("(llllllldddls#s#)",
Jack Jansenc743c8d1996-02-14 16:02:30 +0000525 (long)st.st_mode,
526 (long)st.st_ino,
527 (long)st.st_dev,
528 (long)st.st_nlink,
529 (long)st.st_uid,
530 (long)st.st_gid,
531 (long)st.st_size,
Jack Jansen59b912a1996-10-15 16:13:33 +0000532 (double)st.st_atime,
533 (double)st.st_mtime,
534 (double)st.st_ctime,
535 (long)mst.st_rsize,
536 mst.st_creator, 4,
537 mst.st_type, 4);
Guido van Rossum222c8921995-08-08 14:10:22 +0000538}
539
Jack Jansenf5c20571997-01-30 15:48:07 +0000540static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000541mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000542 PyObject *self;
543 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000544{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000545 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000546 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000547 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000548 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000549 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000550 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000551 if (res != 0)
552 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000553 Py_INCREF(Py_None);
554 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000555}
556
Jack Jansenf5c20571997-01-30 15:48:07 +0000557static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000558mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000559 PyObject *self;
560 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000561{
Guido van Rossum739267b1994-08-29 08:42:37 +0000562 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000563}
564
Jack Jansenf5c20571997-01-30 15:48:07 +0000565static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000566mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000567 PyObject *self;
568 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000569{
570 int fd, size;
571 char *buffer;
Jack Janseneeccca91997-05-07 15:46:31 +0000572 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000573 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000574 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000575 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000576 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000577 if (size < 0)
578 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000579 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000580}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000581
Jack Jansen46ed2761996-10-23 15:46:57 +0000582#ifdef USE_MALLOC_DEBUG
Jack Jansenf5c20571997-01-30 15:48:07 +0000583static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000584mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000585 PyObject*self;
586 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000587{
588 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000589 Py_INCREF(Py_None);
590 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000591}
Jack Jansen4f27a551997-02-20 15:22:17 +0000592#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000593
Jack Jansenf5c20571997-01-30 15:48:07 +0000594static struct PyMethodDef mac_methods[] = {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000595 {"chdir", mac_chdir},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000596 {"close", mac_close},
Jack Jansenb6971731996-02-20 16:24:37 +0000597#ifdef WEHAVE_DUP
Guido van Rossum87f223c1994-05-06 15:54:15 +0000598 {"dup", mac_dup},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000599#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000600#ifdef WEHAVE_FDOPEN
Guido van Rossume7834441994-08-26 09:09:48 +0000601 {"fdopen", mac_fdopen},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000602#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000603#ifdef WEHAVE_FSTAT
604 {"fstat", mac_fstat},
605#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000606 {"getbootvol", mac_getbootvol}, /* non-standard */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000607 {"getcwd", mac_getcwd},
Guido van Rossumd1ef5961995-02-19 15:50:35 +0000608 {"listdir", mac_listdir, 0},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000609 {"lseek", mac_lseek},
Jack Jansen243b29b1996-02-21 12:33:50 +0000610 {"mkdir", mac_mkdir, 1},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000611 {"open", mac_open},
612 {"read", mac_read},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000613 {"rename", mac_rename},
614 {"rmdir", mac_rmdir},
615 {"stat", mac_stat},
Guido van Rossum222c8921995-08-08 14:10:22 +0000616 {"xstat", mac_xstat},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000617 {"sync", mac_sync},
Jack Jansen243b29b1996-02-21 12:33:50 +0000618 {"remove", mac_unlink},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000619 {"unlink", mac_unlink},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000620 {"write", mac_write},
Jack Jansen46ed2761996-10-23 15:46:57 +0000621#ifdef USE_MALLOC_DEBUG
Jack Jansend50e4e11995-01-18 13:58:04 +0000622 {"mstats", mac_mstats},
623#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000624
Guido van Rossumce9739b1994-01-05 16:17:15 +0000625 {NULL, NULL} /* Sentinel */
626};
627
628
629void
630initmac()
631{
Jack Jansenf5c20571997-01-30 15:48:07 +0000632 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000633
Jack Jansenf5c20571997-01-30 15:48:07 +0000634 m = Py_InitModule("mac", mac_methods);
635 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000636
637 /* Initialize mac.error exception */
Jack Jansen55e39271997-10-07 21:47:25 +0000638 MacError = PyErr_NewException("mac.error", NULL, NULL);
639 PyDict_SetItemString(d, "error", MacError);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000640}