Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- +----------------------------------------------------------+
- | +------------------------------------------------------+ |
- | | Quafios Kernel 1.0.1. | |
- | | -> File System: Path Processing Procedures. | |
- | +------------------------------------------------------+ |
- +----------------------------------------------------------+
- */
- // This file is part of Quafios 1.0.1 source code.
- // Copyright (C) 2012 Mostafa Abd El-Aziz Mohamed.
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // You should have received a copy of the GNU General Public License
- // along with Quafios. If not, see <http://www.gnu.org/licenses/>.
- // Visit http://www.quafios.com/ for contact information.
- String reduce_path(String pathname) {
- // convert /////home////george///././////../////george////xyz////
- // into /home/george/xyz
- // (I) Temporary Allocate Memory for Operation:
- // ---------------------------------------------
- String tmp;
- // calculate size needed for allocation.. +1 in (cwd->size) is for the slash /
- // between cwd & pathname.
- tmp.size = pathname.size + (pathname.s[0] != '/' ? (cwd->size+1) : 0);
- tmp.s = kmalloc(tmp.size+1);
- if (tmp.s == (char *) NULLPTR) return tmp; // No Memory!
- // (II) Copy to tmp, with reduction:
- // ----------------------------------
- int i, j = tmp.size, f = 1, skip = 0;
- tmp.s[j--] = 0;
- for (i = pathname.size-1; i >= (signed int) (pathname.size-tmp.size); i--) {
- // get a char to process:
- #define charAt(index) ((index) >= 0 ? pathname.s[(index)] : \
- ((index) == -1 ? '/':cwd->s[cwd->size+1+(index)]))
- unsigned char x = charAt(i);
- if (x == '/') {
- // slash?
- if (!f) {
- // f is a flag to prevent repetitive slashes. ////home -> /home
- // and trailing slashes.
- f = 1;
- if (skip)
- skip--; // skip is used to handle . && ..
- else
- tmp.s[j--] = '/';
- }
- } else if (f && x == '.' && charAt(i-1) == '.' && charAt(i-2) == '/') {
- // .. link
- i--;
- skip++;
- } else if (f && x == '.' && charAt(i-1) == '/') {
- // . link, has no effect.
- } else {
- f = 0; // slashes are gone away :D
- if (!skip) tmp.s[j--] = x;
- }
- }
- // (III) Allocate space for the return String:
- // -------------------------------------------
- String ret;
- ret.size = tmp.size - ++j;
- ret.s = kmalloc(ret.size+1);
- // (IV) Copy Final Data:
- // -----------------------
- if (ret.s != (char *) NULLPTR)
- for (i = 0; i <= ret.size; i++, j++)
- ret.s[i] = tmp.s[j]; // including \0
- // (V) Deallocate Temporary Structures and exit:
- // -----------------------------------------------
- kfree(tmp.s);
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement