home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / libnix-0.8-src.lha / libnix-0.8 / sources / nix / extra / __amigapath.c next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  1.6 KB  |  90 lines

  1. #include <errno.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stabs.h>
  5.  
  6. static int __pathconv;
  7.  
  8. /* Convert Un*x style pathnames to Amiga OS ones */
  9.  
  10. char *__amigapath(const char *path)
  11. { static char *s1=NULL;
  12.   char *s2,*s3;
  13.   int c;
  14.  
  15.   if(!__pathconv)
  16.     return (char *)path;
  17.  
  18.   if(s1!=NULL)
  19.     free(s1);
  20.  
  21.   s1=strdup(path);
  22.   if(s1==NULL)
  23.   { errno=ENOMEM;
  24.     return NULL; }
  25.   
  26.   s3=s2=s1; /* Replace multiple following '/' by single ones */
  27.   do
  28.   { if(*s2=='/')
  29.       while(s2[1]=='/')
  30.         s2++;
  31.     *s3++=*s2;
  32.   }while(*s2++);
  33.  
  34.   s3=s2=s1; /* Remove single dots '.' as directory names */
  35.   c=1;
  36.   do
  37.   { while(c&&s2[0]=='.'&&(s2[1]=='/'||s2[1]=='\0'))
  38.     { s2++;
  39.       if(*s2=='/')
  40.         s2++;
  41.     }
  42.     *s3++=*s2;
  43.     c=0;
  44.     if(*s2=='/')
  45.       c=1;
  46.   }while(*s2++);
  47.   
  48.   s3=s2=s1; /* Remove double dots '..' as directory names */
  49.   c=1;
  50.   do
  51.   { if(c&&s2[0]=='.'&&s2[1]=='.')
  52.     { if(s2[2]=='/')
  53.         s2+=2; 
  54.       else if(s2[2]=='\0')
  55.       { *s3++='/';
  56.         s2+=2; }
  57.     }
  58.     *s3++=*s2;
  59.     c=0;
  60.     if(*s2=='/')
  61.       c=1;
  62.   }while(*s2++);
  63.  
  64.   if(*s1=='/') /* Convert names beginning with '/' */
  65.   { s3=s2=s1;
  66.     s2++;
  67.     if(*s2=='/'||*s2=='\0') /* The root directory */
  68.       return "SYS:";
  69.     while(*s2!='/'&&*s2!='\0')
  70.       *s3++=*s2++;
  71.     *s3++=':';
  72.     if(*s2=='/')
  73.       s2++;
  74.     do
  75.       *s3++=*s2;
  76.     while(*s2++);
  77.   }
  78.  
  79.   return s1;
  80. }
  81.  
  82. void __initamigapath(void)
  83. { char *s;
  84.   s=getenv("NOIXPATHS"); /* Check explicitly for "1", so we can override it locally */
  85.   if(s&&s[0]=='1'&&s[1]=='\0') /* with 'set NOIXPATHS 0' */
  86.     __pathconv=1;
  87. }
  88.  
  89. ADD2INIT(__initamigapath,-10);
  90.