blob: d5f283b68c05edba03f1b31f14e58c1e2906d593 [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 Jansen6e68a7e2001-05-12 21:31:34 +000034#if TARGET_API_MAC_OS8
Jack Jansend9d0b5f2001-01-12 23:37:14 +000035/* Skip for Carbon */
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 Jansen6e68a7e2001-05-12 21:31:34 +000054#if TARGET_API_MAC_OS8
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 Jansend88296d2000-07-11 19:51:05 +0000100int chdir(const char *path);
101int mkdir(const char *path, int mode);
102DIR * opendir(char *);
103void closedir(DIR *);
104struct dirent * readdir(DIR *);
105int rmdir(const char *path);
106int sync(void);
Jack Jansenb6971731996-02-20 16:24:37 +0000107
Jack Jansend88296d2000-07-11 19:51:05 +0000108int unlink(const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000109
Jack Jansenb6971731996-02-20 16:24:37 +0000110#endif /* USE_GUSI */
111
Jack Jansend88296d2000-07-11 19:51:05 +0000112char *getwd(char *);
113char *getbootvol(void);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000114
115
Jack Jansenf5c20571997-01-30 15:48:07 +0000116static PyObject *MacError; /* Exception mac.error */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000117
118/* Set a MAC-specific error from errno, and return NULL */
119
Jack Jansenf5c20571997-01-30 15:48:07 +0000120static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000121mac_error()
122{
Jack Jansenf5c20571997-01-30 15:48:07 +0000123 return PyErr_SetFromErrno(MacError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000124}
125
126/* MAC generic methods */
127
Jack Jansenf5c20571997-01-30 15:48:07 +0000128static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000129mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000130 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000131 int (*func)(const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000132{
133 char *path1;
134 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000135 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000136 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000137 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000138 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000139 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000140 if (res < 0)
141 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000142 Py_INCREF(Py_None);
143 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000144}
145
Jack Jansenf5c20571997-01-30 15:48:07 +0000146static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000147mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000148 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000149 int (*func)(const char *, const char *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000150{
151 char *path1, *path2;
152 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000153 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000154 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000155 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000156 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000157 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000158 if (res < 0)
159 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000160 Py_INCREF(Py_None);
161 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000162}
163
Jack Jansenf5c20571997-01-30 15:48:07 +0000164static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000165mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000166 PyObject *args;
Jack Jansend88296d2000-07-11 19:51:05 +0000167 int (*func)(const char *, int);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000168{
169 char *path;
170 int i;
171 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000172 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000173 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000174 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000175 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000176 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000177 if (res < 0)
178 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000179 Py_INCREF(Py_None);
180 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000181}
182
Jack Jansenf5c20571997-01-30 15:48:07 +0000183static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000184mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000185 PyObject *self;
186 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000187{
Jack Jansen37d21e12000-04-07 09:25:06 +0000188#ifdef USE_GUSI1
Jack Jansenf5c20571997-01-30 15:48:07 +0000189 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000190
191 /* Change MacOS's idea of wd too */
192 rv = mac_1str(args, chdir);
193 PyMac_FixGUSIcd();
194 return rv;
195#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000196 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000197#endif
198
Guido van Rossum87f223c1994-05-06 15:54:15 +0000199}
200
Jack Jansenf5c20571997-01-30 15:48:07 +0000201static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000202mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000203 PyObject *self;
204 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000205{
206 int fd, res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000207 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000208 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000209 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000210 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000211 Py_END_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000212#ifndef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +0000213 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000214 if (res < 0)
215 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000216#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000217 Py_INCREF(Py_None);
218 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000219}
220
Jack Jansenb6971731996-02-20 16:24:37 +0000221#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000222
Jack Jansenf5c20571997-01-30 15:48:07 +0000223static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000224mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000225 PyObject *self;
226 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000227{
228 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000229 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000230 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000231 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000232 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000233 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000234 if (fd < 0)
235 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000236 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000237}
238
Jack Jansenb6971731996-02-20 16:24:37 +0000239#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000240
Jack Jansenb6971731996-02-20 16:24:37 +0000241#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000242static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000243mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000244 PyObject *self;
245 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000246{
Jack Jansend88296d2000-07-11 19:51:05 +0000247 extern int fclose(FILE *);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000248 int fd;
249 char *mode;
250 FILE *fp;
Jack Jansenf5c20571997-01-30 15:48:07 +0000251 if (!PyArg_Parse(args, "(is)", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000252 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000253 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000254 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000255 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000256 if (fp == NULL)
257 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000258 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000259}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000260#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000261
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000262#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000263static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000264mac_getbootvol(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000265 PyObject *self;
266 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000267{
268 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000269 if (!PyArg_NoArgs(args))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000270 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000271 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000272 res = getbootvol();
Jack Jansenf5c20571997-01-30 15:48:07 +0000273 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000274 if (res == NULL)
275 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000276 return PyString_FromString(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000277}
Jack Jansene79dc762000-06-02 21:35:07 +0000278#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000279
Jack Jansenf5c20571997-01-30 15:48:07 +0000280static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000281mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000282 PyObject *self;
283 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000284{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000285 char path[MAXPATHLEN];
286 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000287 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000288 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000289 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000290#ifdef USE_GUSI
291 res = getcwd(path, sizeof path);
292#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000293 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000294#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000295 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000296 if (res == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000297 PyErr_SetString(MacError, path);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000298 return NULL;
299 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000300 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000301}
302
Jack Jansenf5c20571997-01-30 15:48:07 +0000303static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000304mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000305 PyObject *self;
306 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000307{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000308 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000309 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000310 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000311 struct dirent *ep;
Jack Jansenf5c20571997-01-30 15:48:07 +0000312 if (!PyArg_Parse(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000313 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000314 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000315 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000316 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000317 return mac_error();
318 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000319 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000320 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000321 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000322 return NULL;
323 }
324 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000325 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000326 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000327 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000328 d = NULL;
329 break;
330 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000331 if (PyList_Append(d, v) != 0) {
332 Py_DECREF(v);
333 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000334 d = NULL;
335 break;
336 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000337 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000338 }
339 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000341
Guido van Rossumce9739b1994-01-05 16:17:15 +0000342 return d;
343}
344
Jack Jansenf5c20571997-01-30 15:48:07 +0000345static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000346mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000347 PyObject *self;
348 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000349{
350 int fd;
351 int where;
352 int how;
353 long res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000354 if (!PyArg_Parse(args, "(iii)", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000355 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000356 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000357 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000358 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000359 if (res < 0)
360 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000361 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000362}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000363
Jack Jansenf5c20571997-01-30 15:48:07 +0000364static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000365mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000366 PyObject *self;
367 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000368{
Jack Jansen243b29b1996-02-21 12:33:50 +0000369 int res;
370 char *path;
371 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000372 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000373 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000374 Py_BEGIN_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000375#ifdef USE_GUSI1
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000376 res = mkdir(path);
377#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000378 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000379#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000380 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000381 if (res < 0)
382 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000383 Py_INCREF(Py_None);
384 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000385}
386
Jack Jansenf5c20571997-01-30 15:48:07 +0000387static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000388mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000389 PyObject *self;
390 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000391{
392 char *path;
393 int mode;
394 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000395 if (!PyArg_Parse(args, "(si)", &path, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000396 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000397 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000398 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000399 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000400 if (fd < 0)
401 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000402 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000403}
404
Jack Jansenf5c20571997-01-30 15:48:07 +0000405static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000406mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000407 PyObject *self;
408 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000409{
410 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000411 PyObject *buffer;
412 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000413 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000414 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000415 if (buffer == NULL)
416 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 Py_BEGIN_ALLOW_THREADS
418 size = read(fd, PyString_AsString(buffer), size);
419 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000420 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000421 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000422 return mac_error();
423 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000424 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000425 return buffer;
426}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000427
Jack Jansenf5c20571997-01-30 15:48:07 +0000428static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000429mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000430 PyObject *self;
431 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000432{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000433 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000434}
435
Jack Jansenf5c20571997-01-30 15:48:07 +0000436static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000437mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000438 PyObject *self;
439 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000440{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000441 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000442}
443
Jack Jansenf5c20571997-01-30 15:48:07 +0000444static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000445mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000446 PyObject *self;
447 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000448{
Jack Jansen59b912a1996-10-15 16:13:33 +0000449 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000450 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000451 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000452 if (!PyArg_Parse(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000453 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000454 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000455 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000456 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000457 if (res != 0)
458 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000459 return Py_BuildValue("(lllllllddd)",
Jack Jansen59b912a1996-10-15 16:13:33 +0000460 (long)st.st_mode,
461 (long)st.st_ino,
462 (long)st.st_dev,
463 (long)st.st_nlink,
464 (long)st.st_uid,
465 (long)st.st_gid,
466 (long)st.st_size,
467 (double)st.st_atime,
468 (double)st.st_mtime,
469 (double)st.st_ctime);
Jack Jansen40bd7701998-02-20 15:56:19 +0000470}
471
472#ifdef WEHAVE_FSTAT
473static PyObject *
474mac_fstat(self, args)
475 PyObject *self;
476 PyObject *args;
477{
478 struct stat st;
479 long fd;
480 int res;
481 if (!PyArg_Parse(args, "l", &fd))
482 return NULL;
483 Py_BEGIN_ALLOW_THREADS
484 res = fstat((int)fd, &st);
485 Py_END_ALLOW_THREADS
486 if (res != 0)
487 return mac_error();
488 return Py_BuildValue("(lllllllddd)",
Guido van Rossum87f223c1994-05-06 15:54:15 +0000489 (long)st.st_mode,
Guido van Rossume7834441994-08-26 09:09:48 +0000490 (long)st.st_ino,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000491 (long)st.st_dev,
492 (long)st.st_nlink,
493 (long)st.st_uid,
494 (long)st.st_gid,
Guido van Rossum87f223c1994-05-06 15:54:15 +0000495 (long)st.st_size,
Jack Jansen40bd7701998-02-20 15:56:19 +0000496 (double)st.st_atime,
497 (double)st.st_mtime,
498 (double)st.st_ctime);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000499}
Jack Jansen40bd7701998-02-20 15:56:19 +0000500#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000501
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000502#if TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000503static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000504mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000505 PyObject *self;
506 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000507{
Jack Jansen59b912a1996-10-15 16:13:33 +0000508 struct macstat mst;
509 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000510 char *path;
511 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000512 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000513 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000514 /*
515 ** Convoluted: we want stat() and xstat() to agree, so we call both
516 ** stat and macstat, and use the latter only for values not provided by
517 ** the former.
518 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000519 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000520 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000521 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000522 if (res != 0)
523 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000524 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000525 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000526 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000527 if (res != 0)
528 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000529 return Py_BuildValue("(llllllldddls#s#)",
Jack Jansenc743c8d1996-02-14 16:02:30 +0000530 (long)st.st_mode,
531 (long)st.st_ino,
532 (long)st.st_dev,
533 (long)st.st_nlink,
534 (long)st.st_uid,
535 (long)st.st_gid,
536 (long)st.st_size,
Jack Jansen59b912a1996-10-15 16:13:33 +0000537 (double)st.st_atime,
538 (double)st.st_mtime,
539 (double)st.st_ctime,
540 (long)mst.st_rsize,
541 mst.st_creator, 4,
542 mst.st_type, 4);
Guido van Rossum222c8921995-08-08 14:10:22 +0000543}
Jack Jansene79dc762000-06-02 21:35:07 +0000544#endif
Guido van Rossum222c8921995-08-08 14:10:22 +0000545
Jack Jansenf5c20571997-01-30 15:48:07 +0000546static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000547mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000548 PyObject *self;
549 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000550{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000551 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000552 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000553 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000554 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000555 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000556 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000557 if (res != 0)
558 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000559 Py_INCREF(Py_None);
560 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000561}
562
Jack Jansenf5c20571997-01-30 15:48:07 +0000563static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000564mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000565 PyObject *self;
566 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000567{
Guido van Rossum739267b1994-08-29 08:42:37 +0000568 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000569}
570
Jack Jansenf5c20571997-01-30 15:48:07 +0000571static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000572mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000573 PyObject *self;
574 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000575{
576 int fd, size;
577 char *buffer;
Jack Janseneeccca91997-05-07 15:46:31 +0000578 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000579 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000580 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000581 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000582 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000583 if (size < 0)
584 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000585 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000586}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000587
Jack Jansen46ed2761996-10-23 15:46:57 +0000588#ifdef USE_MALLOC_DEBUG
Jack Jansen8748a0b2000-08-25 22:18:20 +0000589void *mstats(char *);
590
Jack Jansenf5c20571997-01-30 15:48:07 +0000591static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000592mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000593 PyObject*self;
594 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000595{
596 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000597 Py_INCREF(Py_None);
598 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000599}
Jack Jansen4f27a551997-02-20 15:22:17 +0000600#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000601
Jack Jansenf5c20571997-01-30 15:48:07 +0000602static struct PyMethodDef mac_methods[] = {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000603 {"chdir", mac_chdir},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000604 {"close", mac_close},
Jack Jansenb6971731996-02-20 16:24:37 +0000605#ifdef WEHAVE_DUP
Guido van Rossum87f223c1994-05-06 15:54:15 +0000606 {"dup", mac_dup},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000607#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000608#ifdef WEHAVE_FDOPEN
Guido van Rossume7834441994-08-26 09:09:48 +0000609 {"fdopen", mac_fdopen},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000610#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000611#ifdef WEHAVE_FSTAT
612 {"fstat", mac_fstat},
613#endif
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000614#if TARGET_API_MAC_OS8
Guido van Rossum87f223c1994-05-06 15:54:15 +0000615 {"getbootvol", mac_getbootvol}, /* non-standard */
Jack Jansene79dc762000-06-02 21:35:07 +0000616#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000617 {"getcwd", mac_getcwd},
Guido van Rossumd1ef5961995-02-19 15:50:35 +0000618 {"listdir", mac_listdir, 0},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000619 {"lseek", mac_lseek},
Jack Jansen243b29b1996-02-21 12:33:50 +0000620 {"mkdir", mac_mkdir, 1},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000621 {"open", mac_open},
622 {"read", mac_read},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000623 {"rename", mac_rename},
624 {"rmdir", mac_rmdir},
625 {"stat", mac_stat},
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000626#if TARGET_API_MAC_OS8
Guido van Rossum222c8921995-08-08 14:10:22 +0000627 {"xstat", mac_xstat},
Jack Jansene79dc762000-06-02 21:35:07 +0000628#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +0000629 {"sync", mac_sync},
Jack Jansen243b29b1996-02-21 12:33:50 +0000630 {"remove", mac_unlink},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000631 {"unlink", mac_unlink},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000632 {"write", mac_write},
Jack Jansen46ed2761996-10-23 15:46:57 +0000633#ifdef USE_MALLOC_DEBUG
Jack Jansend50e4e11995-01-18 13:58:04 +0000634 {"mstats", mac_mstats},
635#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000636
Guido van Rossumce9739b1994-01-05 16:17:15 +0000637 {NULL, NULL} /* Sentinel */
638};
639
640
641void
642initmac()
643{
Jack Jansenf5c20571997-01-30 15:48:07 +0000644 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000645
Jack Jansenf5c20571997-01-30 15:48:07 +0000646 m = Py_InitModule("mac", mac_methods);
647 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000648
649 /* Initialize mac.error exception */
Jack Jansen55e39271997-10-07 21:47:25 +0000650 MacError = PyErr_NewException("mac.error", NULL, NULL);
651 PyDict_SetItemString(d, "error", MacError);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000652}