blob: a100cf554ad3e6e897285e3708aa0ab54292ab8e [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
Jack Jansene79dc762000-06-02 21:35:07 +000034#ifndef TARGET_API_MAC_CARBON_NOTYET
35/* XXXX Skip for Carbon, for now */
Jack Jansen59b912a1996-10-15 16:13:33 +000036#include "macstat.h"
Jack Jansene79dc762000-06-02 21:35:07 +000037#endif
38
Jack Jansenc743c8d1996-02-14 16:02:30 +000039#ifdef USE_GUSI
Jack Jansen59b912a1996-10-15 16:13:33 +000040/* Remove defines from macstat.h */
41#undef S_IFMT
42#undef S_IFDIR
43#undef S_IFREG
44#undef S_IREAD
45#undef S_IWRITE
46#undef S_IEXEC
47
Jack Jansen37d21e12000-04-07 09:25:06 +000048#ifdef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +000049#include <GUSI.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000050#endif /* USE_GUSI1 */
Jack Jansenc743c8d1996-02-14 16:02:30 +000051#include <sys/types.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000052#include <sys/stat.h>
53#else /* USE_GUSI */
Jack Jansene79dc762000-06-02 21:35:07 +000054#ifndef TARGET_API_MAC_CARBON_NOTYET
Jack Jansen59b912a1996-10-15 16:13:33 +000055#define stat macstat
Jack Jansene79dc762000-06-02 21:35:07 +000056#endif
Jack Jansen37d21e12000-04-07 09:25:06 +000057#endif /* USE_GUSI */
Guido van Rossumce9739b1994-01-05 16:17:15 +000058
Jack Jansen37d21e12000-04-07 09:25:06 +000059#ifdef USE_GUSI2
60#define sync bad_sync
61#include <unistd.h>
62#include <fcntl.h>
63#undef sync
64int sync(void);
65#else
Jack Jansen12e89e42000-05-12 21:36:29 +000066#define mode_t int
Guido van Rossum87f223c1994-05-06 15:54:15 +000067#include <fcntl.h>
Jack Jansene79dc762000-06-02 21:35:07 +000068#ifdef _POSIX
69#include <unistd.h>
70#include <stat.h>
71#endif
Jack Jansen0c097ea1994-12-14 13:48:38 +000072#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000073
Jack Jansenb6971731996-02-20 16:24:37 +000074/* Optional routines, for some compiler/runtime combinations */
Jack Jansenb6971731996-02-20 16:24:37 +000075#if defined(USE_GUSI) || !defined(__MWERKS__)
76#define WEHAVE_FDOPEN
77#endif
78#if defined(MPW) || defined(USE_GUSI)
79#define WEHAVE_DUP
80#endif
Jack Jansen40bd7701998-02-20 15:56:19 +000081#if defined(USE_GUSI)
82#define WEHAVE_FSTAT
83#endif
Jack Jansen7cbf4801995-01-22 16:52:38 +000084
Guido van Rossumd4d77281994-08-19 10:51:31 +000085#include "macdefs.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000086#ifdef USE_GUSI
87#include <dirent.h>
88#else
Guido van Rossumd4d77281994-08-19 10:51:31 +000089#include "dirent.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000090#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +000091
92#ifndef MAXPATHLEN
93#define MAXPATHLEN 1024
94#endif
95
96/* Prototypes for Unix simulation on Mac */
97
Jack Jansenb6971731996-02-20 16:24:37 +000098#ifndef USE_GUSI
99
Jack Jansenf5c20571997-01-30 15:48:07 +0000100int chdir Py_PROTO((const char *path));
101int mkdir Py_PROTO((const char *path, int mode));
102DIR * opendir Py_PROTO((char *));
103void closedir Py_PROTO((DIR *));
104struct dirent * readdir Py_PROTO((DIR *));
105int rmdir Py_PROTO((const char *path));
106int sync Py_PROTO((void));
Jack Jansenb6971731996-02-20 16:24:37 +0000107
Jack Jansend4d24cd2000-06-04 22:02:02 +0000108#if defined(__SC__)
Jack Jansenf5c20571997-01-30 15:48:07 +0000109int unlink Py_PROTO((char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000110#else
Jack Jansenf5c20571997-01-30 15:48:07 +0000111int unlink Py_PROTO((const char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000112#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000113
Jack Jansenb6971731996-02-20 16:24:37 +0000114#endif /* USE_GUSI */
115
Jack Jansenf5c20571997-01-30 15:48:07 +0000116char *getwd Py_PROTO((char *));
117char *getbootvol Py_PROTO((void));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000118
119
Jack Jansenf5c20571997-01-30 15:48:07 +0000120static PyObject *MacError; /* Exception mac.error */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000121
122/* Set a MAC-specific error from errno, and return NULL */
123
Jack Jansenf5c20571997-01-30 15:48:07 +0000124static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000125mac_error()
126{
Jack Jansenf5c20571997-01-30 15:48:07 +0000127 return PyErr_SetFromErrno(MacError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000128}
129
130/* MAC generic methods */
131
Jack Jansenf5c20571997-01-30 15:48:07 +0000132static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000133mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000134 PyObject *args;
135 int (*func) Py_FPROTO((const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000136{
137 char *path1;
138 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000139 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000140 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000141 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000142 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000143 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000144 if (res < 0)
145 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000146 Py_INCREF(Py_None);
147 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000148}
149
Jack Jansenf5c20571997-01-30 15:48:07 +0000150static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000151mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000152 PyObject *args;
153 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000154{
155 char *path1, *path2;
156 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000157 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000158 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000159 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000160 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000161 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000162 if (res < 0)
163 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000164 Py_INCREF(Py_None);
165 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000166}
167
Jack Jansenf5c20571997-01-30 15:48:07 +0000168static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000169mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000170 PyObject *args;
171 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000172{
173 char *path;
174 int i;
175 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000176 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000177 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000178 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000179 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000180 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000181 if (res < 0)
182 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000183 Py_INCREF(Py_None);
184 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000185}
186
Jack Jansenf5c20571997-01-30 15:48:07 +0000187static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000188mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000189 PyObject *self;
190 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000191{
Jack Jansen37d21e12000-04-07 09:25:06 +0000192#ifdef USE_GUSI1
Jack Jansenf5c20571997-01-30 15:48:07 +0000193 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000194
195 /* Change MacOS's idea of wd too */
196 rv = mac_1str(args, chdir);
197 PyMac_FixGUSIcd();
198 return rv;
199#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000200 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000201#endif
202
Guido van Rossum87f223c1994-05-06 15:54:15 +0000203}
204
Jack Jansenf5c20571997-01-30 15:48:07 +0000205static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000206mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000207 PyObject *self;
208 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000209{
210 int fd, res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000211 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000212 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000213 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000214 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000215 Py_END_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000216#ifndef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +0000217 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000218 if (res < 0)
219 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000220#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000221 Py_INCREF(Py_None);
222 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000223}
224
Jack Jansenb6971731996-02-20 16:24:37 +0000225#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000226
Jack Jansenf5c20571997-01-30 15:48:07 +0000227static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000228mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000229 PyObject *self;
230 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000231{
232 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000233 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000234 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000235 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000236 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000237 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000238 if (fd < 0)
239 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000240 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000241}
242
Jack Jansenb6971731996-02-20 16:24:37 +0000243#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000244
Jack Jansenb6971731996-02-20 16:24:37 +0000245#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000246static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000247mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000248 PyObject *self;
249 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000250{
Jack Jansenf5c20571997-01-30 15:48:07 +0000251 extern int fclose Py_PROTO((FILE *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000252 int fd;
253 char *mode;
254 FILE *fp;
Jack Jansenf5c20571997-01-30 15:48:07 +0000255 if (!PyArg_Parse(args, "(is)", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000256 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000257 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000258 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000259 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000260 if (fp == NULL)
261 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000262 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000263}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000264#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000265
Jack Jansene79dc762000-06-02 21:35:07 +0000266#ifndef TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000267static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000268mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000269 PyObject *self;
270 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000271{
272 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000273 if (!PyArg_NoArgs(args))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000274 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000275 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000276 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000277 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000278 if (res == NULL)
279 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000280 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000281}
Jack Jansene79dc762000-06-02 21:35:07 +0000282#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000283
Jack Jansenf5c20571997-01-30 15:48:07 +0000284static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000285mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000286 PyObject *self;
287 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000288{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000289 char path[MAXPATHLEN];
290 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000291 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000292 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000293 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000294#ifdef USE_GUSI
295 res = getcwd(path, sizeof path);
296#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000297 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000298#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000299 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000300 if (res == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000301 PyErr_SetString(MacError, path);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000302 return NULL;
303 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000304 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000305}
306
Jack Jansenf5c20571997-01-30 15:48:07 +0000307static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000308mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000309 PyObject *self;
310 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000311{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000312 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000313 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000314 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000315 struct dirent *ep;
Jack Jansenf5c20571997-01-30 15:48:07 +0000316 if (!PyArg_Parse(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000317 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000318 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000319 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000320 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000321 return mac_error();
322 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000323 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000324 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000325 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000326 return NULL;
327 }
328 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000329 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000330 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000331 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000332 d = NULL;
333 break;
334 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000335 if (PyList_Append(d, v) != 0) {
336 Py_DECREF(v);
337 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000338 d = NULL;
339 break;
340 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000341 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000342 }
343 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000344 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000345
Guido van Rossumce9739b1994-01-05 16:17:15 +0000346 return d;
347}
348
Jack Jansenf5c20571997-01-30 15:48:07 +0000349static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000350mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000351 PyObject *self;
352 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000353{
354 int fd;
355 int where;
356 int how;
357 long res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000358 if (!PyArg_Parse(args, "(iii)", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000359 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000360 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000361 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000362 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000363 if (res < 0)
364 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000365 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000366}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000367
Jack Jansenf5c20571997-01-30 15:48:07 +0000368static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000369mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000370 PyObject *self;
371 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000372{
Jack Jansen243b29b1996-02-21 12:33:50 +0000373 int res;
374 char *path;
375 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000376 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000377 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000378 Py_BEGIN_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000379#ifdef USE_GUSI1
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000380 res = mkdir(path);
381#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000382 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000383#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000384 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000385 if (res < 0)
386 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000387 Py_INCREF(Py_None);
388 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000389}
390
Jack Jansenf5c20571997-01-30 15:48:07 +0000391static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000392mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000393 PyObject *self;
394 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000395{
396 char *path;
397 int mode;
398 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000399 if (!PyArg_Parse(args, "(si)", &path, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000400 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000401 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000402 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000403 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000404 if (fd < 0)
405 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000406 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000407}
408
Jack Jansenf5c20571997-01-30 15:48:07 +0000409static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000410mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000411 PyObject *self;
412 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000413{
414 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000415 PyObject *buffer;
416 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000417 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000418 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000419 if (buffer == NULL)
420 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000421 Py_BEGIN_ALLOW_THREADS
422 size = read(fd, PyString_AsString(buffer), size);
423 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000424 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000425 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000426 return mac_error();
427 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000428 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000429 return buffer;
430}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000431
Jack Jansenf5c20571997-01-30 15:48:07 +0000432static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000433mac_rename(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_2str(args, rename);
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_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000442 PyObject *self;
443 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000444{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000445 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000446}
447
Jack Jansenf5c20571997-01-30 15:48:07 +0000448static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000449mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000450 PyObject *self;
451 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000452{
Jack Jansen59b912a1996-10-15 16:13:33 +0000453 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000454 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000455 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000456 if (!PyArg_Parse(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000457 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000458 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000459 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000460 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000461 if (res != 0)
462 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000463 return Py_BuildValue("(lllllllddd)",
Jack Jansen59b912a1996-10-15 16:13:33 +0000464 (long)st.st_mode,
465 (long)st.st_ino,
466 (long)st.st_dev,
467 (long)st.st_nlink,
468 (long)st.st_uid,
469 (long)st.st_gid,
470 (long)st.st_size,
471 (double)st.st_atime,
472 (double)st.st_mtime,
473 (double)st.st_ctime);
Jack Jansen40bd7701998-02-20 15:56:19 +0000474}
475
476#ifdef WEHAVE_FSTAT
477static PyObject *
478mac_fstat(self, args)
479 PyObject *self;
480 PyObject *args;
481{
482 struct stat st;
483 long fd;
484 int res;
485 if (!PyArg_Parse(args, "l", &fd))
486 return NULL;
487 Py_BEGIN_ALLOW_THREADS
488 res = fstat((int)fd, &st);
489 Py_END_ALLOW_THREADS
490 if (res != 0)
491 return mac_error();
492 return Py_BuildValue("(lllllllddd)",
Guido van Rossum87f223c1994-05-06 15:54:15 +0000493 (long)st.st_mode,
Guido van Rossume7834441994-08-26 09:09:48 +0000494 (long)st.st_ino,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000495 (long)st.st_dev,
496 (long)st.st_nlink,
497 (long)st.st_uid,
498 (long)st.st_gid,
Guido van Rossum87f223c1994-05-06 15:54:15 +0000499 (long)st.st_size,
Jack Jansen40bd7701998-02-20 15:56:19 +0000500 (double)st.st_atime,
501 (double)st.st_mtime,
502 (double)st.st_ctime);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000503}
Jack Jansen40bd7701998-02-20 15:56:19 +0000504#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000505
Jack Jansene79dc762000-06-02 21:35:07 +0000506#ifndef TARGET_API_MAC_CARBON_NOTYET
Jack Jansenf5c20571997-01-30 15:48:07 +0000507static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000508mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000509 PyObject *self;
510 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000511{
Jack Jansen59b912a1996-10-15 16:13:33 +0000512 struct macstat mst;
513 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000514 char *path;
515 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000516 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000517 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000518 /*
519 ** Convoluted: we want stat() and xstat() to agree, so we call both
520 ** stat and macstat, and use the latter only for values not provided by
521 ** the former.
522 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000523 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000524 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000525 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000526 if (res != 0)
527 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000528 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000529 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000530 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000531 if (res != 0)
532 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000533 return Py_BuildValue("(llllllldddls#s#)",
Jack Jansenc743c8d1996-02-14 16:02:30 +0000534 (long)st.st_mode,
535 (long)st.st_ino,
536 (long)st.st_dev,
537 (long)st.st_nlink,
538 (long)st.st_uid,
539 (long)st.st_gid,
540 (long)st.st_size,
Jack Jansen59b912a1996-10-15 16:13:33 +0000541 (double)st.st_atime,
542 (double)st.st_mtime,
543 (double)st.st_ctime,
544 (long)mst.st_rsize,
545 mst.st_creator, 4,
546 mst.st_type, 4);
Guido van Rossum222c8921995-08-08 14:10:22 +0000547}
Jack Jansene79dc762000-06-02 21:35:07 +0000548#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000549
Jack Jansenf5c20571997-01-30 15:48:07 +0000550static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000551mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000552 PyObject *self;
553 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000554{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000555 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000556 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000557 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000558 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000559 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000560 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000561 if (res != 0)
562 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000563 Py_INCREF(Py_None);
564 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000565}
566
Jack Jansenf5c20571997-01-30 15:48:07 +0000567static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000568mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000569 PyObject *self;
570 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000571{
Guido van Rossum739267b1994-08-29 08:42:37 +0000572 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000573}
574
Jack Jansenf5c20571997-01-30 15:48:07 +0000575static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000576mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000577 PyObject *self;
578 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000579{
580 int fd, size;
581 char *buffer;
Jack Janseneeccca91997-05-07 15:46:31 +0000582 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000583 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000584 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000585 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000586 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000587 if (size < 0)
588 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000589 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000590}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000591
Jack Jansen46ed2761996-10-23 15:46:57 +0000592#ifdef USE_MALLOC_DEBUG
Jack Jansenf5c20571997-01-30 15:48:07 +0000593static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000594mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000595 PyObject*self;
596 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000597{
598 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000599 Py_INCREF(Py_None);
600 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000601}
Jack Jansen4f27a551997-02-20 15:22:17 +0000602#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000603
Jack Jansenf5c20571997-01-30 15:48:07 +0000604static struct PyMethodDef mac_methods[] = {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000605 {"chdir", mac_chdir},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000606 {"close", mac_close},
Jack Jansenb6971731996-02-20 16:24:37 +0000607#ifdef WEHAVE_DUP
Guido van Rossum87f223c1994-05-06 15:54:15 +0000608 {"dup", mac_dup},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000609#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000610#ifdef WEHAVE_FDOPEN
Guido van Rossume7834441994-08-26 09:09:48 +0000611 {"fdopen", mac_fdopen},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000612#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000613#ifdef WEHAVE_FSTAT
614 {"fstat", mac_fstat},
615#endif
Jack Jansene79dc762000-06-02 21:35:07 +0000616#ifndef TARGET_API_MAC_CARBON
Guido van Rossum87f223c1994-05-06 15:54:15 +0000617 {"getbootvol", mac_getbootvol}, /* non-standard */
Jack Jansene79dc762000-06-02 21:35:07 +0000618#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000619 {"getcwd", mac_getcwd},
Guido van Rossumd1ef5961995-02-19 15:50:35 +0000620 {"listdir", mac_listdir, 0},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000621 {"lseek", mac_lseek},
Jack Jansen243b29b1996-02-21 12:33:50 +0000622 {"mkdir", mac_mkdir, 1},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000623 {"open", mac_open},
624 {"read", mac_read},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000625 {"rename", mac_rename},
626 {"rmdir", mac_rmdir},
627 {"stat", mac_stat},
Jack Jansene79dc762000-06-02 21:35:07 +0000628#ifndef TARGET_API_MAC_CARBON_NOTYET
Guido van Rossum222c8921995-08-08 14:10:22 +0000629 {"xstat", mac_xstat},
Jack Jansene79dc762000-06-02 21:35:07 +0000630#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000631 {"sync", mac_sync},
Jack Jansen243b29b1996-02-21 12:33:50 +0000632 {"remove", mac_unlink},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000633 {"unlink", mac_unlink},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000634 {"write", mac_write},
Jack Jansen46ed2761996-10-23 15:46:57 +0000635#ifdef USE_MALLOC_DEBUG
Jack Jansend50e4e11995-01-18 13:58:04 +0000636 {"mstats", mac_mstats},
637#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000638
Guido van Rossumce9739b1994-01-05 16:17:15 +0000639 {NULL, NULL} /* Sentinel */
640};
641
642
643void
644initmac()
645{
Jack Jansenf5c20571997-01-30 15:48:07 +0000646 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000647
Jack Jansenf5c20571997-01-30 15:48:07 +0000648 m = Py_InitModule("mac", mac_methods);
649 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000650
651 /* Initialize mac.error exception */
Jack Jansen55e39271997-10-07 21:47:25 +0000652 MacError = PyErr_NewException("mac.error", NULL, NULL);
653 PyDict_SetItemString(d, "error", MacError);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000654}