/*--------------------------------------------------------------------------*/ char *get_full_path(char *_FullPath, const char *_Path, size_t _SizeInBytes) { #if defined(_MSC_VER) char *returnedFullPath = NULL; wchar_t *wPath = to_wide_string((char *)_Path); wchar_t *wFullPath = (wchar_t *) MALLOC(sizeof(wchar_t) * _SizeInBytes); _wfullpath(wFullPath, wPath, _SizeInBytes); returnedFullPath = wide_string_to_UTF8(wFullPath); if (returnedFullPath) { strcpy(_FullPath, returnedFullPath); FREE(returnedFullPath); returnedFullPath = NULL; } if (wPath) { FREE(wPath); wPath = NULL; } if (wFullPath) { FREE(wFullPath); wFullPath = NULL; } return _FullPath; #else char *rp = NULL; int lenPath = (int)strlen(_Path); rp = realpath(_Path, _FullPath); int lenFullPath = 0; int haveFileSep = ((lenPath > 1) && isDirSeparator(_Path[lenPath - 1])); int addFileSep = 0; if (!rp) { strcpy(_FullPath, _Path); normalizePath(_FullPath); } lenFullPath = (int)strlen(_FullPath); addFileSep = ((lenFullPath > 1) && (!isDirSeparator(_FullPath[lenFullPath - 1])) && haveFileSep); if (addFileSep) { char *bufTmp = (char *)MALLOC(sizeof(char) * (lenFullPath + strlen(DIR_SEPARATOR) + 1)); if (bufTmp) { sprintf(bufTmp, "%s%s", _FullPath, DIR_SEPARATOR); strcpy(_FullPath, bufTmp); FREE(bufTmp); bufTmp = NULL; } } return _FullPath; #endif }
/* Check if the program name begins with "/" on unix/cygwin, or * with "\" or "X:\" on windows. If not, the program name * is relative to the current directory. */ bool Path::isAbsolute(const char *path) { if (isDirSeparator(path[0])) { return true; } #ifdef GMX_NATIVE_WINDOWS return path[0] != '\0' && path[1] == ':' && isDirSeparator(path[2]); #else return false; #endif }
//------------------------------------------------------------------------------ // format directory name field from a 8.3 name string bool FatFile::parsePathName(const char* path, fname_t* fname, const char** ptr) { uint8_t uc = 0; uint8_t lc = 0; uint8_t bit = FNAME_FLAG_LC_BASE; // blank fill name and extension for (uint8_t i = 0; i < 11; i++) { fname->sfn[i] = ' '; } for (uint8_t i = 0, n = 7;; path++) { uint8_t c = *path; if (c == 0 || isDirSeparator(c)) { // Done. break; } if (c == '.' && n == 7) { n = 10; // max index for full 8.3 name i = 8; // place for extension // bit for extension. bit = FNAME_FLAG_LC_EXT; } else { if (!legal83Char(c) || i > n) { DBG_FAIL_MACRO; goto fail; } if ('a' <= c && c <= 'z') { c += 'A' - 'a'; lc |= bit; } else if ('A' <= c && c <= 'Z') { uc |= bit; } fname->sfn[i++] = c; } } // must have a file name, extension is optional if (fname->sfn[0] == ' ') { DBG_FAIL_MACRO; goto fail; } // Set base-name and extension bits. fname->flags = lc & uc ? 0 : lc; while (isDirSeparator(*path)) { path++; } *ptr = path; return true; fail: return false; }
/*--------------------------------------------------------------------------*/ static int normalizePath(char *path) { char *dirs[PATH_MAX]; int depth = 0; char *dstptr = path; char *srcptr = path; dirs[0] = path; depth++; while (1) { if ((srcptr[0] == '.') && isDirSeparator(srcptr[1])) { /* ./ */ srcptr += 2; } else if (srcptr[0] == '.' && srcptr[1] == '.' && isDirSeparator(srcptr[2])) { /* ../ */ if (depth == 1) { /* ../ */ dstptr[0] = '.'; dstptr[1] = '.'; dstptr[2] = DIR_SEPARATOR[0]; dstptr += 3; srcptr += 3; dirs[0] = dstptr; } else { /* a/b/../c */ depth--; dstptr = dirs[depth - 1]; srcptr += 3; } } else if (srcptr[0] == '.' && srcptr[1] == '.' && srcptr[2] == 0) { /* .. */ if (depth == 1) { dstptr[0] = '.'; dstptr[1] = '.'; dstptr += 2; srcptr += 2; dirs[0] = dstptr; } else { depth--; dstptr = dirs[depth - 1]; srcptr += 2; } } else { while (!isDirSeparator(srcptr[0]) && srcptr[0]) { *dstptr++ = *srcptr++; } if (srcptr[0] == 0) { if (dstptr != path && isDirSeparator(dstptr[-1])) { dstptr[-1] = 0; } dstptr[0] = 0; return 0; } else if (isDirSeparator(srcptr[0])) { *dstptr++ = *srcptr++; dirs[depth] = dstptr; depth++; /* // */ while (isDirSeparator(srcptr[0]) && srcptr[0]) { srcptr++; } } else { /* error */ return -1; } } } }
/*--------------------------------------------------------------------------*/ char *get_full_path(char *_FullPath, const char *_Path, size_t _SizeInBytes) { #if defined(_MSC_VER) char *returnedFullPath = NULL; wchar_t *wPath = to_wide_string((char *)_Path); wchar_t *wFullPath = (wchar_t *) MALLOC(sizeof(wchar_t) * _SizeInBytes); _wfullpath(wFullPath, wPath, _SizeInBytes); returnedFullPath = wide_string_to_UTF8(wFullPath); if (returnedFullPath) { strcpy(_FullPath, returnedFullPath); FREE(returnedFullPath); returnedFullPath = NULL; } if (wPath) { FREE(wPath); wPath = NULL; } if (wFullPath) { FREE(wFullPath); wFullPath = NULL; } return _FullPath; #else char *rp = NULL; int lenPath = (int)strlen(_Path); int lenFullPath = 0; int haveFileSep = ((lenPath > 1) && isDirSeparator(_Path[lenPath - 1])); int addFileSep = 0; rp = realpath(_Path, _FullPath); lenFullPath = (int)strlen(_FullPath); if (rp == NULL) { char * tofind; char * toadd; char * _Path_tmp; char * _Path_start; char * _FullPath_start; char* pstWorkingPath = NULL; //if argument is a relative path, add currentdir at start if (_Path[0] != '/') { int ierr = 0; char* pstCurrentPath = scigetcwd(&ierr); //alloc buffer + 2, 1 for '/' and 1 for null termination pstWorkingPath = (char*)CALLOC(sizeof(char), (lenPath + strlen(pstCurrentPath) + 2)); sprintf(pstWorkingPath, "%s/%s", pstCurrentPath, _Path); lenPath = strlen(pstWorkingPath); FREE(pstCurrentPath); } else { pstWorkingPath = strdup(_Path); } _Path_tmp = (char *)MALLOC(sizeof(char) * (lenPath + 1)); _Path_start = (char *)MALLOC(sizeof(char) * (lenPath + 1)); _FullPath_start = (char *)MALLOC(sizeof(char) * (lenFullPath + 1)); //First case(1): fullpath(TMPDIR+"/a/b/c"), second case(2): fullpath("a/b/c") or third case(3): fullpath("../a/b") strcpy(_Path_start, pstWorkingPath); // _Path_start=TMPDIR+"/a/b/c" (1) or _Path_start="a/b/c" (2) or _Path_start="../a/b/c" (3) strcpy(_FullPath_start, _FullPath); // _Fullpath_Start=TMPDIR+"/a" (1) or _FullPath_start=SCI+"/a" (2) or _FullPath_start=../SCI+"/a" (3) strtok(_Path_start, "/"); // _Path_start=/tmp (1) or _Path_start="a" (2) or _Path_start="a/b/c" (3) strtok(_FullPath_start, "/"); // _FullPath_start=/tmp (1) or _FullPath_start=/home (2) and (3) #if defined(__APPLE__) if (strcmp(_FullPath_start, "/private") == 0) // For case: fullpath(TMPDIR+"/a/b/c") (1) { normalizePath(_FullPath); } #else if (strcmp(_Path_start, _FullPath_start) == 0) // For case: fullpath(TMPDIR+"/a/b/c") (1) { strcpy(_FullPath, pstWorkingPath); normalizePath(_FullPath); } #endif else if (strcmp(_Path, _FullPath) != 0) // For case: fullpath("a/b/c") (2) or fullpath("../a/b/c") (3) { strcpy(_Path_tmp, pstWorkingPath); //_Path_tmp="a/b/c" (2) or _Path_tmp="../a/b/c" (3) strtok(_Path_tmp, "./"); // _Path_tmp becomes a (2) or ../a (3) toadd = strsub(pstWorkingPath, _Path_tmp, ""); // to add = "/b/c" strcat(_FullPath, toadd); //_FullPath=_Fullpath+toadd FREE(toadd); } FREE(pstWorkingPath); FREE(_FullPath_start); FREE(_Path_start); FREE(_Path_tmp); } lenFullPath = (int)strlen(_FullPath); addFileSep = ((lenFullPath > 1) && (!isDirSeparator(_FullPath[lenFullPath - 1])) && haveFileSep); if (addFileSep) { char *bufTmp = (char *)MALLOC(sizeof(char) * (lenFullPath + strlen(DIR_SEPARATOR) + 1)); if (bufTmp) { sprintf(bufTmp, "%s%s", _FullPath, DIR_SEPARATOR); strcpy(_FullPath, bufTmp); FREE(bufTmp); bufTmp = NULL; } } return _FullPath; #endif }