| 1 | n/a | /* RISCOS module implementation */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #include "oslib/osfscontrol.h" |
|---|
| 4 | n/a | #include "oslib/osgbpb.h" |
|---|
| 5 | n/a | #include "oslib/os.h" |
|---|
| 6 | n/a | #include "oslib/osfile.h" |
|---|
| 7 | n/a | #include "unixstuff.h" |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | #include <sys/fcntl.h> |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | #include "Python.h" |
|---|
| 12 | n/a | #include "structseq.h" |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | #include <errno.h> |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | static os_error *e; |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | /*static PyObject *RiscosError;*/ /* Exception riscos.error */ |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | static PyObject *riscos_error(char *s) |
|---|
| 21 | n/a | { |
|---|
| 22 | n/a | PyErr_SetString(PyExc_OSError, s); |
|---|
| 23 | n/a | return NULL; |
|---|
| 24 | n/a | } |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | static PyObject *riscos_oserror(void) |
|---|
| 27 | n/a | { |
|---|
| 28 | n/a | return riscos_error(e->errmess); |
|---|
| 29 | n/a | } |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | /* RISCOS file commands */ |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | static PyObject * |
|---|
| 35 | n/a | riscos_remove(PyObject *self, PyObject *args) |
|---|
| 36 | n/a | { |
|---|
| 37 | n/a | char *path1; |
|---|
| 38 | n/a | if (!PyArg_ParseTuple(args, "s:remove", &path1)) return NULL; |
|---|
| 39 | n/a | if (remove(path1)) return PyErr_SetFromErrno(PyExc_OSError); |
|---|
| 40 | n/a | Py_INCREF(Py_None); |
|---|
| 41 | n/a | return Py_None; |
|---|
| 42 | n/a | } |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | static PyObject * |
|---|
| 45 | n/a | riscos_rename(PyObject *self, PyObject *args) |
|---|
| 46 | n/a | { |
|---|
| 47 | n/a | char *path1, *path2; |
|---|
| 48 | n/a | if (!PyArg_ParseTuple(args, "ss:rename", &path1, &path2)) |
|---|
| 49 | n/a | return NULL; |
|---|
| 50 | n/a | if (rename(path1,path2)) return PyErr_SetFromErrno(PyExc_OSError); |
|---|
| 51 | n/a | Py_INCREF(Py_None); |
|---|
| 52 | n/a | return Py_None; |
|---|
| 53 | n/a | } |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | static PyObject * |
|---|
| 56 | n/a | riscos_system(PyObject *self, PyObject *args) |
|---|
| 57 | n/a | { |
|---|
| 58 | n/a | char *command; |
|---|
| 59 | n/a | if (!PyArg_ParseTuple(args, "s:system", &command)) return NULL; |
|---|
| 60 | n/a | return PyInt_FromLong(system(command)); |
|---|
| 61 | n/a | } |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | static PyObject * |
|---|
| 64 | n/a | riscos_chdir(PyObject *self, PyObject *args) |
|---|
| 65 | n/a | { |
|---|
| 66 | n/a | char *path; |
|---|
| 67 | n/a | if (!PyArg_ParseTuple(args, "s:chdir", &path)) return NULL; |
|---|
| 68 | n/a | e=xosfscontrol_dir(path); |
|---|
| 69 | n/a | if(e) return riscos_oserror(); |
|---|
| 70 | n/a | Py_INCREF(Py_None); |
|---|
| 71 | n/a | return Py_None; |
|---|
| 72 | n/a | } |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | static PyObject * |
|---|
| 75 | n/a | canon(char *path) |
|---|
| 76 | n/a | { |
|---|
| 77 | n/a | int len; |
|---|
| 78 | n/a | PyObject *obj; |
|---|
| 79 | n/a | char *buf; |
|---|
| 80 | n/a | e=xosfscontrol_canonicalise_path(path,0,0,0,0,&len); |
|---|
| 81 | n/a | if(e) return riscos_oserror(); |
|---|
| 82 | n/a | obj=PyString_FromStringAndSize(NULL,-len); |
|---|
| 83 | n/a | if(obj==NULL) return NULL; |
|---|
| 84 | n/a | buf=PyString_AsString(obj); |
|---|
| 85 | n/a | e=xosfscontrol_canonicalise_path(path,buf,0,0,1-len,&len); |
|---|
| 86 | n/a | if(len!=1) return riscos_error("Error expanding path"); |
|---|
| 87 | n/a | if(!e) return obj; |
|---|
| 88 | n/a | Py_DECREF(obj); |
|---|
| 89 | n/a | return riscos_oserror(); |
|---|
| 90 | n/a | } |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | static PyObject * |
|---|
| 93 | n/a | riscos_getcwd(PyObject *self, PyObject *unused) |
|---|
| 94 | n/a | { |
|---|
| 95 | n/a | return canon("@"); |
|---|
| 96 | n/a | } |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | static PyObject * |
|---|
| 99 | n/a | riscos_expand(PyObject *self, PyObject *args) |
|---|
| 100 | n/a | { |
|---|
| 101 | n/a | char *path; |
|---|
| 102 | n/a | if (!PyArg_ParseTuple(args, "s:expand", &path)) return NULL; |
|---|
| 103 | n/a | return canon(path); |
|---|
| 104 | n/a | } |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | static PyObject * |
|---|
| 107 | n/a | riscos_mkdir(PyObject *self, PyObject *args) |
|---|
| 108 | n/a | { |
|---|
| 109 | n/a | char *path; |
|---|
| 110 | n/a | int mode; |
|---|
| 111 | n/a | if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode)) return NULL; |
|---|
| 112 | n/a | e=xosfile_create_dir(path,0); |
|---|
| 113 | n/a | if(e) return riscos_oserror(); |
|---|
| 114 | n/a | Py_INCREF(Py_None); |
|---|
| 115 | n/a | return Py_None; |
|---|
| 116 | n/a | } |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | static PyObject * |
|---|
| 119 | n/a | riscos_listdir(PyObject *self, PyObject *args) |
|---|
| 120 | n/a | { |
|---|
| 121 | n/a | char *path,buf[256]; |
|---|
| 122 | n/a | PyObject *d, *v; |
|---|
| 123 | n/a | int c=0,count; |
|---|
| 124 | n/a | if (!PyArg_ParseTuple(args, "s:listdir", &path)) return NULL; |
|---|
| 125 | n/a | d=PyList_New(0); |
|---|
| 126 | n/a | if(!d) return NULL; |
|---|
| 127 | n/a | for(;;) |
|---|
| 128 | n/a | { e=xosgbpb_dir_entries(path,(osgbpb_string_list*)buf, |
|---|
| 129 | n/a | 1,c,256,0,&count,&c); |
|---|
| 130 | n/a | if(e) |
|---|
| 131 | n/a | { Py_DECREF(d);return riscos_oserror(); |
|---|
| 132 | n/a | } |
|---|
| 133 | n/a | if(count) |
|---|
| 134 | n/a | { v=PyString_FromString(buf); |
|---|
| 135 | n/a | if(!v) { Py_DECREF(d);return 0;} |
|---|
| 136 | n/a | if(PyList_Append(d,v)) {Py_DECREF(d);Py_DECREF(v);return 0;} |
|---|
| 137 | n/a | } |
|---|
| 138 | n/a | if(c==-1) break; |
|---|
| 139 | n/a | } |
|---|
| 140 | n/a | return d; |
|---|
| 141 | n/a | } |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | PyDoc_STRVAR(stat_result__doc__, |
|---|
| 144 | n/a | "stat_result: Result from stat or lstat.\n\n\ |
|---|
| 145 | n/a | This object may be accessed either as a tuple of\n\ |
|---|
| 146 | n/a | (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
|---|
| 147 | n/a | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
|---|
| 148 | n/a | \n\ |
|---|
| 149 | n/a | RiscOS: The fields st_ftype, st_attrs, and st_obtype are also available.\n\ |
|---|
| 150 | n/a | \n\ |
|---|
| 151 | n/a | See os.stat for more information."); |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | static PyStructSequence_Field stat_result_fields[] = { |
|---|
| 154 | n/a | { "st_mode", "protection bits" }, |
|---|
| 155 | n/a | { "st_ino", "inode" }, |
|---|
| 156 | n/a | { "st_dev", "device" }, |
|---|
| 157 | n/a | { "st_nlink", "number of hard links" }, |
|---|
| 158 | n/a | { "st_uid", "user ID of owner" }, |
|---|
| 159 | n/a | { "st_gid", "group ID of owner" }, |
|---|
| 160 | n/a | { "st_size", "total size, in bytes" }, |
|---|
| 161 | n/a | { "st_atime", "time of last access" }, |
|---|
| 162 | n/a | { "st_mtime", "time of last modification" }, |
|---|
| 163 | n/a | { "st_ctime", "time of last change" }, |
|---|
| 164 | n/a | { "st_ftype", "file type" }, |
|---|
| 165 | n/a | { "st_attrs", "attributes" }, |
|---|
| 166 | n/a | { "st_obtype", "object type" }, |
|---|
| 167 | n/a | { 0 } |
|---|
| 168 | n/a | }; |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | static PyStructSequence_Desc stat_result_desc = { |
|---|
| 171 | n/a | "riscos.stat_result", |
|---|
| 172 | n/a | stat_result__doc__, |
|---|
| 173 | n/a | stat_result_fields, |
|---|
| 174 | n/a | 13 |
|---|
| 175 | n/a | }; |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | static PyTypeObject StatResultType; |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | static PyObject * |
|---|
| 180 | n/a | riscos_stat(PyObject *self, PyObject *args) |
|---|
| 181 | n/a | { |
|---|
| 182 | n/a | PyObject *v; |
|---|
| 183 | n/a | char *path; |
|---|
| 184 | n/a | int ob,len; |
|---|
| 185 | n/a | bits t=0; |
|---|
| 186 | n/a | bits ld,ex,at,ft,mode; |
|---|
| 187 | n/a | if (!PyArg_ParseTuple(args, "s:stat", &path)) return NULL; |
|---|
| 188 | n/a | e=xosfile_read_stamped_no_path(path,&ob,&ld,&ex,&len,&at,&ft); |
|---|
| 189 | n/a | if(e) return riscos_oserror(); |
|---|
| 190 | n/a | switch (ob) |
|---|
| 191 | n/a | { case osfile_IS_FILE:mode=0100000;break; /* OCTAL */ |
|---|
| 192 | n/a | case osfile_IS_DIR:mode=040000;break; |
|---|
| 193 | n/a | case osfile_IS_IMAGE:mode=0140000;break; |
|---|
| 194 | n/a | default:return riscos_error("Not found"); |
|---|
| 195 | n/a | } |
|---|
| 196 | n/a | if(ft!=-1) t=unixtime(ld,ex); |
|---|
| 197 | n/a | mode|=(at&7)<<6; |
|---|
| 198 | n/a | mode|=((at&112)*9)>>4; |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | v = PyStructSequence_New(&StatResultType); |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | PyStructSequence_SET_ITEM(v, 0, |
|---|
| 203 | n/a | PyInt_FromLong((long) mode)); /*st_mode*/ |
|---|
| 204 | n/a | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) 0)); /*st_ino*/ |
|---|
| 205 | n/a | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) 0)); /*st_dev*/ |
|---|
| 206 | n/a | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) 0)); /*st_nlink*/ |
|---|
| 207 | n/a | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) 0)); /*st_uid*/ |
|---|
| 208 | n/a | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) 0)); /*st_gid*/ |
|---|
| 209 | n/a | PyStructSequence_SET_ITEM(v, 6, |
|---|
| 210 | n/a | PyInt_FromLong((long) len)); /*st_size*/ |
|---|
| 211 | n/a | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) t)); /*st_atime*/ |
|---|
| 212 | n/a | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) t)); /*st_mtime*/ |
|---|
| 213 | n/a | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) t)); /*st_ctime*/ |
|---|
| 214 | n/a | PyStructSequence_SET_ITEM(v, 10, |
|---|
| 215 | n/a | PyInt_FromLong((long) ft)); /*file type*/ |
|---|
| 216 | n/a | PyStructSequence_SET_ITEM(v, 11, |
|---|
| 217 | n/a | PyInt_FromLong((long) at)); /*attributes*/ |
|---|
| 218 | n/a | PyStructSequence_SET_ITEM(v, 12, |
|---|
| 219 | n/a | PyInt_FromLong((long) ob)); /*object type*/ |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | if (PyErr_Occurred()) { |
|---|
| 222 | n/a | Py_DECREF(v); |
|---|
| 223 | n/a | return NULL; |
|---|
| 224 | n/a | } |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | return v; |
|---|
| 227 | n/a | } |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | static PyObject * |
|---|
| 230 | n/a | riscos_chmod(PyObject *self,PyObject *args) |
|---|
| 231 | n/a | { |
|---|
| 232 | n/a | char *path; |
|---|
| 233 | n/a | bits mode; |
|---|
| 234 | n/a | bits attr; |
|---|
| 235 | n/a | attr=(mode&0x700)>>8; |
|---|
| 236 | n/a | attr|=(mode&7)<<4; |
|---|
| 237 | n/a | if (!PyArg_ParseTuple(args, "si:chmod", &path,(int*)&mode)) return NULL; |
|---|
| 238 | n/a | e=xosfile_write_attr(path,attr); |
|---|
| 239 | n/a | if(e) return riscos_oserror(); |
|---|
| 240 | n/a | Py_INCREF(Py_None); |
|---|
| 241 | n/a | return Py_None; |
|---|
| 242 | n/a | } |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | static PyObject * |
|---|
| 246 | n/a | riscos_utime(PyObject *self, PyObject *args) |
|---|
| 247 | n/a | { |
|---|
| 248 | n/a | char *path; |
|---|
| 249 | n/a | long atime, mtime; |
|---|
| 250 | n/a | PyObject* arg; |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
|---|
| 253 | n/a | return NULL; |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | if (arg == Py_None) { |
|---|
| 256 | n/a | /* optional time values not given */ |
|---|
| 257 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 258 | n/a | e=xosfile_stamp(path); |
|---|
| 259 | n/a | Py_END_ALLOW_THREADS |
|---|
| 260 | n/a | if(e) return riscos_oserror(); |
|---|
| 261 | n/a | } |
|---|
| 262 | n/a | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
|---|
| 263 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 264 | n/a | "utime() arg 2 must be a tuple (atime, mtime)"); |
|---|
| 265 | n/a | return NULL; |
|---|
| 266 | n/a | } |
|---|
| 267 | n/a | else { |
|---|
| 268 | n/a | /* catalogue info*/ |
|---|
| 269 | n/a | fileswitch_object_type obj_type; |
|---|
| 270 | n/a | bits load_addr, exec_addr; |
|---|
| 271 | n/a | int size; |
|---|
| 272 | n/a | fileswitch_attr attr; |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | /* read old catalogue info */ |
|---|
| 275 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 276 | n/a | e=xosfile_read_no_path(path, &obj_type, &load_addr, &exec_addr, &size, &attr); |
|---|
| 277 | n/a | Py_END_ALLOW_THREADS |
|---|
| 278 | n/a | if(e) return riscos_oserror(); |
|---|
| 279 | n/a | |
|---|
| 280 | n/a | /* check if load and exec address really contain filetype and date */ |
|---|
| 281 | n/a | if ( (load_addr & 0xFFF00000U) != 0xFFF00000U) |
|---|
| 282 | n/a | return riscos_error("can't set date for object with load and exec addresses"); |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | /* convert argument mtime to RISC OS load and exec address */ |
|---|
| 285 | n/a | if(acorntime(&exec_addr, &load_addr, (time_t) mtime)) |
|---|
| 286 | n/a | return riscos_oserror(); |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | /* write new load and exec address */ |
|---|
| 289 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 290 | n/a | e = xosfile_write(path, load_addr, exec_addr, attr); |
|---|
| 291 | n/a | Py_END_ALLOW_THREADS |
|---|
| 292 | n/a | if(e) return riscos_oserror(); |
|---|
| 293 | n/a | } |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | Py_INCREF(Py_None); |
|---|
| 296 | n/a | return Py_None; |
|---|
| 297 | n/a | } |
|---|
| 298 | n/a | |
|---|
| 299 | n/a | static PyObject * |
|---|
| 300 | n/a | riscos_settype(PyObject *self, PyObject *args) |
|---|
| 301 | n/a | { |
|---|
| 302 | n/a | char *path,*name; |
|---|
| 303 | n/a | int type; |
|---|
| 304 | n/a | if (!PyArg_ParseTuple(args, "si:settype", &path,&type)) |
|---|
| 305 | n/a | { |
|---|
| 306 | n/a | PyErr_Clear(); |
|---|
| 307 | n/a | if (!PyArg_ParseTuple(args, "ss:settype", &path,&name)) return NULL; |
|---|
| 308 | n/a | e=xosfscontrol_file_type_from_string(name,(bits*)&type); |
|---|
| 309 | n/a | if(e) return riscos_oserror(); |
|---|
| 310 | n/a | } |
|---|
| 311 | n/a | e=xosfile_set_type(path,type); |
|---|
| 312 | n/a | if(e) return riscos_oserror(); |
|---|
| 313 | n/a | Py_INCREF(Py_None); |
|---|
| 314 | n/a | return Py_None; |
|---|
| 315 | n/a | } |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | static PyObject * |
|---|
| 318 | n/a | riscos_getenv(PyObject *self, PyObject *args) |
|---|
| 319 | n/a | { |
|---|
| 320 | n/a | char *name,*value; |
|---|
| 321 | n/a | if(!PyArg_ParseTuple(args,"s:getenv",&name)) return NULL; |
|---|
| 322 | n/a | value=getenv(name); |
|---|
| 323 | n/a | if(value) return PyString_FromString(value); |
|---|
| 324 | n/a | Py_INCREF(Py_None); |
|---|
| 325 | n/a | return Py_None; |
|---|
| 326 | n/a | } |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | static PyObject * |
|---|
| 329 | n/a | riscos_putenv(PyObject *self, PyObject *args) |
|---|
| 330 | n/a | { |
|---|
| 331 | n/a | char *name,*value; |
|---|
| 332 | n/a | int len; |
|---|
| 333 | n/a | os_var_type type=os_VARTYPE_LITERAL_STRING; |
|---|
| 334 | n/a | if(!PyArg_ParseTuple(args,"ss|i:putenv",&name,&value,&type)) return NULL; |
|---|
| 335 | n/a | if(type!=os_VARTYPE_STRING&&type!=os_VARTYPE_MACRO&&type!=os_VARTYPE_EXPANDED |
|---|
| 336 | n/a | &&type!=os_VARTYPE_LITERAL_STRING) |
|---|
| 337 | n/a | return riscos_error("Bad putenv type"); |
|---|
| 338 | n/a | len=strlen(value); |
|---|
| 339 | n/a | if(type!=os_VARTYPE_LITERAL_STRING) len++; |
|---|
| 340 | n/a | /* Other types need null terminator! */ |
|---|
| 341 | n/a | e=xos_set_var_val(name,(byte*)value,len,0,type,0,0); |
|---|
| 342 | n/a | if(e) return riscos_oserror(); |
|---|
| 343 | n/a | Py_INCREF(Py_None); |
|---|
| 344 | n/a | return Py_None; |
|---|
| 345 | n/a | } |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | static PyObject * |
|---|
| 348 | n/a | riscos_delenv(PyObject *self, PyObject *args) |
|---|
| 349 | n/a | { |
|---|
| 350 | n/a | char *name; |
|---|
| 351 | n/a | if(!PyArg_ParseTuple(args,"s:delenv",&name)) return NULL; |
|---|
| 352 | n/a | e=xos_set_var_val(name,NULL,-1,0,0,0,0); |
|---|
| 353 | n/a | if(e) return riscos_oserror(); |
|---|
| 354 | n/a | Py_INCREF(Py_None); |
|---|
| 355 | n/a | return Py_None; |
|---|
| 356 | n/a | } |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | static PyObject * |
|---|
| 359 | n/a | riscos_getenvdict(PyObject *self, PyObject *args) |
|---|
| 360 | n/a | { |
|---|
| 361 | n/a | PyObject *dict; |
|---|
| 362 | n/a | char value[257]; |
|---|
| 363 | n/a | char *which="*"; |
|---|
| 364 | n/a | int size; |
|---|
| 365 | n/a | char *context=NULL; |
|---|
| 366 | n/a | if(!PyArg_ParseTuple(args,"|s:getenvdict",&which)) return NULL; |
|---|
| 367 | n/a | dict = PyDict_New(); |
|---|
| 368 | n/a | if (!dict) return NULL; |
|---|
| 369 | n/a | /* XXX This part ignores errors */ |
|---|
| 370 | n/a | while(!xos_read_var_val(which,value,sizeof(value)-1,(int)context, |
|---|
| 371 | n/a | os_VARTYPE_EXPANDED,&size,(int *)&context,0)) |
|---|
| 372 | n/a | { PyObject *v; |
|---|
| 373 | n/a | value[size]='\0'; |
|---|
| 374 | n/a | v = PyString_FromString(value); |
|---|
| 375 | n/a | if (v == NULL) continue; |
|---|
| 376 | n/a | PyDict_SetItemString(dict, context, v); |
|---|
| 377 | n/a | Py_DECREF(v); |
|---|
| 378 | n/a | } |
|---|
| 379 | n/a | return dict; |
|---|
| 380 | n/a | } |
|---|
| 381 | n/a | |
|---|
| 382 | n/a | static PyMethodDef riscos_methods[] = { |
|---|
| 383 | n/a | |
|---|
| 384 | n/a | {"unlink", riscos_remove, METH_VARARGS}, |
|---|
| 385 | n/a | {"remove", riscos_remove, METH_VARARGS}, |
|---|
| 386 | n/a | {"rename", riscos_rename, METH_VARARGS}, |
|---|
| 387 | n/a | {"system", riscos_system, METH_VARARGS}, |
|---|
| 388 | n/a | {"rmdir", riscos_remove, METH_VARARGS}, |
|---|
| 389 | n/a | {"chdir", riscos_chdir, METH_VARARGS}, |
|---|
| 390 | n/a | {"getcwd", riscos_getcwd, METH_NOARGS}, |
|---|
| 391 | n/a | {"expand", riscos_expand, METH_VARARGS}, |
|---|
| 392 | n/a | {"mkdir", riscos_mkdir, METH_VARARGS}, |
|---|
| 393 | n/a | {"listdir", riscos_listdir, METH_VARARGS}, |
|---|
| 394 | n/a | {"stat", riscos_stat, METH_VARARGS}, |
|---|
| 395 | n/a | {"lstat", riscos_stat, METH_VARARGS}, |
|---|
| 396 | n/a | {"chmod", riscos_chmod, METH_VARARGS}, |
|---|
| 397 | n/a | {"utime", riscos_utime, METH_VARARGS}, |
|---|
| 398 | n/a | {"settype", riscos_settype, METH_VARARGS}, |
|---|
| 399 | n/a | {"getenv", riscos_getenv, METH_VARARGS}, |
|---|
| 400 | n/a | {"putenv", riscos_putenv, METH_VARARGS}, |
|---|
| 401 | n/a | {"delenv", riscos_delenv, METH_VARARGS}, |
|---|
| 402 | n/a | {"getenvdict", riscos_getenvdict, METH_VARARGS}, |
|---|
| 403 | n/a | {NULL, NULL} /* Sentinel */ |
|---|
| 404 | n/a | }; |
|---|
| 405 | n/a | |
|---|
| 406 | n/a | static int |
|---|
| 407 | n/a | ins(PyObject *module, char *symbol, long value) |
|---|
| 408 | n/a | { |
|---|
| 409 | n/a | return PyModule_AddIntConstant(module, symbol, value); |
|---|
| 410 | n/a | } |
|---|
| 411 | n/a | |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | static int |
|---|
| 414 | n/a | all_ins(PyObject *d) |
|---|
| 415 | n/a | { |
|---|
| 416 | n/a | #ifdef F_OK |
|---|
| 417 | n/a | if (ins(d, "F_OK", (long)F_OK)) return -1; |
|---|
| 418 | n/a | #endif |
|---|
| 419 | n/a | #ifdef R_OK |
|---|
| 420 | n/a | if (ins(d, "R_OK", (long)R_OK)) return -1; |
|---|
| 421 | n/a | #endif |
|---|
| 422 | n/a | #ifdef W_OK |
|---|
| 423 | n/a | if (ins(d, "W_OK", (long)W_OK)) return -1; |
|---|
| 424 | n/a | #endif |
|---|
| 425 | n/a | #ifdef X_OK |
|---|
| 426 | n/a | if (ins(d, "X_OK", (long)X_OK)) return -1; |
|---|
| 427 | n/a | #endif |
|---|
| 428 | n/a | #ifdef NGROUPS_MAX |
|---|
| 429 | n/a | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
|---|
| 430 | n/a | #endif |
|---|
| 431 | n/a | #ifdef TMP_MAX |
|---|
| 432 | n/a | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
|---|
| 433 | n/a | #endif |
|---|
| 434 | n/a | #ifdef WCONTINUED |
|---|
| 435 | n/a | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
|---|
| 436 | n/a | #endif |
|---|
| 437 | n/a | #ifdef WNOHANG |
|---|
| 438 | n/a | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
|---|
| 439 | n/a | #endif |
|---|
| 440 | n/a | #ifdef WUNTRACED |
|---|
| 441 | n/a | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
|---|
| 442 | n/a | #endif |
|---|
| 443 | n/a | #ifdef O_RDONLY |
|---|
| 444 | n/a | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
|---|
| 445 | n/a | #endif |
|---|
| 446 | n/a | #ifdef O_WRONLY |
|---|
| 447 | n/a | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
|---|
| 448 | n/a | #endif |
|---|
| 449 | n/a | #ifdef O_RDWR |
|---|
| 450 | n/a | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
|---|
| 451 | n/a | #endif |
|---|
| 452 | n/a | #ifdef O_NDELAY |
|---|
| 453 | n/a | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
|---|
| 454 | n/a | #endif |
|---|
| 455 | n/a | #ifdef O_NONBLOCK |
|---|
| 456 | n/a | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
|---|
| 457 | n/a | #endif |
|---|
| 458 | n/a | #ifdef O_APPEND |
|---|
| 459 | n/a | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
|---|
| 460 | n/a | #endif |
|---|
| 461 | n/a | #ifdef O_DSYNC |
|---|
| 462 | n/a | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
|---|
| 463 | n/a | #endif |
|---|
| 464 | n/a | #ifdef O_RSYNC |
|---|
| 465 | n/a | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
|---|
| 466 | n/a | #endif |
|---|
| 467 | n/a | #ifdef O_SYNC |
|---|
| 468 | n/a | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
|---|
| 469 | n/a | #endif |
|---|
| 470 | n/a | #ifdef O_NOCTTY |
|---|
| 471 | n/a | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
|---|
| 472 | n/a | #endif |
|---|
| 473 | n/a | #ifdef O_CREAT |
|---|
| 474 | n/a | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
|---|
| 475 | n/a | #endif |
|---|
| 476 | n/a | #ifdef O_EXCL |
|---|
| 477 | n/a | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
|---|
| 478 | n/a | #endif |
|---|
| 479 | n/a | #ifdef O_TRUNC |
|---|
| 480 | n/a | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
|---|
| 481 | n/a | #endif |
|---|
| 482 | n/a | #ifdef O_BINARY |
|---|
| 483 | n/a | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
|---|
| 484 | n/a | #endif |
|---|
| 485 | n/a | #ifdef O_TEXT |
|---|
| 486 | n/a | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
|---|
| 487 | n/a | #endif |
|---|
| 488 | n/a | #ifdef O_LARGEFILE |
|---|
| 489 | n/a | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
|---|
| 490 | n/a | #endif |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | /* MS Windows */ |
|---|
| 493 | n/a | #ifdef O_NOINHERIT |
|---|
| 494 | n/a | /* Don't inherit in child processes. */ |
|---|
| 495 | n/a | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
|---|
| 496 | n/a | #endif |
|---|
| 497 | n/a | #ifdef _O_SHORT_LIVED |
|---|
| 498 | n/a | /* Optimize for short life (keep in memory). */ |
|---|
| 499 | n/a | /* MS forgot to define this one with a non-underscore form too. */ |
|---|
| 500 | n/a | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
|---|
| 501 | n/a | #endif |
|---|
| 502 | n/a | #ifdef O_TEMPORARY |
|---|
| 503 | n/a | /* Automatically delete when last handle is closed. */ |
|---|
| 504 | n/a | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
|---|
| 505 | n/a | #endif |
|---|
| 506 | n/a | #ifdef O_RANDOM |
|---|
| 507 | n/a | /* Optimize for random access. */ |
|---|
| 508 | n/a | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
|---|
| 509 | n/a | #endif |
|---|
| 510 | n/a | #ifdef O_SEQUENTIAL |
|---|
| 511 | n/a | /* Optimize for sequential access. */ |
|---|
| 512 | n/a | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
|---|
| 513 | n/a | #endif |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | /* GNU extensions. */ |
|---|
| 516 | n/a | #ifdef O_DIRECT |
|---|
| 517 | n/a | /* Direct disk access. */ |
|---|
| 518 | n/a | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
|---|
| 519 | n/a | #endif |
|---|
| 520 | n/a | #ifdef O_DIRECTORY |
|---|
| 521 | n/a | /* Must be a directory. */ |
|---|
| 522 | n/a | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
|---|
| 523 | n/a | #endif |
|---|
| 524 | n/a | #ifdef O_NOFOLLOW |
|---|
| 525 | n/a | /* Do not follow links. */ |
|---|
| 526 | n/a | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
|---|
| 527 | n/a | #endif |
|---|
| 528 | n/a | |
|---|
| 529 | n/a | /* These come from sysexits.h */ |
|---|
| 530 | n/a | #ifdef EX_OK |
|---|
| 531 | n/a | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
|---|
| 532 | n/a | #endif /* EX_OK */ |
|---|
| 533 | n/a | #ifdef EX_USAGE |
|---|
| 534 | n/a | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
|---|
| 535 | n/a | #endif /* EX_USAGE */ |
|---|
| 536 | n/a | #ifdef EX_DATAERR |
|---|
| 537 | n/a | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
|---|
| 538 | n/a | #endif /* EX_DATAERR */ |
|---|
| 539 | n/a | #ifdef EX_NOINPUT |
|---|
| 540 | n/a | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
|---|
| 541 | n/a | #endif /* EX_NOINPUT */ |
|---|
| 542 | n/a | #ifdef EX_NOUSER |
|---|
| 543 | n/a | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
|---|
| 544 | n/a | #endif /* EX_NOUSER */ |
|---|
| 545 | n/a | #ifdef EX_NOHOST |
|---|
| 546 | n/a | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
|---|
| 547 | n/a | #endif /* EX_NOHOST */ |
|---|
| 548 | n/a | #ifdef EX_UNAVAILABLE |
|---|
| 549 | n/a | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
|---|
| 550 | n/a | #endif /* EX_UNAVAILABLE */ |
|---|
| 551 | n/a | #ifdef EX_SOFTWARE |
|---|
| 552 | n/a | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
|---|
| 553 | n/a | #endif /* EX_SOFTWARE */ |
|---|
| 554 | n/a | #ifdef EX_OSERR |
|---|
| 555 | n/a | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
|---|
| 556 | n/a | #endif /* EX_OSERR */ |
|---|
| 557 | n/a | #ifdef EX_OSFILE |
|---|
| 558 | n/a | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
|---|
| 559 | n/a | #endif /* EX_OSFILE */ |
|---|
| 560 | n/a | #ifdef EX_CANTCREAT |
|---|
| 561 | n/a | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
|---|
| 562 | n/a | #endif /* EX_CANTCREAT */ |
|---|
| 563 | n/a | #ifdef EX_IOERR |
|---|
| 564 | n/a | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
|---|
| 565 | n/a | #endif /* EX_IOERR */ |
|---|
| 566 | n/a | #ifdef EX_TEMPFAIL |
|---|
| 567 | n/a | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
|---|
| 568 | n/a | #endif /* EX_TEMPFAIL */ |
|---|
| 569 | n/a | #ifdef EX_PROTOCOL |
|---|
| 570 | n/a | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
|---|
| 571 | n/a | #endif /* EX_PROTOCOL */ |
|---|
| 572 | n/a | #ifdef EX_NOPERM |
|---|
| 573 | n/a | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
|---|
| 574 | n/a | #endif /* EX_NOPERM */ |
|---|
| 575 | n/a | #ifdef EX_CONFIG |
|---|
| 576 | n/a | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
|---|
| 577 | n/a | #endif /* EX_CONFIG */ |
|---|
| 578 | n/a | #ifdef EX_NOTFOUND |
|---|
| 579 | n/a | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
|---|
| 580 | n/a | #endif /* EX_NOTFOUND */ |
|---|
| 581 | n/a | |
|---|
| 582 | n/a | return 0; |
|---|
| 583 | n/a | } |
|---|
| 584 | n/a | |
|---|
| 585 | n/a | |
|---|
| 586 | n/a | void |
|---|
| 587 | n/a | initriscos() |
|---|
| 588 | n/a | { |
|---|
| 589 | n/a | PyObject *m, *d, *stat_m; |
|---|
| 590 | n/a | |
|---|
| 591 | n/a | m = Py_InitModule("riscos", riscos_methods); |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | if (all_ins(m)) |
|---|
| 594 | n/a | return; |
|---|
| 595 | n/a | |
|---|
| 596 | n/a | d = PyModule_GetDict(m); |
|---|
| 597 | n/a | |
|---|
| 598 | n/a | Py_INCREF(PyExc_OSError); |
|---|
| 599 | n/a | PyModule_AddObject(m, "error", PyExc_OSError); |
|---|
| 600 | n/a | |
|---|
| 601 | n/a | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
|---|
| 602 | n/a | PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType); |
|---|
| 603 | n/a | } |
|---|