Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Jack Jansen | 42218ce | 1997-01-31 16:15:11 +0000 | [diff] [blame] | 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
Guido van Rossum | 9954699 | 1995-01-08 14:33:34 +0000 | [diff] [blame] | 3 | The Netherlands. |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 25 | /* Mac module implementation */ |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 26 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 27 | #include "Python.h" |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 28 | #include "ceval.h" |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <errno.h> |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 33 | |
| 34 | #ifdef THINK_C |
| 35 | #include "unix.h" |
| 36 | #undef S_IFMT |
| 37 | #undef S_IFDIR |
| 38 | #undef S_IFCHR |
| 39 | #undef S_IFBLK |
| 40 | #undef S_IFREG |
| 41 | #undef S_ISDIR |
| 42 | #undef S_ISREG |
| 43 | #endif |
| 44 | |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 45 | #include "macstat.h" |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame] | 46 | #ifdef USE_GUSI |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 47 | /* 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 Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 55 | #include <GUSI.h> |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame] | 56 | #include <sys/types.h> |
| 57 | #include <stat.h> |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame] | 58 | #else |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 59 | #define stat macstat |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame] | 60 | #endif |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 61 | |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 62 | #ifdef __MWERKS__ |
Jack Jansen | 9eeb82d | 1995-01-26 16:34:53 +0000 | [diff] [blame] | 63 | #include <unix.h> |
| 64 | #else |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 65 | #include <fcntl.h> |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 66 | #endif |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 67 | |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 68 | /* Optional routines, for some compiler/runtime combinations */ |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 69 | #if defined(USE_GUSI) || !defined(__MWERKS__) |
| 70 | #define WEHAVE_FDOPEN |
| 71 | #endif |
| 72 | #if defined(MPW) || defined(USE_GUSI) |
| 73 | #define WEHAVE_DUP |
| 74 | #endif |
Jack Jansen | 40bd770 | 1998-02-20 15:56:19 +0000 | [diff] [blame] | 75 | #if defined(USE_GUSI) |
| 76 | #define WEHAVE_FSTAT |
| 77 | #endif |
Jack Jansen | 7cbf480 | 1995-01-22 16:52:38 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 79 | #include "macdefs.h" |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame] | 80 | #ifdef USE_GUSI |
| 81 | #include <dirent.h> |
| 82 | #else |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 83 | #include "dirent.h" |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame] | 84 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 85 | |
| 86 | #ifndef MAXPATHLEN |
| 87 | #define MAXPATHLEN 1024 |
| 88 | #endif |
| 89 | |
| 90 | /* Prototypes for Unix simulation on Mac */ |
| 91 | |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 92 | #ifndef USE_GUSI |
| 93 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 94 | int chdir Py_PROTO((const char *path)); |
| 95 | int mkdir Py_PROTO((const char *path, int mode)); |
| 96 | DIR * opendir Py_PROTO((char *)); |
| 97 | void closedir Py_PROTO((DIR *)); |
| 98 | struct dirent * readdir Py_PROTO((DIR *)); |
| 99 | int rmdir Py_PROTO((const char *path)); |
| 100 | int sync Py_PROTO((void)); |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 101 | |
| 102 | #if defined(THINK_C) || defined(__SC__) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 103 | int unlink Py_PROTO((char *)); |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 104 | #else |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 105 | int unlink Py_PROTO((const char *)); |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 106 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 107 | |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 108 | #endif /* USE_GUSI */ |
| 109 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 110 | char *getwd Py_PROTO((char *)); |
| 111 | char *getbootvol Py_PROTO((void)); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 112 | |
| 113 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 114 | static PyObject *MacError; /* Exception mac.error */ |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 115 | |
| 116 | /* Set a MAC-specific error from errno, and return NULL */ |
| 117 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 118 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 119 | mac_error() |
| 120 | { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 121 | return PyErr_SetFromErrno(MacError); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* MAC generic methods */ |
| 125 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 126 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 127 | mac_1str(args, func) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 128 | PyObject *args; |
| 129 | int (*func) Py_FPROTO((const char *)); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 130 | { |
| 131 | char *path1; |
| 132 | int res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 133 | if (!PyArg_Parse(args, "s", &path1)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 134 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 135 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 136 | res = (*func)(path1); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 137 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 138 | if (res < 0) |
| 139 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 140 | Py_INCREF(Py_None); |
| 141 | return Py_None; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 144 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 145 | mac_2str(args, func) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 146 | PyObject *args; |
| 147 | int (*func) Py_FPROTO((const char *, const char *)); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 148 | { |
| 149 | char *path1, *path2; |
| 150 | int res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 151 | if (!PyArg_Parse(args, "(ss)", &path1, &path2)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 152 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 153 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 154 | res = (*func)(path1, path2); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 155 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 156 | if (res < 0) |
| 157 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 158 | Py_INCREF(Py_None); |
| 159 | return Py_None; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 162 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 163 | mac_strint(args, func) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 164 | PyObject *args; |
| 165 | int (*func) Py_FPROTO((const char *, int)); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 166 | { |
| 167 | char *path; |
| 168 | int i; |
| 169 | int res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 170 | if (!PyArg_Parse(args, "(si)", &path, &i)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 171 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 172 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 173 | res = (*func)(path, i); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 174 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 175 | if (res < 0) |
| 176 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 177 | Py_INCREF(Py_None); |
| 178 | return Py_None; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 181 | static PyObject * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 182 | mac_chdir(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 183 | PyObject *self; |
| 184 | PyObject *args; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 185 | { |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 186 | #ifdef USE_GUSI |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 187 | PyObject *rv; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 188 | |
| 189 | /* Change MacOS's idea of wd too */ |
| 190 | rv = mac_1str(args, chdir); |
| 191 | PyMac_FixGUSIcd(); |
| 192 | return rv; |
| 193 | #else |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 194 | return mac_1str(args, chdir); |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 199 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 200 | mac_close(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 201 | PyObject *self; |
| 202 | PyObject *args; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 203 | { |
| 204 | int fd, res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 205 | if (!PyArg_Parse(args, "i", &fd)) |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 206 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 207 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 208 | res = close(fd); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 209 | Py_END_ALLOW_THREADS |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 210 | #ifndef USE_GUSI |
| 211 | /* GUSI gives surious errors here? */ |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 212 | if (res < 0) |
| 213 | return mac_error(); |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 214 | #endif |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 215 | Py_INCREF(Py_None); |
| 216 | return Py_None; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 219 | #ifdef WEHAVE_DUP |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 220 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 221 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 222 | mac_dup(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 223 | PyObject *self; |
| 224 | PyObject *args; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 225 | { |
| 226 | int fd; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 227 | if (!PyArg_Parse(args, "i", &fd)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 228 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 229 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 230 | fd = dup(fd); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 231 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 232 | if (fd < 0) |
| 233 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 234 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 237 | #endif |
Guido van Rossum | e783444 | 1994-08-26 09:09:48 +0000 | [diff] [blame] | 238 | |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 239 | #ifdef WEHAVE_FDOPEN |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 240 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 241 | mac_fdopen(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 242 | PyObject *self; |
| 243 | PyObject *args; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 244 | { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 245 | extern int fclose Py_PROTO((FILE *)); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 246 | int fd; |
| 247 | char *mode; |
| 248 | FILE *fp; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 249 | if (!PyArg_Parse(args, "(is)", &fd, &mode)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 250 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 251 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 252 | fp = fdopen(fd, mode); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 253 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 254 | if (fp == NULL) |
| 255 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 256 | return PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 257 | } |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 258 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 259 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 260 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 261 | mac_getbootvol(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 262 | PyObject *self; |
| 263 | PyObject *args; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 264 | { |
| 265 | char *res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 266 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 267 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 268 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 269 | res = getbootvol(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 270 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 271 | if (res == NULL) |
| 272 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 273 | return PyString_FromString(res); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 274 | } |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 275 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 276 | static PyObject * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 277 | mac_getcwd(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 278 | PyObject *self; |
| 279 | PyObject *args; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 280 | { |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 281 | char path[MAXPATHLEN]; |
| 282 | char *res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 283 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 284 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 285 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 286 | #ifdef USE_GUSI |
| 287 | res = getcwd(path, sizeof path); |
| 288 | #else |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 289 | res = getwd(path); |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 290 | #endif |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 291 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 292 | if (res == NULL) { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 293 | PyErr_SetString(MacError, path); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 294 | return NULL; |
| 295 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 296 | return PyString_FromString(res); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 299 | static PyObject * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 300 | mac_listdir(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 301 | PyObject *self; |
| 302 | PyObject *args; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 303 | { |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 304 | char *name; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 305 | PyObject *d, *v; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 306 | DIR *dirp; |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 307 | struct dirent *ep; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 308 | if (!PyArg_Parse(args, "s", &name)) |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 309 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 310 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 311 | if ((dirp = opendir(name)) == NULL) { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 312 | Py_BLOCK_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 313 | return mac_error(); |
| 314 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 315 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 316 | closedir(dirp); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 317 | Py_BLOCK_THREADS |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 318 | return NULL; |
| 319 | } |
| 320 | while ((ep = readdir(dirp)) != NULL) { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 321 | v = PyString_FromString(ep->d_name); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 322 | if (v == NULL) { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 323 | Py_DECREF(d); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 324 | d = NULL; |
| 325 | break; |
| 326 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 327 | if (PyList_Append(d, v) != 0) { |
| 328 | Py_DECREF(v); |
| 329 | Py_DECREF(d); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 330 | d = NULL; |
| 331 | break; |
| 332 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 333 | Py_DECREF(v); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 334 | } |
| 335 | closedir(dirp); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 336 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 338 | return d; |
| 339 | } |
| 340 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 341 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 342 | mac_lseek(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 343 | PyObject *self; |
| 344 | PyObject *args; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 345 | { |
| 346 | int fd; |
| 347 | int where; |
| 348 | int how; |
| 349 | long res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 350 | if (!PyArg_Parse(args, "(iii)", &fd, &where, &how)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 351 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 352 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 353 | res = lseek(fd, (long)where, how); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 354 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 355 | if (res < 0) |
| 356 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 357 | return PyInt_FromLong(res); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 358 | } |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame] | 359 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 360 | static PyObject * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 361 | mac_mkdir(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 362 | PyObject *self; |
| 363 | PyObject *args; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 364 | { |
Jack Jansen | 243b29b | 1996-02-21 12:33:50 +0000 | [diff] [blame] | 365 | int res; |
| 366 | char *path; |
| 367 | int mode = 0777; /* Unused */ |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 368 | if (!PyArg_ParseTuple(args, "s|i", &path, &mode)) |
Jack Jansen | 243b29b | 1996-02-21 12:33:50 +0000 | [diff] [blame] | 369 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 370 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | 3ca6edd | 1996-08-06 16:06:31 +0000 | [diff] [blame] | 371 | #ifdef USE_GUSI |
| 372 | res = mkdir(path); |
| 373 | #else |
Jack Jansen | 243b29b | 1996-02-21 12:33:50 +0000 | [diff] [blame] | 374 | res = mkdir(path, mode); |
Jack Jansen | 3ca6edd | 1996-08-06 16:06:31 +0000 | [diff] [blame] | 375 | #endif |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 376 | Py_END_ALLOW_THREADS |
Jack Jansen | 243b29b | 1996-02-21 12:33:50 +0000 | [diff] [blame] | 377 | if (res < 0) |
| 378 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 379 | Py_INCREF(Py_None); |
| 380 | return Py_None; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 383 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 384 | mac_open(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 385 | PyObject *self; |
| 386 | PyObject *args; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 387 | { |
| 388 | char *path; |
| 389 | int mode; |
| 390 | int fd; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 391 | if (!PyArg_Parse(args, "(si)", &path, &mode)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 392 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 393 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 394 | fd = open(path, mode); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 395 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 396 | if (fd < 0) |
| 397 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 398 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 401 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 402 | mac_read(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 403 | PyObject *self; |
| 404 | PyObject *args; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 405 | { |
| 406 | int fd, size; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 407 | PyObject *buffer; |
| 408 | if (!PyArg_Parse(args, "(ii)", &fd, &size)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 409 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 410 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 411 | if (buffer == NULL) |
| 412 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 413 | Py_BEGIN_ALLOW_THREADS |
| 414 | size = read(fd, PyString_AsString(buffer), size); |
| 415 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 416 | if (size < 0) { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 417 | Py_DECREF(buffer); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 418 | return mac_error(); |
| 419 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 420 | _PyString_Resize(&buffer, size); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 421 | return buffer; |
| 422 | } |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 423 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 424 | static PyObject * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 425 | mac_rename(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 426 | PyObject *self; |
| 427 | PyObject *args; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 428 | { |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 429 | return mac_2str(args, rename); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 432 | static PyObject * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 433 | mac_rmdir(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 434 | PyObject *self; |
| 435 | PyObject *args; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 436 | { |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 437 | return mac_1str(args, rmdir); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 440 | static PyObject * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 441 | mac_stat(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 442 | PyObject *self; |
| 443 | PyObject *args; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 444 | { |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 445 | struct stat st; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 446 | char *path; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 447 | int res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 448 | if (!PyArg_Parse(args, "s", &path)) |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 449 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 450 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 451 | res = stat(path, &st); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 452 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 453 | if (res != 0) |
| 454 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 455 | return Py_BuildValue("(lllllllddd)", |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 456 | (long)st.st_mode, |
| 457 | (long)st.st_ino, |
| 458 | (long)st.st_dev, |
| 459 | (long)st.st_nlink, |
| 460 | (long)st.st_uid, |
| 461 | (long)st.st_gid, |
| 462 | (long)st.st_size, |
| 463 | (double)st.st_atime, |
| 464 | (double)st.st_mtime, |
| 465 | (double)st.st_ctime); |
Jack Jansen | 40bd770 | 1998-02-20 15:56:19 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | #ifdef WEHAVE_FSTAT |
| 469 | static PyObject * |
| 470 | mac_fstat(self, args) |
| 471 | PyObject *self; |
| 472 | PyObject *args; |
| 473 | { |
| 474 | struct stat st; |
| 475 | long fd; |
| 476 | int res; |
| 477 | if (!PyArg_Parse(args, "l", &fd)) |
| 478 | return NULL; |
| 479 | Py_BEGIN_ALLOW_THREADS |
| 480 | res = fstat((int)fd, &st); |
| 481 | Py_END_ALLOW_THREADS |
| 482 | if (res != 0) |
| 483 | return mac_error(); |
| 484 | return Py_BuildValue("(lllllllddd)", |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 485 | (long)st.st_mode, |
Guido van Rossum | e783444 | 1994-08-26 09:09:48 +0000 | [diff] [blame] | 486 | (long)st.st_ino, |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 487 | (long)st.st_dev, |
| 488 | (long)st.st_nlink, |
| 489 | (long)st.st_uid, |
| 490 | (long)st.st_gid, |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 491 | (long)st.st_size, |
Jack Jansen | 40bd770 | 1998-02-20 15:56:19 +0000 | [diff] [blame] | 492 | (double)st.st_atime, |
| 493 | (double)st.st_mtime, |
| 494 | (double)st.st_ctime); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 495 | } |
Jack Jansen | 40bd770 | 1998-02-20 15:56:19 +0000 | [diff] [blame] | 496 | #endif /* WEHAVE_FSTAT */ |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 497 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 498 | static PyObject * |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 499 | mac_xstat(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 500 | PyObject *self; |
| 501 | PyObject *args; |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 502 | { |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 503 | struct macstat mst; |
| 504 | struct stat st; |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 505 | char *path; |
| 506 | int res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 507 | if (!PyArg_Parse(args, "s", &path)) |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 508 | return NULL; |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 509 | /* |
| 510 | ** Convoluted: we want stat() and xstat() to agree, so we call both |
| 511 | ** stat and macstat, and use the latter only for values not provided by |
| 512 | ** the former. |
| 513 | */ |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 514 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 515 | res = macstat(path, &mst); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 516 | Py_END_ALLOW_THREADS |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 517 | if (res != 0) |
| 518 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 519 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 520 | res = stat(path, &st); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 521 | Py_END_ALLOW_THREADS |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 522 | if (res != 0) |
| 523 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 524 | return Py_BuildValue("(llllllldddls#s#)", |
Jack Jansen | c743c8d | 1996-02-14 16:02:30 +0000 | [diff] [blame] | 525 | (long)st.st_mode, |
| 526 | (long)st.st_ino, |
| 527 | (long)st.st_dev, |
| 528 | (long)st.st_nlink, |
| 529 | (long)st.st_uid, |
| 530 | (long)st.st_gid, |
| 531 | (long)st.st_size, |
Jack Jansen | 59b912a | 1996-10-15 16:13:33 +0000 | [diff] [blame] | 532 | (double)st.st_atime, |
| 533 | (double)st.st_mtime, |
| 534 | (double)st.st_ctime, |
| 535 | (long)mst.st_rsize, |
| 536 | mst.st_creator, 4, |
| 537 | mst.st_type, 4); |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 540 | static PyObject * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 541 | mac_sync(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 542 | PyObject *self; |
| 543 | PyObject *args; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 544 | { |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 545 | int res; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 546 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 547 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 548 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 549 | res = sync(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 550 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 551 | if (res != 0) |
| 552 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 553 | Py_INCREF(Py_None); |
| 554 | return Py_None; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 557 | static PyObject * |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 558 | mac_unlink(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 559 | PyObject *self; |
| 560 | PyObject *args; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 561 | { |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 562 | return mac_1str(args, (int (*)(const char *))unlink); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 565 | static PyObject * |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 566 | mac_write(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 567 | PyObject *self; |
| 568 | PyObject *args; |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 569 | { |
| 570 | int fd, size; |
| 571 | char *buffer; |
Jack Jansen | eeccca9 | 1997-05-07 15:46:31 +0000 | [diff] [blame] | 572 | if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size)) |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 573 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 574 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 575 | size = write(fd, buffer, size); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 576 | Py_END_ALLOW_THREADS |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 577 | if (size < 0) |
| 578 | return mac_error(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 579 | return PyInt_FromLong((long)size); |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 580 | } |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 581 | |
Jack Jansen | 46ed276 | 1996-10-23 15:46:57 +0000 | [diff] [blame] | 582 | #ifdef USE_MALLOC_DEBUG |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 583 | static PyObject * |
Jack Jansen | d50e4e1 | 1995-01-18 13:58:04 +0000 | [diff] [blame] | 584 | mac_mstats(self, args) |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 585 | PyObject*self; |
| 586 | PyObject *args; |
Jack Jansen | d50e4e1 | 1995-01-18 13:58:04 +0000 | [diff] [blame] | 587 | { |
| 588 | mstats("python"); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 589 | Py_INCREF(Py_None); |
| 590 | return Py_None; |
Jack Jansen | d50e4e1 | 1995-01-18 13:58:04 +0000 | [diff] [blame] | 591 | } |
Jack Jansen | 4f27a55 | 1997-02-20 15:22:17 +0000 | [diff] [blame] | 592 | #endif /* USE_MALLOC_DEBUG */ |
Jack Jansen | d50e4e1 | 1995-01-18 13:58:04 +0000 | [diff] [blame] | 593 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 594 | static struct PyMethodDef mac_methods[] = { |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 595 | {"chdir", mac_chdir}, |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 596 | {"close", mac_close}, |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 597 | #ifdef WEHAVE_DUP |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 598 | {"dup", mac_dup}, |
Guido van Rossum | 921a08f | 1994-05-06 15:56:22 +0000 | [diff] [blame] | 599 | #endif |
Jack Jansen | b697173 | 1996-02-20 16:24:37 +0000 | [diff] [blame] | 600 | #ifdef WEHAVE_FDOPEN |
Guido van Rossum | e783444 | 1994-08-26 09:09:48 +0000 | [diff] [blame] | 601 | {"fdopen", mac_fdopen}, |
Jack Jansen | 0c097ea | 1994-12-14 13:48:38 +0000 | [diff] [blame] | 602 | #endif |
Jack Jansen | 40bd770 | 1998-02-20 15:56:19 +0000 | [diff] [blame] | 603 | #ifdef WEHAVE_FSTAT |
| 604 | {"fstat", mac_fstat}, |
| 605 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 606 | {"getbootvol", mac_getbootvol}, /* non-standard */ |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 607 | {"getcwd", mac_getcwd}, |
Guido van Rossum | d1ef596 | 1995-02-19 15:50:35 +0000 | [diff] [blame] | 608 | {"listdir", mac_listdir, 0}, |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 609 | {"lseek", mac_lseek}, |
Jack Jansen | 243b29b | 1996-02-21 12:33:50 +0000 | [diff] [blame] | 610 | {"mkdir", mac_mkdir, 1}, |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 611 | {"open", mac_open}, |
| 612 | {"read", mac_read}, |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 613 | {"rename", mac_rename}, |
| 614 | {"rmdir", mac_rmdir}, |
| 615 | {"stat", mac_stat}, |
Guido van Rossum | 222c892 | 1995-08-08 14:10:22 +0000 | [diff] [blame] | 616 | {"xstat", mac_xstat}, |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 617 | {"sync", mac_sync}, |
Jack Jansen | 243b29b | 1996-02-21 12:33:50 +0000 | [diff] [blame] | 618 | {"remove", mac_unlink}, |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 619 | {"unlink", mac_unlink}, |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 620 | {"write", mac_write}, |
Jack Jansen | 46ed276 | 1996-10-23 15:46:57 +0000 | [diff] [blame] | 621 | #ifdef USE_MALLOC_DEBUG |
Jack Jansen | d50e4e1 | 1995-01-18 13:58:04 +0000 | [diff] [blame] | 622 | {"mstats", mac_mstats}, |
| 623 | #endif |
Guido van Rossum | 87f223c | 1994-05-06 15:54:15 +0000 | [diff] [blame] | 624 | |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 625 | {NULL, NULL} /* Sentinel */ |
| 626 | }; |
| 627 | |
| 628 | |
| 629 | void |
| 630 | initmac() |
| 631 | { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 632 | PyObject *m, *d; |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 633 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 634 | m = Py_InitModule("mac", mac_methods); |
| 635 | d = PyModule_GetDict(m); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 636 | |
| 637 | /* Initialize mac.error exception */ |
Jack Jansen | 55e3927 | 1997-10-07 21:47:25 +0000 | [diff] [blame] | 638 | MacError = PyErr_NewException("mac.error", NULL, NULL); |
| 639 | PyDict_SetItemString(d, "error", MacError); |
Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 640 | } |