MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / dfs_normalize_path

Function dfs_normalize_path

components/dfs/dfs_v2/src/dfs.c:947–1097  ·  view source on GitHub ↗

* @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

Source from the content-addressed store, hash-verified

945 * - Allocate memory for the returned path string
946 */
947char *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

Callers 15

chdirFunction · 0.70
_dfs_normalize_pathFunction · 0.70
dfs_file_openFunction · 0.70
dfs_file_statFunction · 0.70
dfs_file_lstatFunction · 0.70
dfs_file_setattrFunction · 0.70
dfs_file_unlinkFunction · 0.70
dfs_file_linkFunction · 0.70
dfs_file_symlinkFunction · 0.70
dfs_file_readlinkFunction · 0.70
dfs_file_renameFunction · 0.70
dfs_file_isdirFunction · 0.70

Calls 6

lwp_getcwdFunction · 0.85
rt_kprintfFunction · 0.85
rt_mallocFunction · 0.85
rt_snprintfFunction · 0.85
rt_strdupFunction · 0.85
rt_freeFunction · 0.85

Tested by

no test coverage detected