Advertisement
iocoder

temporary

Nov 25th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1.  
  2. path_t* to_path(char *path) {
  3. /*
  4.     unsigned int i = 0;
  5.     unsigned int j = 0;
  6.     unsigned int flag = 0;
  7.     unsigned int size = 0;
  8.  
  9.     // (I) Calculate size needed for the path:
  10.     // ----------------------------------------
  11.     // remove repetitive slashes ///home//iocoder -> /home/iocoder
  12.     do {
  13.         // Terminator?
  14.         if (!path[i]) break;
  15.  
  16.         if (path[i] == '/') {
  17.             if (!flag) {
  18.                 flag = 1;
  19.                 size++;
  20.             }
  21.         } else {
  22.             size++;
  23.             flag = 0;
  24.         }
  25.     } while (++i);
  26.  
  27.     // (II) Remove trailing slash:
  28.     // ---------------------------
  29.     if (flag) size--;
  30.  
  31.     // (III) Path Relative to Current Working Directory?
  32.     // --------------------------------------------------
  33.     if (path[0] != '/') {
  34.         if (size)
  35.             size += cwd->size + 1; // +1 for the slash between cwd & path.
  36.         else
  37.             size = cwd->size; // path = cwd.
  38.     }
  39.  
  40.     // (IV) Allocate Memory:
  41.     // ----------------------
  42.     path_t *ret = (path_t *) kmalloc(sizeof(path_t) + size);
  43.     if (ret == (path_t *) NULLPTR) return ret; // return NULLPTR;
  44.  
  45.     // (V) Insert Data:
  46.     // -------------------
  47.     i = 0, j = 0;
  48.  
  49.     // Insert info:
  50.     ret->size = size;
  51.  
  52.     // Copy cwd if needed:
  53.     if (path[0] != '/') {
  54.         for (; i < cwd->size; i++)
  55.             ret->s[i] = cwd->s[i];
  56.  
  57.         // The slash between cwd & path:
  58.         if (i < size)
  59.             ret->s[i++] = '/';
  60.     }
  61.    
  62.     // Copy path string:
  63.     while(i < size) {
  64.         ret->s[i++] = path[j++];
  65.         if (path[j-1] == '/')
  66.             while(path[j] == '/') j++;
  67.     }
  68.  
  69.     // trailing \0:
  70.     ret->s[i] = 0;
  71.  
  72.     // (VI) Return:
  73.     // -------------
  74.     return ret;
  75. */
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement