blob: d5cdd94518be38b60ba2f4cccc1a45f88ebed15f [file] [log] [blame]
Guido van Rossum228d8072001-03-02 05:58:11 +00001/* RISCOS module implementation */
2
3#include "h.osfscontrol"
4#include "h.osgbpb"
5#include "h.os"
6#include "h.osfile"
7
8#include "Python.h"
9
10#include <errno.h>
11
12static os_error *e;
13
14static PyObject *RiscosError; /* Exception riscos.error */
15
16static PyObject *riscos_oserror(void)
17{ PyErr_SetString(RiscosError,e->errmess);
18 return 0;
19}
20
21static PyObject *riscos_error(char *s) { PyErr_SetString(RiscosError,s);return 0;}
22
23/* RISCOS file commands */
24
25static PyObject *riscos_remove(PyObject *self,PyObject *args)
26{ char *path1;
27 if (!PyArg_Parse(args, "s", &path1)) return NULL;
28 if (remove(path1)) return PyErr_SetFromErrno(RiscosError);
29 Py_INCREF(Py_None);
30 return Py_None;
31}
32
33static PyObject *riscos_rename(PyObject *self,PyObject *args)
34{ char *path1, *path2;
35 if (!PyArg_Parse(args, "(ss)", &path1, &path2)) return NULL;
36 if (rename(path1,path2)) return PyErr_SetFromErrno(RiscosError);;
37 Py_INCREF(Py_None);
38 return Py_None;
39}
40
41static PyObject *riscos_system(PyObject *self,PyObject *args)
42{ char *command;
43 if (!PyArg_Parse(args, "s", &command)) return NULL;
44 return PyInt_FromLong(system(command));
45}
46
47static PyObject *riscos_chdir(PyObject *self,PyObject *args)
48{ char *path;
49 if (!PyArg_Parse(args, "s", &path)) return NULL;
50 e=xosfscontrol_dir(path);
51 if(e) return riscos_oserror();
52 Py_INCREF(Py_None);
53 return Py_None;
54}
55
56static PyObject *canon(char *path)
57{ int len;
58 PyObject *obj;
59 char *buf;
60 e=xosfscontrol_canonicalise_path(path,0,0,0,0,&len);
61 if(e) return riscos_oserror();
62 obj=PyString_FromStringAndSize(NULL,-len);
63 if(obj==NULL) return NULL;
64 buf=PyString_AsString(obj);
65 e=xosfscontrol_canonicalise_path(path,buf,0,0,1-len,&len);
66 if(len!=1) return riscos_error("Error expanding path");
67 if(!e) return obj;
68 Py_DECREF(obj);
69 return riscos_oserror();
70}
71
72static PyObject *riscos_getcwd(PyObject *self,PyObject *args)
73{ if(!PyArg_NoArgs(args)) return NULL;
74 return canon("@");
75}
76
77static PyObject *riscos_expand(PyObject *self,PyObject *args)
78{ char *path;
79 if (!PyArg_Parse(args, "s", &path)) return NULL;
80 return canon(path);
81}
82
83static PyObject *riscos_mkdir(PyObject *self,PyObject *args)
84{ char *path;
85 int mode;
86 if (!PyArg_ParseTuple(args, "s|i", &path, &mode)) return NULL;
87 e=xosfile_create_dir(path,0);
88 if(e) return riscos_oserror();
89 Py_INCREF(Py_None);
90 return Py_None;
91}
92
93static PyObject *riscos_listdir(PyObject *self,PyObject *args)
94{ char *path,buf[256];
95 PyObject *d, *v;
96 int c=0,count;
97 if (!PyArg_Parse(args, "s", &path)) return NULL;
98 d=PyList_New(0);
99 if(!d) return NULL;
100 for(;;)
101 { e=xosgbpb_dir_entries(path,(osgbpb_string_list*)buf,
102 1,c,256,0,&count,&c);
103 if(e)
104 { Py_DECREF(d);return riscos_oserror();
105 }
106 if(count)
107 { v=PyString_FromString(buf);
108 if(!v) { Py_DECREF(d);return 0;}
109 if(PyList_Append(d,v)) {Py_DECREF(d);Py_DECREF(v);return 0;}
110 }
111 if(c==-1) break;
112 }
113 return d;
114}
115
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000116static char stat_result__doc__[] =
117"stat_result: Result from stat or lstat.\n\n\
118This object may be accessed either as a tuple of\n\
119 (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
120or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
121\n\
122RiscOS: The fields st_ftype, st_attrs, and st_obtype are also available.\n\
123\n\
124See os.stat for more information.\n";
125
126static PyStructSequence_Field stat_result_fields[] = {
127 { "st_mode", "protection bits" },
128 { "st_ino", "inode" },
129 { "st_dev", "device" },
130 { "st_nlink", "number of hard links" },
131 { "st_uid", "user ID of owner" },
132 { "st_gid", "group ID of owner" },
133 { "st_size", "total size, in bytes" },
134 { "st_atime", "time of last access" },
135 { "st_mtime", "time of last modification" },
136 { "st_ctime", "time of last change" },
137 { "st_ftype", "file type" },
138 { "st_attrs", "attributes" },
139 { "st_obtype", "object type" }
140 { 0 }
141};
142
143static PyStructSequence_Desc stat_result_desc = {
144 "stat_result",
145 stat_result__doc__,
146 stat_result_fields,
147 13
148};
149
150static PyTypeObject StatResultType;
151
Guido van Rossum228d8072001-03-02 05:58:11 +0000152static PyObject *riscos_stat(PyObject *self,PyObject *args)
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000153{
154 PyObject *v;
155 char *path;
Guido van Rossum228d8072001-03-02 05:58:11 +0000156 int ob,len;
157 bits t=0;
158 bits ld,ex,at,ft,mode;
159 if (!PyArg_Parse(args, "s", &path)) return NULL;
160 e=xosfile_read_stamped_no_path(path,&ob,&ld,&ex,&len,&at,&ft);
161 if(e) return riscos_oserror();
162 switch (ob)
163 { case osfile_IS_FILE:mode=0100000;break; /* OCTAL */
164 case osfile_IS_DIR:mode=040000;break;
165 case osfile_IS_IMAGE:mode=0140000;break;
166 default:return riscos_error("Not found");
167 }
168 if(ft!=-1) t=unixtime(ld,ex);
169 mode|=(at&7)<<6;
170 mode|=((at&112)*9)>>4;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000171
172 v = PyStructSequence_New(&StatResultType);
173
174 PyStructSequence_SET_ITEM(v, 0,
175 PyInt_FromLong((long) mode)); /*st_mode*/
176 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) 0)); /*st_ino*/
177 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) 0)); /*st_dev*/
178 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) 0)); /*st_nlink*/
179 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) 0)); /*st_uid*/
180 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) 0)); /*st_gid*/
181 PyStructSequence_SET_ITEM(v, 6,
182 PyInt_FromLong((long) len)); /*st_size*/
183 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) t)); /*st_atime*/
184 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) t)); /*st_mtime*/
185 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) t)); /*st_ctime*/
186 PyStructSequence_SET_ITEM(v, 10,
187 PyInt_FromLong((long) ft)); /*file type*/
188 PyStructSequence_SET_ITEM(v, 11,
189 PyInt_FromLong((long) at)); /*attributes*/
190 PyStructSequence_SET_ITEM(v, 12,
191 PyInt_FromLong((long) ot)); /*object type*/
192
193 if (PyErr_Occurred()) {
194 Py_DECREF(v);
195 return NULL;
196 }
197
198 return v;
Guido van Rossum228d8072001-03-02 05:58:11 +0000199}
200
201static PyObject *riscos_chmod(PyObject *self,PyObject *args)
202{ char *path;
203 bits mode;
204 bits attr;
205 attr=(mode&0x700)>>8;
206 attr|=(mode&7)<<4;
207 if (!PyArg_Parse(args, "(si)", &path,(int*)&mode)) return NULL;
208 e=xosfile_write_attr(path,attr);
209 if(e) return riscos_oserror();
210 Py_INCREF(Py_None);
211 return Py_None;
212}
213
214static PyObject *riscos_utime(PyObject *self,PyObject *args)
215{ char *path;
216 int x,y;
217 if (!PyArg_Parse(args, "(s(ii))", &path,&x,&y)) return NULL;
218 e=xosfile_stamp(path);
219 if(e) return riscos_oserror();
220 Py_INCREF(Py_None);
221 return Py_None;
222}
223
224static PyObject *riscos_settype(PyObject *self,PyObject *args)
225{ char *path,*name;
226 int type;
227 if (!PyArg_Parse(args, "(si)", &path,&type))
228 { PyErr_Clear();
229 if (!PyArg_Parse(args, "(ss)", &path,&name)) return NULL;
230 e=xosfscontrol_file_type_from_string(name,(bits*)&type);
231 if(e) return riscos_oserror();
232 }
233 e=xosfile_set_type(path,type);
234 if(e) return riscos_oserror();
235 Py_INCREF(Py_None);
236 return Py_None;
237}
238
239static PyObject *riscos_getenv(PyObject *self,PyObject *args)
240{ char *name,*value;
241 if(!PyArg_Parse(args,"s",&name)) return NULL;
242 value=getenv(name);
243 if(value) return PyString_FromString(value);
244 Py_INCREF(Py_None);
245 return Py_None;
246}
247
248static PyObject *riscos_putenv(PyObject *self,PyObject *args)
249{ char *name,*value;
250 int len;
251 os_var_type type=os_VARTYPE_LITERAL_STRING;
252 if(!PyArg_ParseTuple(args,"ss|i",&name,&value,&type)) return NULL;
253 if(type!=os_VARTYPE_STRING&&type!=os_VARTYPE_MACRO&&type!=os_VARTYPE_EXPANDED
254 &&type!=os_VARTYPE_LITERAL_STRING)
255 return riscos_error("Bad putenv type");
256 len=strlen(value);
257 if(type!=os_VARTYPE_LITERAL_STRING) len++;
258 /* Other types need null terminator! */
259 e=xos_set_var_val(name,(byte*)value,len,0,type,0,0);
260 if(e) return riscos_oserror();
261 Py_INCREF(Py_None);
262 return Py_None;
263}
264
265static PyObject *riscos_delenv(PyObject *self,PyObject *args)
266{ char *name;
267 if(!PyArg_Parse(args,"s",&name)) return NULL;
268 e=xos_set_var_val(name,NULL,-1,0,0,0,0);
269 if(e) return riscos_oserror();
270 Py_INCREF(Py_None);
271 return Py_None;
272}
273
274static PyObject *riscos_getenvdict(PyObject *self,PyObject *args)
275{ PyObject *dict;
276 char value[257];
277 char *which="*";
278 int size;
279 char *context=NULL;
280 if(!PyArg_ParseTuple(args,"|s",&which)) return NULL;
281 dict = PyDict_New();
282 if (!dict) return NULL;
283 /* XXX This part ignores errors */
284 while(!xos_read_var_val(which,value,sizeof(value)-1,(int)context,
285 os_VARTYPE_EXPANDED,&size,(int *)&context,0))
286 { PyObject *v;
287 value[size]='\0';
288 v = PyString_FromString(value);
289 if (v == NULL) continue;
290 PyDict_SetItemString(dict, context, v);
291 Py_DECREF(v);
292 }
293 return dict;
294}
295
296static PyMethodDef riscos_methods[] = {
297
298 {"unlink", riscos_remove},
299 {"remove", riscos_remove},
300 {"rename", riscos_rename},
301 {"system", riscos_system},
302 {"rmdir", riscos_remove},
303 {"chdir", riscos_chdir},
304 {"getcwd", riscos_getcwd},
305 {"expand", riscos_expand},
306 {"mkdir", riscos_mkdir,1},
307 {"listdir", riscos_listdir},
308 {"stat", riscos_stat},
309 {"lstat", riscos_stat},
310 {"chmod", riscos_chmod},
311 {"utime", riscos_utime},
312 {"settype", riscos_settype},
313 {"getenv", riscos_getenv},
314 {"putenv", riscos_putenv},
315 {"delenv", riscos_delenv},
316 {"getenvdict", riscos_getenvdict,1},
317 {NULL, NULL} /* Sentinel */
318};
319
320
321
322void
323initriscos()
324{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000325 PyObject *m, *d, *stat_m;
Guido van Rossum228d8072001-03-02 05:58:11 +0000326
327 m = Py_InitModule("riscos", riscos_methods);
328 d = PyModule_GetDict(m);
329
330 /* Initialize riscos.error exception */
331 RiscosError = PyString_FromString("riscos.error");
332 if (RiscosError == NULL || PyDict_SetItemString(d, "error", RiscosError) != 0)
333 Py_FatalError("can't define riscos.error");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000334
335 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
336 PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum228d8072001-03-02 05:58:11 +0000337}