Advertisement
iocoder

reduce_path();

Nov 25th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. /*
  2.       +----------------------------------------------------------+
  3.       | +------------------------------------------------------+ |
  4.       | |  Quafios Kernel 1.0.1.                   | |
  5.       | |  -> File System: Path Processing Procedures.     | |
  6.       | +------------------------------------------------------+ |
  7.       +----------------------------------------------------------+
  8. */
  9.  
  10. // This file is part of Quafios 1.0.1 source code.
  11. // Copyright (C) 2012  Mostafa Abd El-Aziz Mohamed.
  12.  
  13. // This program is free software: you can redistribute it and/or modify
  14. // it under the terms of the GNU General Public License as published by
  15. // the Free Software Foundation, either version 3 of the License, or
  16. // (at your option) any later version.
  17.  
  18. // This program is distributed in the hope that it will be useful,
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. // GNU General Public License for more details.
  22.  
  23. // You should have received a copy of the GNU General Public License
  24. // along with Quafios.  If not, see <http://www.gnu.org/licenses/>.
  25.  
  26. // Visit http://www.quafios.com/ for contact information.
  27.  
  28. String reduce_path(String pathname) {
  29.  
  30.     // convert /////home////george///././////../////george////xyz////
  31.     // into /home/george/xyz
  32.  
  33.     // (I) Temporary Allocate Memory for Operation:
  34.     // ---------------------------------------------
  35.     String tmp;
  36.     // calculate size needed for allocation.. +1 in (cwd->size) is for the slash /
  37.     // between cwd & pathname.
  38.     tmp.size = pathname.size + (pathname.s[0] != '/' ? (cwd->size+1) : 0);
  39.     tmp.s = kmalloc(tmp.size+1);
  40.     if (tmp.s == (char *) NULLPTR) return tmp; // No Memory!
  41.  
  42.     // (II) Copy to tmp, with reduction:
  43.     // ----------------------------------
  44.     int i, j = tmp.size, f = 1, skip = 0;
  45.    
  46.     tmp.s[j--] = 0;
  47.    
  48.     for (i = pathname.size-1; i >= (signed int) (pathname.size-tmp.size); i--) {
  49.        
  50.         // get a char to process:
  51.         #define charAt(index) ((index) >= 0 ? pathname.s[(index)] : \
  52.                     ((index) == -1 ? '/':cwd->s[cwd->size+1+(index)]))
  53.         unsigned char x = charAt(i);
  54.        
  55.         if (x == '/') {
  56.             // slash?
  57.             if (!f) {
  58.                 // f is a flag to prevent repetitive slashes. ////home -> /home
  59.                 // and trailing slashes.
  60.                 f = 1;
  61.                 if (skip)
  62.                     skip--; // skip is used to handle . && ..
  63.                 else
  64.                     tmp.s[j--] = '/';
  65.             }
  66.         } else if (f && x == '.' && charAt(i-1) == '.' && charAt(i-2) == '/') {
  67.             // .. link
  68.             i--;
  69.             skip++;
  70.         } else if (f && x == '.' && charAt(i-1) == '/') {
  71.             // . link, has no effect.
  72.         } else {
  73.             f = 0;  // slashes are gone away :D
  74.             if (!skip) tmp.s[j--] = x;
  75.         }
  76.     }
  77.  
  78.     // (III) Allocate space for the return String:
  79.     // -------------------------------------------
  80.     String ret;
  81.     ret.size = tmp.size - ++j;
  82.     ret.s = kmalloc(ret.size+1);
  83.  
  84.     // (IV) Copy Final Data:
  85.     // -----------------------
  86.     if (ret.s != (char *) NULLPTR)
  87.         for (i = 0; i <= ret.size; i++, j++)
  88.             ret.s[i] = tmp.s[j]; // including \0
  89.  
  90.     // (V) Deallocate Temporary Structures and exit:
  91.     // -----------------------------------------------
  92.     kfree(tmp.s);
  93.     return ret;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement