home *** CD-ROM | disk | FTP | other *** search
- /* RISCOS module implementation */
-
- #include "h.osfscontrol"
- #include "h.osgbpb"
- #include "h.os"
- #include "h.osfile"
-
- #include "allobjects.h"
-
- #include <errno.h>
-
- static os_error *e;
-
- static object *RiscosError; /* Exception riscos.error */
-
- static object *riscos_oserror(void)
- { err_setstr(RiscosError,e->errmess);
- return 0;
- }
-
- static object *riscos_error(char *s) { err_setstr(RiscosError,s);return 0;}
-
- /* RISCOS file commands */
-
- static object *riscos_remove(object *self,object *args)
- { char *path1;
- if (!getargs(args, "s", &path1)) return NULL;
- if (remove(path1)) return err_errno(RiscosError);
- INCREF(None);
- return None;
- }
-
- static object *riscos_rename(object *self,object *args)
- { char *path1, *path2;
- if (!getargs(args, "(ss)", &path1, &path2)) return NULL;
- if (rename(path1,path2)) return err_errno(RiscosError);;
- INCREF(None);
- return None;
- }
-
- static object *riscos_system(object *self,object *args)
- { char *command;
- if (!getargs(args, "s", &command)) return NULL;
- return newintobject(system(command));
- }
-
- static object *riscos_chdir(object *self,object *args)
- { char *path;
- if (!getargs(args, "s", &path)) return NULL;
- e=xosfscontrol_dir(path);
- if(e) return riscos_oserror();
- INCREF(None);
- return None;
- }
-
- static object *canon(char *path)
- { int len;
- object *obj;
- char *buf;
- e=xosfscontrol_canonicalise_path(path,0,0,0,0,&len);
- if(e) return riscos_oserror();
- obj=newsizedstringobject(NULL,-len);
- if(obj==NULL) return NULL;
- buf=getstringvalue(obj);
- e=xosfscontrol_canonicalise_path(path,buf,0,0,1-len,&len);
- if(len!=1) return riscos_error("Error expanding path");
- if(!e) return obj;
- DECREF(obj);
- return riscos_oserror();
- }
-
- static object *riscos_getcwd(object *self,object *args)
- { if(!getnoarg(args)) return NULL;
- return canon("@");
- }
-
- static object *riscos_expand(object *self,object *args)
- { char *path;
- if (!getargs(args, "s", &path)) return NULL;
- return canon(path);
- }
-
- static object *riscos_mkdir(object *self,object *args)
- { char *path;
- int mode;
- if (!newgetargs(args, "s|i", &path, &mode)) return NULL;
- e=xosfile_create_dir(path,0);
- if(e) return riscos_oserror();
- INCREF(None);
- return None;
- }
-
- static object *riscos_listdir(object *self,object *args)
- { char *path,buf[256];
- object *d, *v;
- int c=0;
- if (!getargs(args, "s", &path)) return NULL;
- d=newlistobject(0);
- if(!d) return NULL;
- for(;;)
- { e=xosgbpb_dir_entries(path,(osgbpb_string_list*)buf,1,c,256,0,0,&c);
- if(e)
- { DECREF(d);return riscos_oserror();
- }
- if(c==-1) break;
- v=newstringobject(buf);
- if(!v) { DECREF(d);return 0;}
- if(addlistitem(d,v)) {DECREF(d);DECREF(v);return 0;}
- }
- return d;
- }
-
- static object *riscos_stat(object *self,object *args)
- { char *path;
- int ob,len;
- bits t=0;
- bits ld,ex,at,ft,mode;
- if (!getargs(args, "s", &path)) return NULL;
- e=xosfile_read_stamped_no_path(path,&ob,&ld,&ex,&len,&at,&ft);
- if(e) return riscos_oserror();
- switch (ob)
- { case osfile_IS_FILE:mode=0100000;break; /* OCTAL */
- case osfile_IS_DIR:mode=040000;break;
- case osfile_IS_IMAGE:mode=0140000;break;
- default:return riscos_error("Not found");
- }
- if(ft!=-1) t=unixtime(ld,ex);
- mode|=(at&7)<<6;
- mode|=((at&112)*9)>>4;
- return mkvalue("(lllllllllllll)",
- (long)mode,/*st_mode*/
- 0,/*st_ino*/
- 0,/*st_dev*/
- 0,/*st_nlink*/
- 0,/*st_uid*/
- 0,/*st_gid*/
- (long)len,/*st_size*/
- (long)t,/*st_atime*/
- (long)t,/*st_mtime*/
- (long)t,/*st_ctime*/
- (long)ft,/*file type*/
- (long)at,/*attributes*/
- (long)ob/*object type*/
- );
- }
-
- static object *riscos_chmod(object *self,object *args)
- { char *path;
- bits mode;
- bits attr;
- attr=(mode&0x700)>>8;
- attr|=(mode&7)<<4;
- if (!getargs(args, "(si)", &path,(int*)&mode)) return NULL;
- e=xosfile_write_attr(path,attr);
- if(e) return riscos_oserror();
- INCREF(None);
- return None;
- }
-
- static object *riscos_utime(object *self,object *args)
- { char *path;
- int x,y;
- if (!getargs(args, "(s(ii))", &path,&x,&y)) return NULL;
- e=xosfile_stamp(path);
- if(e) return riscos_oserror();
- INCREF(None);
- return None;
- }
-
- static object *riscos_settype(object *self,object *args)
- { char *path,*name;
- int type;
- if (!getargs(args, "(si)", &path,&type))
- { err_clear();
- if (!getargs(args, "(ss)", &path,&name)) return NULL;
- e=xosfscontrol_file_type_from_string(name,(bits*)&type);
- if(e) return riscos_oserror();
- }
- e=xosfile_set_type(path,type);
- if(e) return riscos_oserror();
- INCREF(None);
- return None;
- }
-
- static object *riscos_getenv(object *self,object *args)
- { char *name,*value;
- if(!getargs(args,"s",&name)) return NULL;
- value=getenv(name);
- if(value) return newstringobject(value);
- INCREF(None);
- return None;
- }
-
- static object *riscos_putenv(object *self,object *args)
- { char *name,*value;
- os_var_type type=os_VARTYPE_LITERAL_STRING;
- if(!newgetargs(args,"ss|i",&name,&value,&type)) return NULL;
- if(type!=os_VARTYPE_STRING&&type!=os_VARTYPE_MACRO&&type!=os_VARTYPE_EXPANDED
- &&type!=os_VARTYPE_LITERAL_STRING)
- return riscos_error("Bad putenv type");
- e=xos_set_var_val(name,(byte*)value,strlen(value)+1,0,type,0,0);
- if(e) return riscos_oserror();
- INCREF(None);
- return None;
- }
-
- static struct methodlist riscos_methods[] = {
-
- {"unlink", riscos_remove},
- {"remove", riscos_remove},
- {"rename", riscos_rename},
- {"system", riscos_system},
- {"rmdir", riscos_remove},
- {"chdir", riscos_chdir},
- {"getcwd", riscos_getcwd},
- {"expand", riscos_expand},
- {"mkdir", riscos_mkdir,1},
- {"listdir", riscos_listdir},
- {"stat", riscos_stat},
- {"lstat", riscos_stat},
- {"chmod", riscos_chmod},
- {"utime", riscos_utime},
- {"settype", riscos_settype},
- {"getenv", riscos_getenv},
- {"putenv", riscos_putenv},
-
- /*
- {"open", riscos_open},
- {"close", riscos_close},
- {"dup", riscos_dup},
- {"dup2", riscos_dup2},
- {"lseek", riscos_lseek},
- {"read", riscos_read},
- {"write", riscos_write},
- {"fstat", riscos_fstat},
- {"fdopen", riscos_fdopen},
- */
- {NULL, NULL} /* Sentinel */
- };
-
-
-
- void
- initriscos()
- {
- object *m, *d;
-
- m = initmodule("riscos", riscos_methods);
- d = getmoduledict(m);
-
- /* Initialize riscos.error exception */
- RiscosError = newstringobject("riscos.error");
- if (RiscosError == NULL || dictinsert(d, "error", RiscosError) != 0)
- fatal("can't define riscos.error");
- }
-