* this function will normalize a path according to specified parent directory * and file name. * * @param directory the parent path * @param filename the file name * * @return the built full file path (absolute path) */
| 748 | * @return the built full file path (absolute path) |
| 749 | */ |
| 750 | char *dfs_normalize_path(const char *directory, const char *filename) |
| 751 | { |
| 752 | char *fullpath; |
| 753 | char *dst0, *dst, *src; |
| 754 | |
| 755 | /* check parameters */ |
| 756 | RT_ASSERT(filename != NULL); |
| 757 | |
| 758 | #ifdef DFS_USING_WORKDIR |
| 759 | if (directory == NULL) /* shall use working directory */ |
| 760 | { |
| 761 | #ifdef RT_USING_SMART |
| 762 | directory = lwp_getcwd(); |
| 763 | #else |
| 764 | directory = &working_directory[0]; |
| 765 | #endif |
| 766 | } |
| 767 | #else |
| 768 | if ((directory == NULL) && (filename[0] != '/')) |
| 769 | { |
| 770 | rt_kprintf(NO_WORKING_DIR); |
| 771 | |
| 772 | return NULL; |
| 773 | } |
| 774 | #endif |
| 775 | |
| 776 | if (filename[0] != '/') /* it's a absolute path, use it directly */ |
| 777 | { |
| 778 | fullpath = (char *)rt_malloc(strlen(directory) + strlen(filename) + 2); |
| 779 | |
| 780 | if (fullpath == NULL) |
| 781 | return NULL; |
| 782 | |
| 783 | /* join path and file name */ |
| 784 | rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, |
| 785 | "%s/%s", directory, filename); |
| 786 | } |
| 787 | else |
| 788 | { |
| 789 | fullpath = rt_strdup(filename); /* copy string */ |
| 790 | |
| 791 | if (fullpath == NULL) |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | src = fullpath; |
| 796 | dst = fullpath; |
| 797 | |
| 798 | dst0 = dst; |
| 799 | while (1) |
| 800 | { |
| 801 | char c = *src; |
| 802 | |
| 803 | if (c == '.') |
| 804 | { |
| 805 | if (!src[1]) src++; /* '.' and ends */ |
| 806 | else if (src[1] == '/') |
| 807 | { |
no test coverage detected