* @brief Normalize a path by combining directory and filename into an absolute path * * @param[in] directory The parent directory path (NULL means use working directory) * @param[in] filename The filename or relative path to be normalized * * @return char* The normalized absolute path (must be freed by caller), * NULL if path is invalid or memory allocation fails * * @note This fun
| 945 | * - Allocate memory for the returned path string |
| 946 | */ |
| 947 | char *dfs_normalize_path(const char *directory, const char *filename) |
| 948 | { |
| 949 | char *fullpath; |
| 950 | char *dst0, *dst, *src; |
| 951 | |
| 952 | /* check parameters */ |
| 953 | RT_ASSERT(filename != NULL); |
| 954 | |
| 955 | #ifdef DFS_USING_WORKDIR |
| 956 | if (directory == NULL) /* shall use working directory */ |
| 957 | { |
| 958 | #ifdef RT_USING_SMART |
| 959 | directory = lwp_getcwd(); |
| 960 | #else |
| 961 | directory = &working_directory[0]; |
| 962 | #endif |
| 963 | } |
| 964 | #else |
| 965 | if ((directory == NULL) && (filename[0] != '/')) |
| 966 | { |
| 967 | rt_kprintf(NO_WORKING_DIR); |
| 968 | |
| 969 | return NULL; |
| 970 | } |
| 971 | #endif |
| 972 | |
| 973 | if (filename[0] != '/') |
| 974 | { |
| 975 | int path_len; |
| 976 | |
| 977 | path_len = strlen(directory) + strlen(filename) + 2; |
| 978 | if (path_len > DFS_PATH_MAX) |
| 979 | { |
| 980 | return NULL; |
| 981 | } |
| 982 | |
| 983 | fullpath = (char *)rt_malloc(path_len); |
| 984 | if (fullpath == NULL) |
| 985 | { |
| 986 | return NULL; |
| 987 | } |
| 988 | |
| 989 | /* join path and file name */ |
| 990 | rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, |
| 991 | "%s/%s", directory, filename); |
| 992 | } |
| 993 | else /* it's a absolute path, use it directly */ |
| 994 | { |
| 995 | fullpath = rt_strdup(filename); /* copy string */ |
| 996 | |
| 997 | if (fullpath == NULL) |
| 998 | return NULL; |
| 999 | } |
| 1000 | |
| 1001 | /* Initialize source and destination pointers to start of path */ |
| 1002 | src = fullpath; |
| 1003 | dst = fullpath; |
| 1004 |
no test coverage detected