home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / acorn / riscos.c < prev    next >
C/C++ Source or Header  |  1997-05-09  |  7KB  |  375 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 = ""; /* 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.  os_error *er;
  201.  
  202.  len = strlen(d);
  203.  if ((s = malloc(len + 1)) == NULL)
  204.    return -1;
  205.  
  206.  strcpy(s,d);
  207.  if (s[len-1]=='.')
  208.    s[len-1]=0;
  209.  
  210.  if (er=SWI_OS_File_5(s,&objtype,NULL,NULL,NULL,NULL),er!=NULL) {
  211.    free(s);
  212.    return -1;
  213.  }
  214.  if (objtype<2 || (!scanimage && objtype==3)) {
  215.    /* this is a file or it doesn't exist */
  216.    free(s);
  217.    return -1;
  218.  }
  219.  
  220.  if (er=SWI_OS_File_6(s),er!=NULL) {
  221.    /* maybe this is a problem with the DDEUtils module, try to canonicalise the path */
  222.    char canon[256];
  223.    int size=255;
  224.  
  225.    if (er=SWI_OS_FSControl_37(s,canon,&size),er!=NULL) {
  226.      free(s);
  227.      return -1;
  228.    }
  229.    if (er=SWI_OS_File_6(canon),er!=NULL) {
  230.      free(s);
  231.      return -1;
  232.    }
  233.  }
  234.  free(s);
  235.  return 0;
  236. }
  237.  
  238. #endif /* !SFX */
  239.  
  240. int chmod(char *file, int mode)
  241. {
  242. /*************** NOT YET IMPLEMENTED!!!!!! ******************/
  243. /* I don't know if this will be needed or not... */
  244.  file=file;
  245.  mode=mode;
  246.  return 0;
  247. }
  248.  
  249. void setfiletype(char *fname,int ftype)
  250. {
  251.  char str[256];
  252.  sprintf(str,"SetType %s &%3.3X",fname,ftype);
  253.  SWI_OS_CLI(str);
  254. }
  255.  
  256. void getRISCOSexts(char *envstr)
  257. {
  258.  char *envptr;                               /* value returned by getenv */
  259.  
  260.  envptr = getenv(envstr);
  261.  if (envptr == NULL || *envptr == 0) return;
  262.  
  263.  exts2swap=malloc(1+strlen(envptr));
  264.  if (exts2swap == NULL)
  265.    return;
  266.  
  267.  strcpy(exts2swap, envptr);
  268. }
  269.  
  270. int checkext(char *suff)
  271. {
  272.  register char *extptr=exts2swap;
  273.  register char *suffptr;
  274.  register int e,s;
  275.  
  276.  while(*extptr) {
  277.    suffptr=suff;
  278.    e=*extptr; s=*suffptr;
  279.    while (e && e!=':' && s && s!='.' && s!='/' && e==s) {
  280.      e=*++extptr; s=*++suffptr;
  281.    }
  282.    if (e==':') e=0;
  283.    if (s=='.' || s=='/') s=0;
  284.    if (!e && !s) {
  285.      return 1;
  286.    }
  287.    while(*extptr!=':' && *extptr!='\0')    /* skip to next extension */
  288.      extptr++;
  289.    if (*extptr!='\0')
  290.      extptr++;
  291.  }
  292.  return 0;
  293. }
  294.  
  295. void swapext(char *name, char *exptr)
  296. {
  297.  char ext[MAXEXT];
  298.  register char *p1=exptr+1;
  299.  register char *p2=ext;
  300.  int extchar=*exptr;
  301.  
  302.  while(*p1 && *p1!='.' && *p1!='/')
  303.    *p2++=*p1++;
  304.  *p2=0;
  305.  p2=exptr-1;
  306.  p1--;
  307.  while(p2 >= name)
  308.    *p1--=*p2--;
  309.  p1=name;
  310.  p2=ext;
  311.  while(*p2)
  312.    *p1++=*p2++;
  313.  *p1=(extchar=='/'?'.':'/');
  314. }
  315.  
  316. void remove_prefix(void)
  317. {
  318.  SWI_DDEUtils_Prefix(NULL);
  319. }
  320.  
  321. void set_prefix(void)
  322. {
  323.  char *pref;
  324.  int size=0;
  325.  
  326.  if (SWI_OS_FSControl_37("@",pref,&size)!=NULL)
  327.    return;
  328.  
  329.  size=1-size;
  330.  
  331.  if (pref=malloc(size),pref!=NULL) {
  332.    if (SWI_OS_FSControl_37("@",pref,&size)!=NULL) {
  333.      free(pref);
  334.      return;
  335.    }
  336.  
  337.    if (SWI_DDEUtils_Prefix(pref)==NULL) {
  338.      atexit(remove_prefix);
  339.    }
  340.  
  341.    free(pref);
  342.  }
  343. }
  344.  
  345. #ifdef localtime
  346. #  undef localtime
  347. #endif
  348.  
  349. #ifdef gmtime
  350. #  undef gmtime
  351. #endif
  352.  
  353. /* Acorn's implementation of localtime() and gmtime()
  354.  * doesn't consider the timezone offset, so we have to
  355.  * add it before calling the library functions
  356.  */
  357.  
  358. struct tm *riscos_localtime(const time_t *timer)
  359. {
  360.  time_t localt=*timer;
  361.  
  362.  localt+=SWI_Read_Timezone()/100;
  363.  
  364.  return localtime(&localt);
  365. }
  366.  
  367. struct tm *riscos_gmtime(const time_t *timer)
  368. {
  369.  time_t localt=*timer;
  370.  
  371.  localt+=SWI_Read_Timezone()/100;
  372.  
  373.  return gmtime(&localt);
  374. }
  375.