Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. extern char __curdir_buf[1024];
  6.  
  7. char* __get_curdir(void) {return __curdir_buf;}
  8.  
  9. static void ___chdir(char* dest, const char* src)
  10. {
  11.         // handle absolute paths
  12.         if (src[0]=='/')
  13.         {
  14.                 strcpy(dest,src);
  15.                 return;
  16.         }
  17.         // handle relative paths
  18.         char* ptr = dest + strlen(dest);
  19.         while (*src)
  20.         {
  21.                 if (src[0] == '.' && src[1] == 0)
  22.                         break;
  23.                 if (src[0] == '.' && src[1] == '/')
  24.                 {++src;++src;continue;}
  25.                 if (src[0] == '.' && src[1] == '.' &&
  26.                         (src[2] == 0 || src[2] == '/'))
  27.                 {
  28.                         while (ptr > dest && ptr[-1] != '/')
  29.                                 --ptr;
  30.                         ++src;++src;
  31.                         if (*src == 0) break;
  32.                         ++src;
  33.                         continue;
  34.                 }
  35.                 *ptr++ = '/';
  36.                 if (*src == '/') ++src;
  37.                 while (*src && *src!='/') *ptr++ = *src++;
  38.         }
  39.         *ptr = 0;
  40. }
  41.  
  42. void __chdir(const char* path)
  43. {
  44.         ___chdir(__curdir_buf,path);
  45. }
  46.  
  47. static char __libc_combine_buffer[1024];
  48. char* __libc_combine_path(const char* c)
  49. {
  50.         strcpy(__libc_combine_buffer,__curdir_buf);
  51.         ___chdir(__libc_combine_buffer,c);
  52.         return __libc_combine_buffer;
  53. }
  54.  
  55.  
  56. #ifdef __TEST_IN_DOS__
  57.  
  58. int main(void)
  59. {
  60.  init_dir_stack();
  61.  printf("|%s|\n",__get_curdir());
  62.  __chdir("jp/1/2/3");
  63.  printf("|%s|\n",__get_curdir());
  64.  __chdir("/jp/1/2/3");
  65.  printf("|%s|\n",__get_curdir());
  66.  __chdir("../4");
  67.  printf("|%s|\n",__get_curdir());
  68.  __chdir("./../..");
  69.  printf("|%s|\n",__get_curdir());
  70.  printf("Combined=|%s|\n",combine_path("./abc/def/../../../rd/2"));
  71.  return 0;
  72. }
  73. #endif
  74.