home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pytexdoc / ext / source / !Python / Modules / c / riscosmodu < prev    next >
Encoding:
Text File  |  1996-08-12  |  6.3 KB  |  256 lines

  1. /* RISCOS module implementation */
  2.  
  3. #include "h.osfscontrol"
  4. #include "h.osgbpb"
  5. #include "h.os"
  6. #include "h.osfile"
  7.  
  8. #include "allobjects.h"
  9.  
  10. #include <errno.h>
  11.  
  12. static os_error *e;
  13.  
  14. static object *RiscosError; /* Exception riscos.error */
  15.  
  16. static object *riscos_oserror(void)
  17. { err_setstr(RiscosError,e->errmess);
  18.   return 0;
  19. }
  20.  
  21. static object *riscos_error(char *s) { err_setstr(RiscosError,s);return 0;}
  22.  
  23. /* RISCOS file commands */
  24.  
  25. static object *riscos_remove(object *self,object *args)
  26. {       char *path1;
  27.     if (!getargs(args, "s", &path1)) return NULL;
  28.     if (remove(path1)) return err_errno(RiscosError);
  29.     INCREF(None);
  30.     return None;
  31. }
  32.  
  33. static object *riscos_rename(object *self,object *args)
  34. {    char *path1, *path2;
  35.     if (!getargs(args, "(ss)", &path1, &path2)) return NULL;
  36.     if (rename(path1,path2)) return err_errno(RiscosError);;
  37.     INCREF(None);
  38.     return None;
  39. }
  40.  
  41. static object *riscos_system(object *self,object *args)
  42. {    char *command;
  43.     if (!getargs(args, "s", &command)) return NULL;
  44.     return newintobject(system(command));
  45. }
  46.  
  47. static object *riscos_chdir(object *self,object *args)
  48. {    char *path;
  49.     if (!getargs(args, "s", &path)) return NULL;
  50.     e=xosfscontrol_dir(path);
  51.     if(e) return riscos_oserror();
  52.     INCREF(None);
  53.     return None;
  54. }
  55.  
  56. static object *canon(char *path)
  57. { int len;
  58.   object *obj;
  59.   char *buf;
  60.   e=xosfscontrol_canonicalise_path(path,0,0,0,0,&len);
  61.   if(e) return riscos_oserror();
  62.   obj=newsizedstringobject(NULL,-len);
  63.   if(obj==NULL) return NULL;
  64.   buf=getstringvalue(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.   DECREF(obj);
  69.   return riscos_oserror();
  70. }
  71.  
  72. static object *riscos_getcwd(object *self,object *args)
  73. { if(!getnoarg(args)) return NULL;
  74.   return canon("@");
  75. }
  76.  
  77. static object *riscos_expand(object *self,object *args)
  78. {    char *path;
  79.     if (!getargs(args, "s", &path)) return NULL;
  80.         return canon(path);
  81. }
  82.  
  83. static object *riscos_mkdir(object *self,object *args)
  84. {    char *path;
  85.         int mode;
  86.         if (!newgetargs(args, "s|i", &path, &mode)) return NULL;
  87.         e=xosfile_create_dir(path,0);
  88.         if(e) return riscos_oserror();
  89.     INCREF(None);
  90.     return None;
  91. }
  92.  
  93. static object *riscos_listdir(object *self,object *args)
  94. {    char *path,buf[256];
  95.         object *d, *v;
  96.         int c=0;
  97.     if (!getargs(args, "s", &path)) return NULL;
  98.     d=newlistobject(0);
  99.     if(!d) return NULL;
  100.     for(;;)
  101.     { e=xosgbpb_dir_entries(path,(osgbpb_string_list*)buf,1,c,256,0,0,&c);
  102.       if(e)
  103.       { DECREF(d);return riscos_oserror();
  104.       }
  105.       if(c==-1) break;
  106.       v=newstringobject(buf);
  107.       if(!v) { DECREF(d);return 0;}
  108.       if(addlistitem(d,v)) {DECREF(d);DECREF(v);return 0;}
  109.     }
  110.     return d;
  111. }
  112.  
  113. static object *riscos_stat(object *self,object *args)
  114. {    char *path;
  115.         int ob,len;
  116.         bits t=0;
  117.         bits ld,ex,at,ft,mode;
  118.     if (!getargs(args, "s", &path)) return NULL;
  119.     e=xosfile_read_stamped_no_path(path,&ob,&ld,&ex,&len,&at,&ft);
  120.     if(e) return riscos_oserror();
  121.     switch (ob)
  122.     { case osfile_IS_FILE:mode=0100000;break;  /* OCTAL */
  123.       case osfile_IS_DIR:mode=040000;break;
  124.       case osfile_IS_IMAGE:mode=0140000;break;
  125.       default:return riscos_error("Not found");
  126.     }
  127.     if(ft!=-1) t=unixtime(ld,ex);
  128.     mode|=(at&7)<<6;
  129.     mode|=((at&112)*9)>>4;
  130.         return mkvalue("(lllllllllllll)",
  131.             (long)mode,/*st_mode*/
  132.             0,/*st_ino*/
  133.             0,/*st_dev*/
  134.             0,/*st_nlink*/
  135.             0,/*st_uid*/
  136.             0,/*st_gid*/
  137.             (long)len,/*st_size*/
  138.             (long)t,/*st_atime*/
  139.             (long)t,/*st_mtime*/
  140.             (long)t,/*st_ctime*/
  141.             (long)ft,/*file type*/
  142.             (long)at,/*attributes*/
  143.             (long)ob/*object type*/
  144.             );
  145. }
  146.  
  147. static object *riscos_chmod(object *self,object *args)
  148. {    char *path;
  149.         bits mode;
  150.         bits attr;
  151.         attr=(mode&0x700)>>8;
  152.         attr|=(mode&7)<<4;
  153.     if (!getargs(args, "(si)", &path,(int*)&mode)) return NULL;
  154.         e=xosfile_write_attr(path,attr);
  155.         if(e) return riscos_oserror();
  156.     INCREF(None);
  157.     return None;
  158. }
  159.  
  160. static object *riscos_utime(object *self,object *args)
  161. {    char *path;
  162.         int x,y;
  163.     if (!getargs(args, "(s(ii))", &path,&x,&y)) return NULL;
  164.         e=xosfile_stamp(path);
  165.         if(e) return riscos_oserror();
  166.     INCREF(None);
  167.     return None;
  168. }
  169.  
  170. static object *riscos_settype(object *self,object *args)
  171. {    char *path,*name;
  172.         int type;
  173.     if (!getargs(args, "(si)", &path,&type))
  174.     { err_clear();
  175.       if (!getargs(args, "(ss)", &path,&name)) return NULL;
  176.       e=xosfscontrol_file_type_from_string(name,(bits*)&type);
  177.       if(e) return riscos_oserror();
  178.     }
  179.         e=xosfile_set_type(path,type);
  180.         if(e) return riscos_oserror();
  181.     INCREF(None);
  182.     return None;
  183. }
  184.  
  185. static object *riscos_getenv(object *self,object *args)
  186. { char *name,*value;
  187.   if(!getargs(args,"s",&name)) return NULL;
  188.   value=getenv(name);
  189.   if(value) return newstringobject(value);
  190.   INCREF(None);
  191.   return None;
  192. }
  193.  
  194. static object *riscos_putenv(object *self,object *args)
  195. { char *name,*value;
  196.   os_var_type type=os_VARTYPE_LITERAL_STRING;
  197.   if(!newgetargs(args,"ss|i",&name,&value,&type)) return NULL;
  198.   if(type!=os_VARTYPE_STRING&&type!=os_VARTYPE_MACRO&&type!=os_VARTYPE_EXPANDED
  199.                             &&type!=os_VARTYPE_LITERAL_STRING)
  200.     return riscos_error("Bad putenv type");
  201.   e=xos_set_var_val(name,(byte*)value,strlen(value)+1,0,type,0,0);
  202.   if(e) return riscos_oserror();
  203.   INCREF(None);
  204.   return None;
  205. }
  206.  
  207. static struct methodlist riscos_methods[] = {
  208.  
  209.     {"unlink",    riscos_remove},
  210.         {"remove",      riscos_remove},
  211.     {"rename",    riscos_rename},
  212.     {"system",    riscos_system},
  213.     {"rmdir",    riscos_remove},
  214.     {"chdir",    riscos_chdir},
  215.     {"getcwd",    riscos_getcwd},
  216.     {"expand",      riscos_expand},
  217.     {"mkdir",    riscos_mkdir,1},
  218.     {"listdir",    riscos_listdir},
  219.     {"stat",    riscos_stat},
  220.     {"lstat",    riscos_stat},
  221.         {"chmod",    riscos_chmod},
  222.     {"utime",    riscos_utime},
  223.     {"settype",    riscos_settype},
  224.     {"getenv",      riscos_getenv},
  225.     {"putenv",      riscos_putenv},
  226.  
  227. /*
  228.     {"open",    riscos_open},
  229.     {"close",    riscos_close},
  230.     {"dup",        riscos_dup},
  231.     {"dup2",    riscos_dup2},
  232.     {"lseek",    riscos_lseek},
  233.     {"read",    riscos_read},
  234.     {"write",    riscos_write},
  235.     {"fstat",    riscos_fstat},
  236.     {"fdopen",    riscos_fdopen},
  237. */
  238.     {NULL,        NULL}         /* Sentinel */
  239. };
  240.  
  241.  
  242.  
  243. void
  244. initriscos()
  245. {
  246.     object *m, *d;
  247.  
  248.     m = initmodule("riscos", riscos_methods);
  249.     d = getmoduledict(m);
  250.  
  251.     /* Initialize riscos.error exception */
  252.     RiscosError = newstringobject("riscos.error");
  253.     if (RiscosError == NULL || dictinsert(d, "error", RiscosError) != 0)
  254.         fatal("can't define riscos.error");
  255. }
  256.