| 259 | } |
| 260 | |
| 261 | char* CanonicalizePath(const char* path, char* workingDir){ |
| 262 | char* tempPath; |
| 263 | if(workingDir && path[0] != '/'){ |
| 264 | tempPath = (char*)kmalloc(strlen(path) + strlen(workingDir) + 2); |
| 265 | strcpy(tempPath, workingDir); |
| 266 | strcpy(tempPath + strlen(tempPath), "/"); |
| 267 | strcpy(tempPath + strlen(tempPath), path); |
| 268 | } else { |
| 269 | tempPath = (char*)kmalloc(strlen(path) + 1); |
| 270 | strcpy(tempPath, path); |
| 271 | } |
| 272 | |
| 273 | char* file = strtok(tempPath,"/"); |
| 274 | List<char*>* tokens = new List<char*>(); |
| 275 | |
| 276 | while(file != NULL){ |
| 277 | tokens->add_back(file); |
| 278 | file = strtok(NULL, "/"); |
| 279 | } |
| 280 | |
| 281 | int newLength = 2; // Separator and null terminator |
| 282 | newLength += strlen(path) + strlen(workingDir); |
| 283 | for(unsigned i = 0; i < tokens->get_length(); i++){ |
| 284 | if(strlen(tokens->get_at(i)) == 0){ |
| 285 | tokens->remove_at(i--); |
| 286 | continue; |
| 287 | } else if(strcmp(tokens->get_at(i), ".") == 0){ |
| 288 | tokens->remove_at(i--); |
| 289 | continue; |
| 290 | } else if(strcmp(tokens->get_at(i), "..") == 0){ |
| 291 | if(i){ |
| 292 | tokens->remove_at(i); |
| 293 | tokens->remove_at(i - 1); |
| 294 | i -= 2; |
| 295 | } else tokens->remove_at(i); |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | newLength += strlen(tokens->get_at(i)) + 1; // Name and separator |
| 300 | } |
| 301 | |
| 302 | char* outPath = (char*)kmalloc(newLength); |
| 303 | outPath[0] = 0; |
| 304 | |
| 305 | if(!tokens->get_length()) strcpy(outPath + strlen(outPath), "/"); |
| 306 | else for(unsigned i = 0; i < tokens->get_length(); i++){ |
| 307 | strcpy(outPath + strlen(outPath), "/"); |
| 308 | strcpy(outPath + strlen(outPath), tokens->get_at(i)); |
| 309 | } |
| 310 | |
| 311 | kfree(tempPath); |
| 312 | delete tokens; |
| 313 | |
| 314 | return outPath; |
| 315 | } |
| 316 | |
| 317 | char* BaseName(const char* path){ |
| 318 | char* pathCopy = (char*)kmalloc(strlen(path) + 1); |