blob: 74c1243c558af14a10307a9f4b14be940cd643ab [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 Jansene79dc762000-06-02 21:35:07 +000045#ifndef TARGET_API_MAC_CARBON_NOTYET
46/* XXXX Skip for Carbon, for now */
Jack Jansen59b912a1996-10-15 16:13:33 +000047#include "macstat.h"
Jack Jansene79dc762000-06-02 21:35:07 +000048#endif
49
Jack Jansenc743c8d1996-02-14 16:02:30 +000050#ifdef USE_GUSI
Jack Jansen59b912a1996-10-15 16:13:33 +000051/* Remove defines from macstat.h */
52#undef S_IFMT
53#undef S_IFDIR
54#undef S_IFREG
55#undef S_IREAD
56#undef S_IWRITE
57#undef S_IEXEC
58
Jack Jansen37d21e12000-04-07 09:25:06 +000059#ifdef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +000060#include <GUSI.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000061#endif /* USE_GUSI1 */
Jack Jansenc743c8d1996-02-14 16:02:30 +000062#include <sys/types.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000063#include <sys/stat.h>
64#else /* USE_GUSI */
Jack Jansene79dc762000-06-02 21:35:07 +000065#ifndef TARGET_API_MAC_CARBON_NOTYET
Jack Jansen59b912a1996-10-15 16:13:33 +000066#define stat macstat
Jack Jansene79dc762000-06-02 21:35:07 +000067#endif
Jack Jansen37d21e12000-04-07 09:25:06 +000068#endif /* USE_GUSI */
Guido van Rossumce9739b1994-01-05 16:17:15 +000069
Jack Jansen37d21e12000-04-07 09:25:06 +000070#ifdef USE_GUSI2
71#define sync bad_sync
72#include <unistd.h>
73#include <fcntl.h>
74#undef sync
75int sync(void);
76#else
Jack Jansen12e89e42000-05-12 21:36:29 +000077#define mode_t int
Guido van Rossum87f223c1994-05-06 15:54:15 +000078#include <fcntl.h>
Jack Jansene79dc762000-06-02 21:35:07 +000079#ifdef _POSIX
80#include <unistd.h>
81#include <stat.h>
82#endif
Jack Jansen0c097ea1994-12-14 13:48:38 +000083#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000084
Jack Jansenb6971731996-02-20 16:24:37 +000085/* Optional routines, for some compiler/runtime combinations */
Jack Jansenb6971731996-02-20 16:24:37 +000086#if defined(USE_GUSI) || !defined(__MWERKS__)
87#define WEHAVE_FDOPEN
88#endif
89#if defined(MPW) || defined(USE_GUSI)
90#define WEHAVE_DUP
91#endif
Jack Jansen40bd7701998-02-20 15:56:19 +000092#if defined(USE_GUSI)
93#define WEHAVE_FSTAT
94#endif
Jack Jansen7cbf4801995-01-22 16:52:38 +000095
Guido van Rossumd4d77281994-08-19 10:51:31 +000096#include "macdefs.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000097#ifdef USE_GUSI
98#include <dirent.h>
99#else
Guido van Rossumd4d77281994-08-19 10:51:31 +0000100#include "dirent.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +0000101#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000102
103#ifndef MAXPATHLEN
104#define MAXPATHLEN 1024
105#endif
106
107/* Prototypes for Unix simulation on Mac */
108
Jack Jansenb6971731996-02-20 16:24:37 +0000109#ifndef USE_GUSI
110
Jack Jansenf5c20571997-01-30 15:48:07 +0000111int chdir Py_PROTO((const char *path));
112int mkdir Py_PROTO((const char *path, int mode));
113DIR * opendir Py_PROTO((char *));
114void closedir Py_PROTO((DIR *));
115struct dirent * readdir Py_PROTO((DIR *));
116int rmdir Py_PROTO((const char *path));
117int sync Py_PROTO((void));
Jack Jansenb6971731996-02-20 16:24:37 +0000118
119#if defined(THINK_C) || defined(__SC__)
Jack Jansenf5c20571997-01-30 15:48:07 +0000120int unlink Py_PROTO((char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000121#else
Jack Jansenf5c20571997-01-30 15:48:07 +0000122int unlink Py_PROTO((const char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000123#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000124
Jack Jansenb6971731996-02-20 16:24:37 +0000125#endif /* USE_GUSI */
126
Jack Jansenf5c20571997-01-30 15:48:07 +0000127char *getwd Py_PROTO((char *));
128char *getbootvol Py_PROTO((void));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000129
130
Jack Jansenf5c20571997-01-30 15:48:07 +0000131static PyObject *MacError; /* Exception mac.error */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000132
133/* Set a MAC-specific error from errno, and return NULL */
134
Jack Jansenf5c20571997-01-30 15:48:07 +0000135static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000136mac_error()
137{
Jack Jansenf5c20571997-01-30 15:48:07 +0000138 return PyErr_SetFromErrno(MacError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000139}
140
141/* MAC generic methods */
142
Jack Jansenf5c20571997-01-30 15:48:07 +0000143static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000144mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000145 PyObject *args;
146 int (*func) Py_FPROTO((const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000147{
148 char *path1;
149 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000150 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000151 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000152 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000153 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000154 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000155 if (res < 0)
156 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000157 Py_INCREF(Py_None);
158 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000159}
160
Jack Jansenf5c20571997-01-30 15:48:07 +0000161static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000162mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000163 PyObject *args;
164 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000165{
166 char *path1, *path2;
167 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000168 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000169 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000170 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000171 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000172 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000173 if (res < 0)
174 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000175 Py_INCREF(Py_None);
176 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000177}
178
Jack Jansenf5c20571997-01-30 15:48:07 +0000179static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000180mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000181 PyObject *args;
182 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000183{
184 char *path;
185 int i;
186 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000187 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000188 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000189 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000190 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000191 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000192 if (res < 0)
193 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000194 Py_INCREF(Py_None);
195 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000196}
197
Jack Jansenf5c20571997-01-30 15:48:07 +0000198static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000199mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000200 PyObject *self;
201 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000202{
Jack Jansen37d21e12000-04-07 09:25:06 +0000203#ifdef USE_GUSI1
Jack Jansenf5c20571997-01-30 15:48:07 +0000204 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000205
206 /* Change MacOS's idea of wd too */
207 rv = mac_1str(args, chdir);
208 PyMac_FixGUSIcd();
209 return rv;
210#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000211 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000212#endif
213
Guido van Rossum87f223c1994-05-06 15:54:15 +0000214}
215
Jack Jansenf5c20571997-01-30 15:48:07 +0000216static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000217mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000218 PyObject *self;
219 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000220{
221 int fd, res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000222 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000223 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000224 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000225 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000226 Py_END_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000227#ifndef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +0000228 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000229 if (res < 0)
230 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000231#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000232 Py_INCREF(Py_None);
233 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000234}
235
Jack Jansenb6971731996-02-20 16:24:37 +0000236#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000237
Jack Jansenf5c20571997-01-30 15:48:07 +0000238static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000239mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000240 PyObject *self;
241 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000242{
243 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000244 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000245 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000246 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000247 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000248 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000249 if (fd < 0)
250 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000251 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000252}
253
Jack Jansenb6971731996-02-20 16:24:37 +0000254#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000255
Jack Jansenb6971731996-02-20 16:24:37 +0000256#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000257static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000258mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000259 PyObject *self;
260 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000261{
Jack Jansenf5c20571997-01-30 15:48:07 +0000262 extern int fclose Py_PROTO((FILE *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000263 int fd;
264 char *mode;
265 FILE *fp;
Jack Jansenf5c20571997-01-30 15:48:07 +0000266 if (!PyArg_Parse(args, "(is)", &fd, &mode))
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 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000270 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000271 if (fp == NULL)
272 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000273 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000274}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000275#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000276
Jack Jansene79dc762000-06-02 21:35:07 +0000277#ifndef TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000278static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000279mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000280 PyObject *self;
281 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000282{
283 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000284 if (!PyArg_NoArgs(args))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000285 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000286 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000287 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000288 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000289 if (res == NULL)
290 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000291 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000292}
Jack Jansene79dc762000-06-02 21:35:07 +0000293#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000294
Jack Jansenf5c20571997-01-30 15:48:07 +0000295static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000296mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000297 PyObject *self;
298 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000299{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000300 char path[MAXPATHLEN];
301 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000302 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000303 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000304 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000305#ifdef USE_GUSI
306 res = getcwd(path, sizeof path);
307#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000308 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000309#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000310 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000311 if (res == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000312 PyErr_SetString(MacError, path);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000313 return NULL;
314 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000315 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000316}
317
Jack Jansenf5c20571997-01-30 15:48:07 +0000318static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000319mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000320 PyObject *self;
321 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000322{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000323 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000324 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000325 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000326 struct dirent *ep;
Jack Jansenf5c20571997-01-30 15:48:07 +0000327 if (!PyArg_Parse(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000328 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000329 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000330 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000331 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000332 return mac_error();
333 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000334 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000335 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000336 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000337 return NULL;
338 }
339 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000341 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000342 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000343 d = NULL;
344 break;
345 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000346 if (PyList_Append(d, v) != 0) {
347 Py_DECREF(v);
348 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000349 d = NULL;
350 break;
351 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000352 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000353 }
354 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000355 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000356
Guido van Rossumce9739b1994-01-05 16:17:15 +0000357 return d;
358}
359
Jack Jansenf5c20571997-01-30 15:48:07 +0000360static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000361mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000362 PyObject *self;
363 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000364{
365 int fd;
366 int where;
367 int how;
368 long res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000369 if (!PyArg_Parse(args, "(iii)", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000370 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000371 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000372 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000373 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000374 if (res < 0)
375 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000376 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000377}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000378
Jack Jansenf5c20571997-01-30 15:48:07 +0000379static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000380mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000381 PyObject *self;
382 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000383{
Jack Jansen243b29b1996-02-21 12:33:50 +0000384 int res;
385 char *path;
386 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000387 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000388 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000389 Py_BEGIN_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000390#ifdef USE_GUSI1
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000391 res = mkdir(path);
392#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000393 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000394#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000395 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000396 if (res < 0)
397 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000398 Py_INCREF(Py_None);
399 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000400}
401
Jack Jansenf5c20571997-01-30 15:48:07 +0000402static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000403mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000404 PyObject *self;
405 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000406{
407 char *path;
408 int mode;
409 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000410 if (!PyArg_Parse(args, "(si)", &path, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000411 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000412 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000413 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000414 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000415 if (fd < 0)
416 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000418}
419
Jack Jansenf5c20571997-01-30 15:48:07 +0000420static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000421mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000422 PyObject *self;
423 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000424{
425 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000426 PyObject *buffer;
427 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000428 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000429 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000430 if (buffer == NULL)
431 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000432 Py_BEGIN_ALLOW_THREADS
433 size = read(fd, PyString_AsString(buffer), size);
434 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000435 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000436 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000437 return mac_error();
438 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000439 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000440 return buffer;
441}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000442
Jack Jansenf5c20571997-01-30 15:48:07 +0000443static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000444mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000445 PyObject *self;
446 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000447{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000448 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000449}
450
Jack Jansenf5c20571997-01-30 15:48:07 +0000451static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000452mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000453 PyObject *self;
454 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000455{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000456 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000457}
458
Jack Jansenf5c20571997-01-30 15:48:07 +0000459static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000460mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000461 PyObject *self;
462 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000463{
Jack Jansen59b912a1996-10-15 16:13:33 +0000464 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000465 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000466 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000467 if (!PyArg_Parse(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000468 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000469 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000470 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000471 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000472 if (res != 0)
473 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000474 return Py_BuildValue("(lllllllddd)",
Jack Jansen59b912a1996-10-15 16:13:33 +0000475 (long)st.st_mode,
476 (long)st.st_ino,
477 (long)st.st_dev,
478 (long)st.st_nlink,
479 (long)st.st_uid,
480 (long)st.st_gid,
481 (long)st.st_size,
482 (double)st.st_atime,
483 (double)st.st_mtime,
484 (double)st.st_ctime);
Jack Jansen40bd7701998-02-20 15:56:19 +0000485}
486
487#ifdef WEHAVE_FSTAT
488static PyObject *
489mac_fstat(self, args)
490 PyObject *self;
491 PyObject *args;
492{
493 struct stat st;
494 long fd;
495 int res;
496 if (!PyArg_Parse(args, "l", &fd))
497 return NULL;
498 Py_BEGIN_ALLOW_THREADS
499 res = fstat((int)fd, &st);
500 Py_END_ALLOW_THREADS
501 if (res != 0)
502 return mac_error();
503 return Py_BuildValue("(lllllllddd)",
Guido van Rossum87f223c1994-05-06 15:54:15 +0000504 (long)st.st_mode,
Guido van Rossume7834441994-08-26 09:09:48 +0000505 (long)st.st_ino,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000506 (long)st.st_dev,
507 (long)st.st_nlink,
508 (long)st.st_uid,
509 (long)st.st_gid,
Guido van Rossum87f223c1994-05-06 15:54:15 +0000510 (long)st.st_size,
Jack Jansen40bd7701998-02-20 15:56:19 +0000511 (double)st.st_atime,
512 (double)st.st_mtime,
513 (double)st.st_ctime);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000514}
Jack Jansen40bd7701998-02-20 15:56:19 +0000515#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000516
Jack Jansene79dc762000-06-02 21:35:07 +0000517#ifndef TARGET_API_MAC_CARBON_NOTYET
Jack Jansenf5c20571997-01-30 15:48:07 +0000518static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000519mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000520 PyObject *self;
521 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000522{
Jack Jansen59b912a1996-10-15 16:13:33 +0000523 struct macstat mst;
524 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000525 char *path;
526 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000527 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000528 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000529 /*
530 ** Convoluted: we want stat() and xstat() to agree, so we call both
531 ** stat and macstat, and use the latter only for values not provided by
532 ** the former.
533 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000534 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000535 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000536 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000537 if (res != 0)
538 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000539 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000540 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000541 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000542 if (res != 0)
543 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000544 return Py_BuildValue("(llllllldddls#s#)",
Jack Jansenc743c8d1996-02-14 16:02:30 +0000545 (long)st.st_mode,
546 (long)st.st_ino,
547 (long)st.st_dev,
548 (long)st.st_nlink,
549 (long)st.st_uid,
550 (long)st.st_gid,
551 (long)st.st_size,
Jack Jansen59b912a1996-10-15 16:13:33 +0000552 (double)st.st_atime,
553 (double)st.st_mtime,
554 (double)st.st_ctime,
555 (long)mst.st_rsize,
556 mst.st_creator, 4,
557 mst.st_type, 4);
Guido van Rossum222c8921995-08-08 14:10:22 +0000558}
Jack Jansene79dc762000-06-02 21:35:07 +0000559#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000560
Jack Jansenf5c20571997-01-30 15:48:07 +0000561static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000562mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000563 PyObject *self;
564 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000565{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000566 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000567 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000568 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000569 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000570 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000571 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000572 if (res != 0)
573 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000574 Py_INCREF(Py_None);
575 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000576}
577
Jack Jansenf5c20571997-01-30 15:48:07 +0000578static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000579mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000580 PyObject *self;
581 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000582{
Guido van Rossum739267b1994-08-29 08:42:37 +0000583 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000584}
585
Jack Jansenf5c20571997-01-30 15:48:07 +0000586static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000587mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000588 PyObject *self;
589 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000590{
591 int fd, size;
592 char *buffer;
Jack Janseneeccca91997-05-07 15:46:31 +0000593 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000594 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000595 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000596 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000597 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000598 if (size < 0)
599 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000600 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000601}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000602
Jack Jansen46ed2761996-10-23 15:46:57 +0000603#ifdef USE_MALLOC_DEBUG
Jack Jansenf5c20571997-01-30 15:48:07 +0000604static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000605mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000606 PyObject*self;
607 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000608{
609 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000610 Py_INCREF(Py_None);
611 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000612}
Jack Jansen4f27a551997-02-20 15:22:17 +0000613#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000614
Jack Jansenf5c20571997-01-30 15:48:07 +0000615static struct PyMethodDef mac_methods[] = {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000616 {"chdir", mac_chdir},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000617 {"close", mac_close},
Jack Jansenb6971731996-02-20 16:24:37 +0000618#ifdef WEHAVE_DUP
Guido van Rossum87f223c1994-05-06 15:54:15 +0000619 {"dup", mac_dup},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000620#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000621#ifdef WEHAVE_FDOPEN
Guido van Rossume7834441994-08-26 09:09:48 +0000622 {"fdopen", mac_fdopen},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000623#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000624#ifdef WEHAVE_FSTAT
625 {"fstat", mac_fstat},
626#endif
Jack Jansene79dc762000-06-02 21:35:07 +0000627#ifndef TARGET_API_MAC_CARBON
Guido van Rossum87f223c1994-05-06 15:54:15 +0000628 {"getbootvol", mac_getbootvol}, /* non-standard */
Jack Jansene79dc762000-06-02 21:35:07 +0000629#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000630 {"getcwd", mac_getcwd},
Guido van Rossumd1ef5961995-02-19 15:50:35 +0000631 {"listdir", mac_listdir, 0},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000632 {"lseek", mac_lseek},
Jack Jansen243b29b1996-02-21 12:33:50 +0000633 {"mkdir", mac_mkdir, 1},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000634 {"open", mac_open},
635 {"read", mac_read},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000636 {"rename", mac_rename},
637 {"rmdir", mac_rmdir},
638 {"stat", mac_stat},
Jack Jansene79dc762000-06-02 21:35:07 +0000639#ifndef TARGET_API_MAC_CARBON_NOTYET
Guido van Rossum222c8921995-08-08 14:10:22 +0000640 {"xstat", mac_xstat},
Jack Jansene79dc762000-06-02 21:35:07 +0000641#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000642 {"sync", mac_sync},
Jack Jansen243b29b1996-02-21 12:33:50 +0000643 {"remove", mac_unlink},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000644 {"unlink", mac_unlink},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000645 {"write", mac_write},
Jack Jansen46ed2761996-10-23 15:46:57 +0000646#ifdef USE_MALLOC_DEBUG
Jack Jansend50e4e11995-01-18 13:58:04 +0000647 {"mstats", mac_mstats},
648#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000649
Guido van Rossumce9739b1994-01-05 16:17:15 +0000650 {NULL, NULL} /* Sentinel */
651};
652
653
654void
655initmac()
656{
Jack Jansenf5c20571997-01-30 15:48:07 +0000657 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000658
Jack Jansenf5c20571997-01-30 15:48:07 +0000659 m = Py_InitModule("mac", mac_methods);
660 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000661
662 /* Initialize mac.error exception */
Jack Jansen55e39271997-10-07 21:47:25 +0000663 MacError = PyErr_NewException("mac.error", NULL, NULL);
664 PyDict_SetItemString(d, "error", MacError);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000665}