/* * normalize: normalize path name * * i) path path name * i) root root of project (must be end with a '/') * i) cwd current directory * o) result normalized path name * i) size size of the result * r) ==NULL: error * !=NULL: result */ char * normalize(const char *path, const char *root, const char *cwd, char *result, const int size) { char *p, abs[MAXPATHLEN]; if (normalize_pathname(path, result, size) == NULL) goto toolong; if (isabspath(path)) { if (strlen(result) > MAXPATHLEN) goto toolong; strcpy(abs, result); } else { if (rel2abs(result, cwd, abs, sizeof(abs)) == NULL) goto toolong; } /* * Remove the root part of path and insert './'. * rootdir /a/b/ * path /a/b/c/d.c -> c/d.c -> ./c/d.c */ p = locatestring(abs, root, MATCH_AT_FIRST); if (p == NULL) return NULL; strlimcpy(result, "./", size); strlimcpy(result + 2, p, size - 2); return result; toolong: die("path name is too long."); }
// able to store a string of PATH_MAX length. char *full_gamedir_path(const char *path, char *fullpath) { char buf[PATH_MAX]; // Build pathname from filename, plus gamedir if relative path. if(is_absolute_path(path)) STRNCPY(buf, path, sizeof(buf)); else snprintf(buf, sizeof(buf), "%s/%s", GameDLL.gamedir, path); // Remove relative path components, if possible. if(!realpath(buf, fullpath)) { META_DEBUG(4, ("Unable to get realpath for '%s': %s", buf, str_os_error())); STRNCPY(fullpath, path, PATH_MAX); } // Replace backslashes, etc. normalize_pathname(fullpath); return(fullpath); }
/** * @details * normalize: normalize path name * * @param[in] path path name * @param[in] root root of project (@STRONG{must be end with a '/'}) * @param[in] cwd current directory * @param[out] result normalized path name * @param[in] size size of the @a result * @return ==NULL: error <br> * !=NULL: @a result * * @note Calls die() if the result path name is too long (#MAXPATHLEN). */ char * normalize(const char *path, const char *root, const char *cwd, char *result, const int size) { char *p, abs[MAXPATHLEN]; if (normalize_pathname(path, result, size) == NULL) goto toolong; if (isabspath(path)) { if (strlen(result) > MAXPATHLEN) goto toolong; strcpy(abs, result); } else { if (rel2abs(result, cwd, abs, sizeof(abs)) == NULL) goto toolong; } /* * Remove the root part of path and insert './'. * rootdir /a/b/ * path /a/b/c/d.c -> c/d.c -> ./c/d.c */ p = locatestring(abs, root, MATCH_AT_FIRST); if (p == NULL) { p = locatestring(root, abs, MATCH_AT_FIRST); /* * abs == /usr/src should be considered to be equal to root == /usr/src/. */ if (p && !strcmp(p, "/")) result[0] = '\0'; else return NULL; } strlimcpy(result, "./", size); strlimcpy(result + 2, p, size - 2); return result; toolong: die("path name is too long."); }