Guido van Rossum | ce9739b | 1994-01-05 16:17:15 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991, 1992, 1993, 1004 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
| 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 | |
| 25 | /* Macintosh OS module implementation */ |
| 26 | |
| 27 | #include "allobjects.h" |
| 28 | |
| 29 | #include "import.h" |
| 30 | #include "modsupport.h" |
| 31 | |
| 32 | #include "::unixemu:dir.h" |
| 33 | #include "::unixemu:stat.h" |
| 34 | |
| 35 | static object *MacError; /* Exception */ |
| 36 | |
| 37 | |
| 38 | static object * |
| 39 | mac_chdir(self, args) |
| 40 | object *self; |
| 41 | object *args; |
| 42 | { |
| 43 | char *path; |
| 44 | if (!getstrarg(args, &path)) |
| 45 | return NULL; |
| 46 | if (chdir(path) != 0) |
| 47 | return err_errno(MacError); |
| 48 | INCREF(None); |
| 49 | return None; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | static object * |
| 54 | mac_getcwd(self, args) |
| 55 | object *self; |
| 56 | object *args; |
| 57 | { |
| 58 | extern char *getwd(); |
| 59 | char buf[1025]; |
| 60 | if (!getnoarg(args)) |
| 61 | return NULL; |
| 62 | strcpy(buf, "mac.getcwd() failed"); /* In case getwd() doesn't set a msg */ |
| 63 | if (getwd(buf) == NULL) { |
| 64 | err_setstr(MacError, buf); |
| 65 | return NULL; |
| 66 | } |
| 67 | return newstringobject(buf); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | static object * |
| 72 | mac_listdir(self, args) |
| 73 | object *self; |
| 74 | object *args; |
| 75 | { |
| 76 | object *d, *v; |
| 77 | char *name; |
| 78 | DIR *dirp; |
| 79 | struct direct *ep; |
| 80 | if (!getstrarg(args, &name)) |
| 81 | return NULL; |
| 82 | if ((dirp = opendir(name)) == NULL) |
| 83 | return err_errno(MacError); |
| 84 | if ((d = newlistobject(0)) == NULL) { |
| 85 | closedir(dirp); |
| 86 | return NULL; |
| 87 | } |
| 88 | while ((ep = readdir(dirp)) != NULL) { |
| 89 | v = newstringobject(ep->d_name); |
| 90 | if (v == NULL) { |
| 91 | DECREF(d); |
| 92 | d = NULL; |
| 93 | break; |
| 94 | } |
| 95 | if (addlistitem(d, v) != 0) { |
| 96 | DECREF(v); |
| 97 | DECREF(d); |
| 98 | d = NULL; |
| 99 | break; |
| 100 | } |
| 101 | DECREF(v); |
| 102 | } |
| 103 | closedir(dirp); |
| 104 | return d; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static object * |
| 109 | mac_mkdir(self, args) |
| 110 | object *self; |
| 111 | object *args; |
| 112 | { |
| 113 | char *path; |
| 114 | int mode; |
| 115 | if (!getargs(args, "(si)", &path, &mode)) |
| 116 | return NULL; |
| 117 | if (mkdir(path, mode) != 0) |
| 118 | return err_errno(MacError); |
| 119 | INCREF(None); |
| 120 | return None; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static object * |
| 125 | mac_rename(self, args) |
| 126 | object *self; |
| 127 | object *args; |
| 128 | { |
| 129 | char *src, *dst; |
| 130 | if (!getargs(args, "(ss)", &src, &dst)) |
| 131 | return NULL; |
| 132 | if (rename(src, dst) != 0) |
| 133 | return err_errno(MacError); |
| 134 | INCREF(None); |
| 135 | return None; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | static object * |
| 140 | mac_rmdir(self, args) |
| 141 | object *self; |
| 142 | object *args; |
| 143 | { |
| 144 | char *path; |
| 145 | if (!getstrarg(args, &path)) |
| 146 | return NULL; |
| 147 | if (rmdir(path) != 0) |
| 148 | return err_errno(MacError); |
| 149 | INCREF(None); |
| 150 | return None; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | static object * |
| 155 | mac_stat(self, args) |
| 156 | object *self; |
| 157 | object *args; |
| 158 | { |
| 159 | struct stat st; |
| 160 | char *path; |
| 161 | object *v; |
| 162 | if (!getstrarg(args, &path)) |
| 163 | return NULL; |
| 164 | if (stat(path, &st) != 0) |
| 165 | return err_errno(MacError); |
| 166 | v = newtupleobject(11); |
| 167 | if (v == NULL) |
| 168 | return NULL; |
| 169 | #define SET(i, val) settupleitem(v, i, newintobject((long)(val))) |
| 170 | #define UNSET(i, val) SET(i, 0) /* For values my Mac stat doesn't support */ |
| 171 | SET(0, st.st_mode); |
| 172 | UNSET(1, st.st_ino); |
| 173 | UNSET(2, st.st_dev); |
| 174 | UNSET(3, st.st_nlink); |
| 175 | UNSET(4, st.st_uid); |
| 176 | UNSET(5, st.st_gid); |
| 177 | SET(6, st.st_size); |
| 178 | UNSET(7, st.st_atime); |
| 179 | SET(8, st.st_mtime); |
| 180 | UNSET(9, st.st_ctime); |
| 181 | SET(10, st.st_rsize); /* Mac-specific: resource size */ |
| 182 | /* XXX Check that unixemu:stat.c defines this! */ |
| 183 | #undef SET |
| 184 | if (err_occurred()) { |
| 185 | DECREF(v); |
| 186 | return NULL; |
| 187 | } |
| 188 | return v; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | static object * |
| 193 | mac_sync(self, args) |
| 194 | object *self; |
| 195 | object *args; |
| 196 | { |
| 197 | if (!getnoarg(args)) |
| 198 | return NULL; |
| 199 | sync(); |
| 200 | INCREF(None); |
| 201 | return None; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | static object * |
| 206 | mac_unlink(self, args) |
| 207 | object *self; |
| 208 | object *args; |
| 209 | { |
| 210 | char *path; |
| 211 | if (!getstrarg(args, &path)) |
| 212 | return NULL; |
| 213 | if (unlink(path) != 0) |
| 214 | return err_errno(MacError); |
| 215 | INCREF(None); |
| 216 | return None; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | static struct methodlist mac_methods[] = { |
| 221 | {"chdir", mac_chdir}, |
| 222 | {"getcwd", mac_getcwd}, |
| 223 | {"listdir", mac_listdir}, |
| 224 | {"mkdir", mac_mkdir}, |
| 225 | {"rename", mac_rename}, |
| 226 | {"rmdir", mac_rmdir}, |
| 227 | {"stat", mac_stat}, |
| 228 | {"sync", mac_sync}, |
| 229 | {"unlink", mac_unlink}, |
| 230 | {NULL, NULL} /* Sentinel */ |
| 231 | }; |
| 232 | |
| 233 | |
| 234 | void |
| 235 | initmac() |
| 236 | { |
| 237 | object *m, *d; |
| 238 | |
| 239 | m = initmodule("mac", mac_methods); |
| 240 | d = getmoduledict(m); |
| 241 | |
| 242 | /* Initialize mac.error exception */ |
| 243 | MacError = newstringobject("mac.error"); |
| 244 | if (MacError == NULL || dictinsert(d, "error", MacError) != 0) |
| 245 | fatal("can't define mac.error"); |
| 246 | } |