Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- path_t* to_path(char *path) {
- /*
- unsigned int i = 0;
- unsigned int j = 0;
- unsigned int flag = 0;
- unsigned int size = 0;
- // (I) Calculate size needed for the path:
- // ----------------------------------------
- // remove repetitive slashes ///home//iocoder -> /home/iocoder
- do {
- // Terminator?
- if (!path[i]) break;
- if (path[i] == '/') {
- if (!flag) {
- flag = 1;
- size++;
- }
- } else {
- size++;
- flag = 0;
- }
- } while (++i);
- // (II) Remove trailing slash:
- // ---------------------------
- if (flag) size--;
- // (III) Path Relative to Current Working Directory?
- // --------------------------------------------------
- if (path[0] != '/') {
- if (size)
- size += cwd->size + 1; // +1 for the slash between cwd & path.
- else
- size = cwd->size; // path = cwd.
- }
- // (IV) Allocate Memory:
- // ----------------------
- path_t *ret = (path_t *) kmalloc(sizeof(path_t) + size);
- if (ret == (path_t *) NULLPTR) return ret; // return NULLPTR;
- // (V) Insert Data:
- // -------------------
- i = 0, j = 0;
- // Insert info:
- ret->size = size;
- // Copy cwd if needed:
- if (path[0] != '/') {
- for (; i < cwd->size; i++)
- ret->s[i] = cwd->s[i];
- // The slash between cwd & path:
- if (i < size)
- ret->s[i++] = '/';
- }
- // Copy path string:
- while(i < size) {
- ret->s[i++] = path[j++];
- if (path[j-1] == '/')
- while(path[j] == '/') j++;
- }
- // trailing \0:
- ret->s[i] = 0;
- // (VI) Return:
- // -------------
- return ret;
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement