home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / acorn / riscos.c < prev    next >
C/C++ Source or Header  |  1996-04-18  |  6KB  |  332 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1996 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden, Sergio Monesi, Karl Davis and
  5.  Igor Mandrichenko.
  6.  Permission is granted to any individual or institution to use, copy, or
  7.  redistribute this software so long as all of the original files are included,
  8.  that it is not sold for profit, and that this copyright notice is retained.
  9.  
  10. */
  11.  
  12. /* riscos.c */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "zip.h"
  18. #include "riscos.h"
  19.  
  20. #define MAXEXT 16
  21.  
  22. /* External globals */
  23. extern int scanimage;
  24.  
  25. /* Local globals (!?!?) */
  26. char *exts2swap = NULL; /* Extesions to swap (actually, directory names) */
  27.  
  28. int stat(char *filename,struct stat *res)
  29. {
  30.  int attr;    /* object attributes */
  31.  int load;    /* load address */
  32.  int exec;    /* exec address */
  33.  int type;    /* type: 0 not found, 1 file, 2 dir, 3 image */
  34.  
  35.  if (!res)
  36.    return -1;
  37.  
  38.  if (SWI_OS_File_5(filename,&type,&load,&exec,(int *)&res->st_size,&attr)!=NULL)
  39.    return -1;
  40.  
  41.  if (type==0)
  42.    return -1;
  43.  
  44.  res->st_dev=0;
  45.  res->st_ino=0;
  46.  res->st_nlink=0;
  47.  res->st_uid=1;
  48.  res->st_gid=1;
  49.  res->st_rdev=0;
  50.  res->st_blksize=1024;
  51.  
  52.  res->st_mode = ((attr & 0001) << 8) | ((attr & 0002) << 6) | ((attr & 0020) >> 2) | ((attr & 0040) >> 4);
  53.  
  54.  switch (type) {
  55.    case 1:                        /* File */
  56.     res->st_mode |= S_IFREG;
  57.     break;
  58.    case 2:                        /* Directory */
  59.     res->st_mode |= S_IFDIR | 0700;
  60.     break;
  61.    case 3:                        /* Image file */
  62.     if (scanimage)
  63.       res->st_mode |= S_IFDIR | 0700;
  64.     else
  65.       res->st_mode |= S_IFREG;
  66.     break;
  67.  }
  68.  
  69.  if ((((unsigned int) load) >> 20) == 0xfff) {     /* date stamped file */
  70.    register unsigned int t1, t2, tc;
  71.  
  72.    t1 = (unsigned int) (exec);
  73.    t2 = (unsigned int) (load & 0xff);
  74.  
  75.    tc = 0x6e996a00U;
  76.    if (t1 < tc)
  77.      t2--;
  78.    t1 -= tc;
  79.    t2 -= 0x33;          /* 00:00:00 Jan. 1 1970 = 0x336e996a00 */
  80.  
  81.    t1 = (t1 / 100) + (t2 * 42949673U);  /* 0x100000000 / 100 = 42949672.96 */
  82.    t1 -= (t2 / 25);             /* compensate for .04 error */
  83.  
  84.    res->st_atime = res->st_mtime = res->st_ctime = t1;
  85.  }
  86.  else
  87.    res->st_atime = res->st_mtime = res->st_ctime = 0;
  88.  
  89.  return 0;
  90. }
  91.  
  92. #ifndef SFX
  93.  
  94. DIR *opendir(char *dirname)
  95. {
  96.  DIR *thisdir;
  97.  int type;
  98.  int attr;
  99.  os_error *er;
  100.  
  101.  thisdir=(DIR *)malloc(sizeof(DIR));
  102.  if (thisdir==NULL)
  103.    return NULL;
  104.  
  105.  thisdir->dirname=(char *)malloc(strlen(dirname)+1);
  106.  if (thisdir->dirname==NULL) {
  107.    free(thisdir);
  108.    return NULL;
  109.  }
  110.  
  111.  strcpy(thisdir->dirname,dirname);
  112.  if (thisdir->dirname[strlen(thisdir->dirname)-1]=='.')
  113.    thisdir->dirname[strlen(thisdir->dirname)-1]=0;
  114.  
  115.  if (er=SWI_OS_File_5(thisdir->dirname,&type,NULL,NULL,NULL,&attr),er!=NULL ||
  116.      type<=1 || (type==3 && !scanimage))
  117.  {
  118.    free(thisdir->dirname);
  119.    free(thisdir);
  120.    return NULL;
  121.  }
  122.  
  123.  thisdir->buf=malloc(DIR_BUFSIZE);
  124.  if (thisdir->buf==NULL) {
  125.    free(thisdir->dirname);
  126.    free(thisdir);
  127.    return NULL;
  128.  }
  129.  
  130.  thisdir->size=DIR_BUFSIZE;
  131.  thisdir->offset=0;
  132.  thisdir->read=0;
  133.  
  134.  return thisdir;
  135. }
  136.  
  137. struct dirent *readdir(DIR *d)
  138. {
  139.  static struct dirent dent;
  140.  
  141.  if (d->read==0) {    /* no more objects read in the buffer */
  142.    if (d->offset==-1) {    /* no more objects to read */
  143.      return NULL;
  144.    }
  145.  
  146.    d->read=255;
  147.    if (SWI_OS_GBPB_9(d->dirname,d->buf,&d->read,&d->offset,DIR_BUFSIZE,NULL)!=NULL)
  148.      return NULL;
  149.  
  150.    if (d->read==0) {
  151.      d->offset=-1;
  152.      return NULL;
  153.    }
  154.    d->read--;
  155.    d->act=(char *)d->buf;
  156.  }
  157.  else {     /* some object is ready in buffer */
  158.    d->read--;
  159.    d->act=(char *)(d->act+strlen(d->act)+1);
  160.  }
  161.  
  162.  strcpy(dent.d_name,d->act);
  163.  dent.d_namlen=strlen(dent.d_name);
  164.  
  165.  return &dent;
  166. }
  167.  
  168. void closedir(DIR *d)
  169. {
  170.  if (d->buf!=NULL)
  171.    free(d->buf);
  172.  if (d->dirname!=NULL)
  173.    free(d->dirname);
  174.  free(d);
  175. }
  176.  
  177. int unlink(f)
  178. char *f;                /* file to delete */
  179. /* Delete the file *f, returning non-zero on failure. */
  180. {
  181.  os_error *er;
  182.  char canon[256];
  183.  int size=255;
  184.  
  185.  er=SWI_OS_FSControl_37(f,canon,&size);
  186.  if (er==NULL) {
  187.    er=SWI_OS_FSControl_27(canon,0x100);
  188.  }
  189.  else {
  190.    er=SWI_OS_FSControl_27(f,0x100);
  191.  }
  192.  return (int)er;
  193. }
  194.  
  195. int deletedir(char *d)
  196. {
  197.  int objtype;
  198.  char *s;
  199.  int len;
  200.  
  201.  len = strlen(d);
  202.  if ((s = malloc(len + 1)) == NULL)
  203.    return -1;
  204.  
  205.  strcpy(s,d);
  206.  if (s[len-1]=='.')
  207.    s[len-1]=0;
  208.  
  209.  if (SWI_OS_File_5(s,&objtype,NULL,NULL,NULL,NULL)!=NULL) {
  210.    free(s);
  211.    return -1;
  212.  }
  213.  if (objtype<2 || (!scanimage && objtype==3)) {
  214. /* this is a file or it doesn't exist */
  215.    free(s);
  216.    return -1;
  217.  }
  218.  if (SWI_OS_File_6(s)!=NULL) {
  219.    free(s);
  220.    return -1;
  221.  }
  222.  free(s);
  223.  return 0;
  224. }
  225.  
  226. #endif /* !SFX */
  227.  
  228. int chmod(char *file, int mode)
  229. {
  230. /*************** NOT YET IMPLEMENTED!!!!!! ******************/
  231. /* I don't know if this will be needed or not... */
  232.  file=file;
  233.  mode=mode;
  234.  return 0;
  235. }
  236.  
  237. void setfiletype(char *fname,int ftype)
  238. {
  239.  char str[256];
  240.  sprintf(str,"SetType %s &%3.3X",fname,ftype);
  241.  SWI_OS_CLI(str);
  242. }
  243.  
  244. void getRISCOSexts(char *envstr)
  245. {
  246.  char *envptr;                               /* value returned by getenv */
  247.  
  248.  envptr = getenv(envstr);
  249.  if (envptr == NULL || *envptr == 0) return;
  250.  
  251.  exts2swap=malloc(1+strlen(envptr));
  252.  if (exts2swap == NULL)
  253.    return;
  254.  
  255.  strcpy(exts2swap, envptr);
  256. }
  257.  
  258. int checkext(char *suff)
  259. {
  260.  register char *extptr=exts2swap;
  261.  register char *suffptr;
  262.  register int e,s;
  263.  
  264.  while(*extptr) {
  265.    suffptr=suff;
  266.    e=*extptr; s=*suffptr;
  267.    while (e && e!=':' && s && s!='.' && s!='/' && e==s) {
  268.      e=*++extptr; s=*++suffptr;
  269.    }
  270.    if (e==':') e=0;
  271.    if (s=='.' || s=='/') s=0;
  272.    if (!e && !s) {
  273.      return 1;
  274.    }
  275.    while(*extptr!=':')    /* skip to next extension */
  276.      extptr++;
  277.    if (*extptr!=NULL)
  278.      extptr++;
  279.  }
  280.  return 0;
  281. }
  282.  
  283. void swapext(char *name, char *exptr)
  284. {
  285.  char ext[MAXEXT];
  286.  register char *p1=exptr+1;
  287.  register char *p2=ext;
  288.  int extchar=*exptr;
  289.  
  290.  while(*p1 && *p1!='.' && *p1!='/')
  291.    *p2++=*p1++;
  292.  *p2=0;
  293.  p2=exptr-1;
  294.  p1--;
  295.  while(p2 >= name)
  296.    *p1--=*p2--;
  297.  p1=name;
  298.  p2=ext;
  299.  while(*p2)
  300.    *p1++=*p2++;
  301.  *p1=(extchar=='/'?'.':'/');
  302. }
  303.  
  304. void remove_prefix(void)
  305. {
  306.  SWI_DDEUtils_Prefix(NULL);
  307. }
  308.  
  309. void set_prefix(void)
  310. {
  311.  char *pref;
  312.  int size=0;
  313.  
  314.  if (SWI_OS_FSControl_37("@",pref,&size)!=NULL)
  315.    return;
  316.  
  317.  size=1-size;
  318.  
  319.  if (pref=malloc(size),pref!=NULL) {
  320.  if (SWI_OS_FSControl_37("@",pref,&size)!=NULL) {
  321.    free(pref);
  322.    return;
  323.  }
  324.  
  325.  if (SWI_DDEUtils_Prefix(pref)==NULL) {
  326.    atexit(remove_prefix);
  327.  }
  328.  
  329.  free(pref);
  330.  }
  331. }
  332.