blob: c2224f00bd80980d0bb4e5fa53639365e9b6dbe2 [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 Jansen59b912a1996-10-15 16:13:33 +000045#include "macstat.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000046#ifdef USE_GUSI
Jack Jansen59b912a1996-10-15 16:13:33 +000047/* Remove defines from macstat.h */
48#undef S_IFMT
49#undef S_IFDIR
50#undef S_IFREG
51#undef S_IREAD
52#undef S_IWRITE
53#undef S_IEXEC
54
Jack Jansen37d21e12000-04-07 09:25:06 +000055#ifdef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +000056#include <GUSI.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000057#endif /* USE_GUSI1 */
Jack Jansenc743c8d1996-02-14 16:02:30 +000058#include <sys/types.h>
Jack Jansen37d21e12000-04-07 09:25:06 +000059#include <sys/stat.h>
60#else /* USE_GUSI */
Jack Jansen59b912a1996-10-15 16:13:33 +000061#define stat macstat
Jack Jansen37d21e12000-04-07 09:25:06 +000062#endif /* USE_GUSI */
Guido van Rossumce9739b1994-01-05 16:17:15 +000063
Jack Jansen37d21e12000-04-07 09:25:06 +000064#ifdef USE_GUSI2
65#define sync bad_sync
66#include <unistd.h>
67#include <fcntl.h>
68#undef sync
69int sync(void);
70#else
Jack Jansen12e89e42000-05-12 21:36:29 +000071#define mode_t int
Guido van Rossum87f223c1994-05-06 15:54:15 +000072#include <fcntl.h>
Jack Jansen0c097ea1994-12-14 13:48:38 +000073#endif
Guido van Rossumce9739b1994-01-05 16:17:15 +000074
Jack Jansenb6971731996-02-20 16:24:37 +000075/* Optional routines, for some compiler/runtime combinations */
Jack Jansenb6971731996-02-20 16:24:37 +000076#if defined(USE_GUSI) || !defined(__MWERKS__)
77#define WEHAVE_FDOPEN
78#endif
79#if defined(MPW) || defined(USE_GUSI)
80#define WEHAVE_DUP
81#endif
Jack Jansen40bd7701998-02-20 15:56:19 +000082#if defined(USE_GUSI)
83#define WEHAVE_FSTAT
84#endif
Jack Jansen7cbf4801995-01-22 16:52:38 +000085
Guido van Rossumd4d77281994-08-19 10:51:31 +000086#include "macdefs.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000087#ifdef USE_GUSI
88#include <dirent.h>
89#else
Guido van Rossumd4d77281994-08-19 10:51:31 +000090#include "dirent.h"
Jack Jansenc743c8d1996-02-14 16:02:30 +000091#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +000092
93#ifndef MAXPATHLEN
94#define MAXPATHLEN 1024
95#endif
96
97/* Prototypes for Unix simulation on Mac */
98
Jack Jansenb6971731996-02-20 16:24:37 +000099#ifndef USE_GUSI
100
Jack Jansenf5c20571997-01-30 15:48:07 +0000101int chdir Py_PROTO((const char *path));
102int mkdir Py_PROTO((const char *path, int mode));
103DIR * opendir Py_PROTO((char *));
104void closedir Py_PROTO((DIR *));
105struct dirent * readdir Py_PROTO((DIR *));
106int rmdir Py_PROTO((const char *path));
107int sync Py_PROTO((void));
Jack Jansenb6971731996-02-20 16:24:37 +0000108
109#if defined(THINK_C) || defined(__SC__)
Jack Jansenf5c20571997-01-30 15:48:07 +0000110int unlink Py_PROTO((char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000111#else
Jack Jansenf5c20571997-01-30 15:48:07 +0000112int unlink Py_PROTO((const char *));
Guido van Rossum739267b1994-08-29 08:42:37 +0000113#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000114
Jack Jansenb6971731996-02-20 16:24:37 +0000115#endif /* USE_GUSI */
116
Jack Jansenf5c20571997-01-30 15:48:07 +0000117char *getwd Py_PROTO((char *));
118char *getbootvol Py_PROTO((void));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000119
120
Jack Jansenf5c20571997-01-30 15:48:07 +0000121static PyObject *MacError; /* Exception mac.error */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000122
123/* Set a MAC-specific error from errno, and return NULL */
124
Jack Jansenf5c20571997-01-30 15:48:07 +0000125static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000126mac_error()
127{
Jack Jansenf5c20571997-01-30 15:48:07 +0000128 return PyErr_SetFromErrno(MacError);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000129}
130
131/* MAC generic methods */
132
Jack Jansenf5c20571997-01-30 15:48:07 +0000133static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000134mac_1str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000135 PyObject *args;
136 int (*func) Py_FPROTO((const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000137{
138 char *path1;
139 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000140 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000141 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000142 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000143 res = (*func)(path1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000144 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000145 if (res < 0)
146 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000147 Py_INCREF(Py_None);
148 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000149}
150
Jack Jansenf5c20571997-01-30 15:48:07 +0000151static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000152mac_2str(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000153 PyObject *args;
154 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000155{
156 char *path1, *path2;
157 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000158 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000159 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000160 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000161 res = (*func)(path1, path2);
Jack Jansenf5c20571997-01-30 15:48:07 +0000162 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000163 if (res < 0)
164 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000165 Py_INCREF(Py_None);
166 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000167}
168
Jack Jansenf5c20571997-01-30 15:48:07 +0000169static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000170mac_strint(args, func)
Jack Jansenf5c20571997-01-30 15:48:07 +0000171 PyObject *args;
172 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000173{
174 char *path;
175 int i;
176 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000177 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000178 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000179 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000180 res = (*func)(path, i);
Jack Jansenf5c20571997-01-30 15:48:07 +0000181 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000182 if (res < 0)
183 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000184 Py_INCREF(Py_None);
185 return Py_None;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000186}
187
Jack Jansenf5c20571997-01-30 15:48:07 +0000188static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000189mac_chdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000190 PyObject *self;
191 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000192{
Jack Jansen37d21e12000-04-07 09:25:06 +0000193#ifdef USE_GUSI1
Jack Jansenf5c20571997-01-30 15:48:07 +0000194 PyObject *rv;
Jack Jansen378815c1996-03-06 16:21:34 +0000195
196 /* Change MacOS's idea of wd too */
197 rv = mac_1str(args, chdir);
198 PyMac_FixGUSIcd();
199 return rv;
200#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000201 return mac_1str(args, chdir);
Jack Jansen378815c1996-03-06 16:21:34 +0000202#endif
203
Guido van Rossum87f223c1994-05-06 15:54:15 +0000204}
205
Jack Jansenf5c20571997-01-30 15:48:07 +0000206static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000207mac_close(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000208 PyObject *self;
209 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000210{
211 int fd, res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000212 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000213 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000214 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000215 res = close(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000216 Py_END_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000217#ifndef USE_GUSI1
Jack Jansenb6971731996-02-20 16:24:37 +0000218 /* GUSI gives surious errors here? */
Guido van Rossum87f223c1994-05-06 15:54:15 +0000219 if (res < 0)
220 return mac_error();
Jack Jansenb6971731996-02-20 16:24:37 +0000221#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000222 Py_INCREF(Py_None);
223 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000224}
225
Jack Jansenb6971731996-02-20 16:24:37 +0000226#ifdef WEHAVE_DUP
Guido van Rossum921a08f1994-05-06 15:56:22 +0000227
Jack Jansenf5c20571997-01-30 15:48:07 +0000228static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000229mac_dup(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000230 PyObject *self;
231 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000232{
233 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000234 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000235 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000236 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000237 fd = dup(fd);
Jack Jansenf5c20571997-01-30 15:48:07 +0000238 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000239 if (fd < 0)
240 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000241 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000242}
243
Jack Jansenb6971731996-02-20 16:24:37 +0000244#endif
Guido van Rossume7834441994-08-26 09:09:48 +0000245
Jack Jansenb6971731996-02-20 16:24:37 +0000246#ifdef WEHAVE_FDOPEN
Jack Jansenf5c20571997-01-30 15:48:07 +0000247static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000248mac_fdopen(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000249 PyObject *self;
250 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000251{
Jack Jansenf5c20571997-01-30 15:48:07 +0000252 extern int fclose Py_PROTO((FILE *));
Guido van Rossum87f223c1994-05-06 15:54:15 +0000253 int fd;
254 char *mode;
255 FILE *fp;
Jack Jansenf5c20571997-01-30 15:48:07 +0000256 if (!PyArg_Parse(args, "(is)", &fd, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000257 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000258 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000259 fp = fdopen(fd, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000260 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000261 if (fp == NULL)
262 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000263 return PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000264}
Jack Jansen0c097ea1994-12-14 13:48:38 +0000265#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000266
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}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000282
Jack Jansenf5c20571997-01-30 15:48:07 +0000283static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000284mac_getcwd(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000285 PyObject *self;
286 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000287{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000288 char path[MAXPATHLEN];
289 char *res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000290 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000291 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000292 Py_BEGIN_ALLOW_THREADS
Jack Jansenb6971731996-02-20 16:24:37 +0000293#ifdef USE_GUSI
294 res = getcwd(path, sizeof path);
295#else
Guido van Rossum87f223c1994-05-06 15:54:15 +0000296 res = getwd(path);
Jack Jansenb6971731996-02-20 16:24:37 +0000297#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000298 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000299 if (res == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000300 PyErr_SetString(MacError, path);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000301 return NULL;
302 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000303 return PyString_FromString(res);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000304}
305
Jack Jansenf5c20571997-01-30 15:48:07 +0000306static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000307mac_listdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000308 PyObject *self;
309 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000310{
Guido van Rossumce9739b1994-01-05 16:17:15 +0000311 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000312 PyObject *d, *v;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000313 DIR *dirp;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000314 struct dirent *ep;
Jack Jansenf5c20571997-01-30 15:48:07 +0000315 if (!PyArg_Parse(args, "s", &name))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000316 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000317 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000318 if ((dirp = opendir(name)) == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000319 Py_BLOCK_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000320 return mac_error();
321 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000322 if ((d = PyList_New(0)) == NULL) {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000323 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000324 Py_BLOCK_THREADS
Guido van Rossumce9739b1994-01-05 16:17:15 +0000325 return NULL;
326 }
327 while ((ep = readdir(dirp)) != NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000328 v = PyString_FromString(ep->d_name);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000329 if (v == NULL) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000330 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000331 d = NULL;
332 break;
333 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000334 if (PyList_Append(d, v) != 0) {
335 Py_DECREF(v);
336 Py_DECREF(d);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000337 d = NULL;
338 break;
339 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 Py_DECREF(v);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000341 }
342 closedir(dirp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000343 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000344
Guido van Rossumce9739b1994-01-05 16:17:15 +0000345 return d;
346}
347
Jack Jansenf5c20571997-01-30 15:48:07 +0000348static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000349mac_lseek(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000350 PyObject *self;
351 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000352{
353 int fd;
354 int where;
355 int how;
356 long res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000357 if (!PyArg_Parse(args, "(iii)", &fd, &where, &how))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000358 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000359 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000360 res = lseek(fd, (long)where, how);
Jack Jansenf5c20571997-01-30 15:48:07 +0000361 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000362 if (res < 0)
363 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000364 return PyInt_FromLong(res);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000365}
Jack Jansenc743c8d1996-02-14 16:02:30 +0000366
Jack Jansenf5c20571997-01-30 15:48:07 +0000367static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000368mac_mkdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000369 PyObject *self;
370 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000371{
Jack Jansen243b29b1996-02-21 12:33:50 +0000372 int res;
373 char *path;
374 int mode = 0777; /* Unused */
Jack Jansenf5c20571997-01-30 15:48:07 +0000375 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Jack Jansen243b29b1996-02-21 12:33:50 +0000376 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000377 Py_BEGIN_ALLOW_THREADS
Jack Jansen37d21e12000-04-07 09:25:06 +0000378#ifdef USE_GUSI1
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000379 res = mkdir(path);
380#else
Jack Jansen243b29b1996-02-21 12:33:50 +0000381 res = mkdir(path, mode);
Jack Jansen3ca6edd1996-08-06 16:06:31 +0000382#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000383 Py_END_ALLOW_THREADS
Jack Jansen243b29b1996-02-21 12:33:50 +0000384 if (res < 0)
385 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000386 Py_INCREF(Py_None);
387 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000388}
389
Jack Jansenf5c20571997-01-30 15:48:07 +0000390static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000391mac_open(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000392 PyObject *self;
393 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000394{
395 char *path;
396 int mode;
397 int fd;
Jack Jansenf5c20571997-01-30 15:48:07 +0000398 if (!PyArg_Parse(args, "(si)", &path, &mode))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000399 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000400 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000401 fd = open(path, mode);
Jack Jansenf5c20571997-01-30 15:48:07 +0000402 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000403 if (fd < 0)
404 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000405 return PyInt_FromLong((long)fd);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000406}
407
Jack Jansenf5c20571997-01-30 15:48:07 +0000408static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000409mac_read(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000410 PyObject *self;
411 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000412{
413 int fd, size;
Jack Jansenf5c20571997-01-30 15:48:07 +0000414 PyObject *buffer;
415 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000416 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000418 if (buffer == NULL)
419 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000420 Py_BEGIN_ALLOW_THREADS
421 size = read(fd, PyString_AsString(buffer), size);
422 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000423 if (size < 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000424 Py_DECREF(buffer);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000425 return mac_error();
426 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000427 _PyString_Resize(&buffer, size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000428 return buffer;
429}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000430
Jack Jansenf5c20571997-01-30 15:48:07 +0000431static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000432mac_rename(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000433 PyObject *self;
434 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000435{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000436 return mac_2str(args, rename);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000437}
438
Jack Jansenf5c20571997-01-30 15:48:07 +0000439static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000440mac_rmdir(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000441 PyObject *self;
442 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000443{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000444 return mac_1str(args, rmdir);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000445}
446
Jack Jansenf5c20571997-01-30 15:48:07 +0000447static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000448mac_stat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000449 PyObject *self;
450 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000451{
Jack Jansen59b912a1996-10-15 16:13:33 +0000452 struct stat st;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000453 char *path;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000454 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000455 if (!PyArg_Parse(args, "s", &path))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000456 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000457 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000458 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000459 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000460 if (res != 0)
461 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000462 return Py_BuildValue("(lllllllddd)",
Jack Jansen59b912a1996-10-15 16:13:33 +0000463 (long)st.st_mode,
464 (long)st.st_ino,
465 (long)st.st_dev,
466 (long)st.st_nlink,
467 (long)st.st_uid,
468 (long)st.st_gid,
469 (long)st.st_size,
470 (double)st.st_atime,
471 (double)st.st_mtime,
472 (double)st.st_ctime);
Jack Jansen40bd7701998-02-20 15:56:19 +0000473}
474
475#ifdef WEHAVE_FSTAT
476static PyObject *
477mac_fstat(self, args)
478 PyObject *self;
479 PyObject *args;
480{
481 struct stat st;
482 long fd;
483 int res;
484 if (!PyArg_Parse(args, "l", &fd))
485 return NULL;
486 Py_BEGIN_ALLOW_THREADS
487 res = fstat((int)fd, &st);
488 Py_END_ALLOW_THREADS
489 if (res != 0)
490 return mac_error();
491 return Py_BuildValue("(lllllllddd)",
Guido van Rossum87f223c1994-05-06 15:54:15 +0000492 (long)st.st_mode,
Guido van Rossume7834441994-08-26 09:09:48 +0000493 (long)st.st_ino,
Guido van Rossumd4d77281994-08-19 10:51:31 +0000494 (long)st.st_dev,
495 (long)st.st_nlink,
496 (long)st.st_uid,
497 (long)st.st_gid,
Guido van Rossum87f223c1994-05-06 15:54:15 +0000498 (long)st.st_size,
Jack Jansen40bd7701998-02-20 15:56:19 +0000499 (double)st.st_atime,
500 (double)st.st_mtime,
501 (double)st.st_ctime);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000502}
Jack Jansen40bd7701998-02-20 15:56:19 +0000503#endif /* WEHAVE_FSTAT */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000504
Jack Jansenf5c20571997-01-30 15:48:07 +0000505static PyObject *
Guido van Rossum222c8921995-08-08 14:10:22 +0000506mac_xstat(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000507 PyObject *self;
508 PyObject *args;
Guido van Rossum222c8921995-08-08 14:10:22 +0000509{
Jack Jansen59b912a1996-10-15 16:13:33 +0000510 struct macstat mst;
511 struct stat st;
Guido van Rossum222c8921995-08-08 14:10:22 +0000512 char *path;
513 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000514 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum222c8921995-08-08 14:10:22 +0000515 return NULL;
Jack Jansen59b912a1996-10-15 16:13:33 +0000516 /*
517 ** Convoluted: we want stat() and xstat() to agree, so we call both
518 ** stat and macstat, and use the latter only for values not provided by
519 ** the former.
520 */
Jack Jansenf5c20571997-01-30 15:48:07 +0000521 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000522 res = macstat(path, &mst);
Jack Jansenf5c20571997-01-30 15:48:07 +0000523 Py_END_ALLOW_THREADS
Guido van Rossum222c8921995-08-08 14:10:22 +0000524 if (res != 0)
525 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000526 Py_BEGIN_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000527 res = stat(path, &st);
Jack Jansenf5c20571997-01-30 15:48:07 +0000528 Py_END_ALLOW_THREADS
Jack Jansen59b912a1996-10-15 16:13:33 +0000529 if (res != 0)
530 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000531 return Py_BuildValue("(llllllldddls#s#)",
Jack Jansenc743c8d1996-02-14 16:02:30 +0000532 (long)st.st_mode,
533 (long)st.st_ino,
534 (long)st.st_dev,
535 (long)st.st_nlink,
536 (long)st.st_uid,
537 (long)st.st_gid,
538 (long)st.st_size,
Jack Jansen59b912a1996-10-15 16:13:33 +0000539 (double)st.st_atime,
540 (double)st.st_mtime,
541 (double)st.st_ctime,
542 (long)mst.st_rsize,
543 mst.st_creator, 4,
544 mst.st_type, 4);
Guido van Rossum222c8921995-08-08 14:10:22 +0000545}
546
Jack Jansenf5c20571997-01-30 15:48:07 +0000547static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000548mac_sync(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000549 PyObject *self;
550 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000551{
Guido van Rossum87f223c1994-05-06 15:54:15 +0000552 int res;
Jack Jansenf5c20571997-01-30 15:48:07 +0000553 if (!PyArg_NoArgs(args))
Guido van Rossumce9739b1994-01-05 16:17:15 +0000554 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000555 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000556 res = sync();
Jack Jansenf5c20571997-01-30 15:48:07 +0000557 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000558 if (res != 0)
559 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000560 Py_INCREF(Py_None);
561 return Py_None;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000562}
563
Jack Jansenf5c20571997-01-30 15:48:07 +0000564static PyObject *
Guido van Rossumce9739b1994-01-05 16:17:15 +0000565mac_unlink(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000566 PyObject *self;
567 PyObject *args;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000568{
Guido van Rossum739267b1994-08-29 08:42:37 +0000569 return mac_1str(args, (int (*)(const char *))unlink);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000570}
571
Jack Jansenf5c20571997-01-30 15:48:07 +0000572static PyObject *
Guido van Rossum87f223c1994-05-06 15:54:15 +0000573mac_write(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000574 PyObject *self;
575 PyObject *args;
Guido van Rossum87f223c1994-05-06 15:54:15 +0000576{
577 int fd, size;
578 char *buffer;
Jack Janseneeccca91997-05-07 15:46:31 +0000579 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum87f223c1994-05-06 15:54:15 +0000580 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000581 Py_BEGIN_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000582 size = write(fd, buffer, size);
Jack Jansenf5c20571997-01-30 15:48:07 +0000583 Py_END_ALLOW_THREADS
Guido van Rossum87f223c1994-05-06 15:54:15 +0000584 if (size < 0)
585 return mac_error();
Jack Jansenf5c20571997-01-30 15:48:07 +0000586 return PyInt_FromLong((long)size);
Guido van Rossum87f223c1994-05-06 15:54:15 +0000587}
Guido van Rossumce9739b1994-01-05 16:17:15 +0000588
Jack Jansen46ed2761996-10-23 15:46:57 +0000589#ifdef USE_MALLOC_DEBUG
Jack Jansenf5c20571997-01-30 15:48:07 +0000590static PyObject *
Jack Jansend50e4e11995-01-18 13:58:04 +0000591mac_mstats(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000592 PyObject*self;
593 PyObject *args;
Jack Jansend50e4e11995-01-18 13:58:04 +0000594{
595 mstats("python");
Jack Jansenf5c20571997-01-30 15:48:07 +0000596 Py_INCREF(Py_None);
597 return Py_None;
Jack Jansend50e4e11995-01-18 13:58:04 +0000598}
Jack Jansen4f27a551997-02-20 15:22:17 +0000599#endif /* USE_MALLOC_DEBUG */
Jack Jansend50e4e11995-01-18 13:58:04 +0000600
Jack Jansenf5c20571997-01-30 15:48:07 +0000601static struct PyMethodDef mac_methods[] = {
Guido van Rossumce9739b1994-01-05 16:17:15 +0000602 {"chdir", mac_chdir},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000603 {"close", mac_close},
Jack Jansenb6971731996-02-20 16:24:37 +0000604#ifdef WEHAVE_DUP
Guido van Rossum87f223c1994-05-06 15:54:15 +0000605 {"dup", mac_dup},
Guido van Rossum921a08f1994-05-06 15:56:22 +0000606#endif
Jack Jansenb6971731996-02-20 16:24:37 +0000607#ifdef WEHAVE_FDOPEN
Guido van Rossume7834441994-08-26 09:09:48 +0000608 {"fdopen", mac_fdopen},
Jack Jansen0c097ea1994-12-14 13:48:38 +0000609#endif
Jack Jansen40bd7701998-02-20 15:56:19 +0000610#ifdef WEHAVE_FSTAT
611 {"fstat", mac_fstat},
612#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000613 {"getbootvol", mac_getbootvol}, /* non-standard */
Guido van Rossumce9739b1994-01-05 16:17:15 +0000614 {"getcwd", mac_getcwd},
Guido van Rossumd1ef5961995-02-19 15:50:35 +0000615 {"listdir", mac_listdir, 0},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000616 {"lseek", mac_lseek},
Jack Jansen243b29b1996-02-21 12:33:50 +0000617 {"mkdir", mac_mkdir, 1},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000618 {"open", mac_open},
619 {"read", mac_read},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000620 {"rename", mac_rename},
621 {"rmdir", mac_rmdir},
622 {"stat", mac_stat},
Guido van Rossum222c8921995-08-08 14:10:22 +0000623 {"xstat", mac_xstat},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000624 {"sync", mac_sync},
Jack Jansen243b29b1996-02-21 12:33:50 +0000625 {"remove", mac_unlink},
Guido van Rossumce9739b1994-01-05 16:17:15 +0000626 {"unlink", mac_unlink},
Guido van Rossum87f223c1994-05-06 15:54:15 +0000627 {"write", mac_write},
Jack Jansen46ed2761996-10-23 15:46:57 +0000628#ifdef USE_MALLOC_DEBUG
Jack Jansend50e4e11995-01-18 13:58:04 +0000629 {"mstats", mac_mstats},
630#endif
Guido van Rossum87f223c1994-05-06 15:54:15 +0000631
Guido van Rossumce9739b1994-01-05 16:17:15 +0000632 {NULL, NULL} /* Sentinel */
633};
634
635
636void
637initmac()
638{
Jack Jansenf5c20571997-01-30 15:48:07 +0000639 PyObject *m, *d;
Guido van Rossumce9739b1994-01-05 16:17:15 +0000640
Jack Jansenf5c20571997-01-30 15:48:07 +0000641 m = Py_InitModule("mac", mac_methods);
642 d = PyModule_GetDict(m);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000643
644 /* Initialize mac.error exception */
Jack Jansen55e39271997-10-07 21:47:25 +0000645 MacError = PyErr_NewException("mac.error", NULL, NULL);
646 PyDict_SetItemString(d, "error", MacError);
Guido van Rossumce9739b1994-01-05 16:17:15 +0000647}