diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index 5100be8b..0b453293 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -74,11 +74,6 @@ Copyright (C) 1991, 1992, 1997, 1998, 1999, 2000, 2001, 2002, 2003, GNU GPL version 2 or later. -realpath.c -========== - -Portions from GeekGadgets for Amiga. Copyright and license unknown. - recoil.{c,h} ============ diff --git a/src/io.c b/src/io.c index 55603496..ec17f00b 100644 --- a/src/io.c +++ b/src/io.c @@ -342,7 +342,7 @@ char * Extract_path(const char *source) char * position; char * path; - path = Realpath(source, NULL); + path = Realpath(source); if (path == NULL) { GFX2_Log(GFX2_ERROR, "Realpath(\"%s\") failed !\n", source); @@ -603,7 +603,7 @@ void For_each_file(const char * directory_name, void Callback(const char *, cons char * search_string; HANDLE h; - full_filename = Realpath(directory_name, NULL); + full_filename = Realpath(directory_name); search_string = Filepath_append_to_dir((full_filename != NULL) ? full_filename : directory_name, "*"); free(full_filename); h = FindFirstFileA(search_string, &fd); @@ -1151,10 +1151,10 @@ char * Calculate_relative_path(const char * ref_path, const char * path) if (ref_path == NULL || path == NULL) return NULL; - real_ref_path = Realpath(ref_path, NULL); + real_ref_path = Realpath(ref_path); if (real_ref_path == NULL) real_ref_path = strdup(ref_path); - real_path = Realpath(path, NULL); + real_path = Realpath(path); if (real_path == NULL) real_path = strdup(path); #if defined(WIN32) || defined(__MINT__) diff --git a/src/main.c b/src/main.c index d3612b22..08c6ff17 100644 --- a/src/main.c +++ b/src/main.c @@ -536,7 +536,7 @@ int Analyze_command_line(int argc, char * argv[], char * filenames[], char * dir } else if (File_exists(argv[index])) { - buffer = Realpath(argv[index], NULL); + buffer = Realpath(argv[index]); filename = Find_last_separator(buffer); if (filename != NULL) { diff --git a/src/readini.c b/src/readini.c index 8e86932d..211fba06 100644 --- a/src/readini.c +++ b/src/readini.c @@ -998,7 +998,7 @@ int Load_INI(T_Config * conf) if (conf->Scripts_directory == NULL) { // Default when empty: - char * path = Realpath(Data_directory, NULL); + char * path = Realpath(Data_directory); conf->Scripts_directory = Filepath_append_to_dir(path, SCRIPTS_SUBDIRECTORY); free(path); } diff --git a/src/realpath.c b/src/realpath.c index 2b2c004e..6861e4e5 100644 --- a/src/realpath.c +++ b/src/realpath.c @@ -17,115 +17,21 @@ #if defined(__AROS__) || defined(__BEOS__) || defined(__MORPHOS__) || defined(__GP2X__) || defined(__WIZ__) || defined(__CAANOO__) || defined(__amigaos__) || defined(__SWITCH__) // These platforms don't have realpath(). -// We use the following implementation, found in: -// http://amiga.sourceforge.net/amigadevhelp/FUNCTIONS/GeekGadgets/realpath/ex02_realpath.c -// -// When tested on Debian, this piece of code doesn't resolve -// symbolic link in the filename itself, only on the directories in -// the path. So this implementation is limited, it's really better to -// use realpath() if your platform has it. - - #if !defined(PATH_MAX) - // This is a random default value ... - #define PATH_MAX 4096 - #endif - - static char *sep(char *path) - { - char *tmp, c; - - tmp = strrchr(path, '/'); - if(tmp) { - c = tmp[1]; - tmp[1] = 0; - if (chdir(path)) { - return NULL; - } - tmp[1] = c; - - return tmp + 1; - } - return path; - } - - // Find the real path of _path by chdir to it and then getcwd. - // If resolved_path is null, it is allocated. - char *Realpath(const char *_path, char *resolved_path) - { - #if defined(__AROS__) - int fd = open("", O_RDONLY); // GrafX2 is compiled without Unix support - #else - int fd = open(".", O_RDONLY); - #endif - int l; - char current_dir_path[PATH_MAX]; - char path[PATH_MAX], lnk[PATH_MAX], *tmp = (char *)""; - - if (fd < 0) { - return NULL; - } - getcwd(current_dir_path,PATH_MAX); - strncpy(path, _path, PATH_MAX); - - if (chdir(path)) { - if (errno == ENOTDIR) { - #if defined(__MORPHOS__) || defined(__amigaos__) || defined(__SWITCH__) - // No symbolic links and no readlink() - l = -1; - #else - l = readlink(path, lnk, PATH_MAX); - #endif - if (!(tmp = sep(path))) { - resolved_path = NULL; - goto abort; - } - if (l < 0) { - if (errno != EINVAL) { - resolved_path = NULL; - goto abort; - } - } else { - lnk[l] = 0; - if (!(tmp = sep(lnk))) { - resolved_path = NULL; - goto abort; - } - } - } else { - resolved_path = NULL; - goto abort; - } - } - - if(resolved_path==NULL) // if we called realpath with null as a 2nd arg - resolved_path = (char*) malloc( PATH_MAX ); - - if (!getcwd(resolved_path, PATH_MAX)) { - resolved_path = NULL; - goto abort; - } - - if(strcmp(resolved_path, "/") && *tmp) { - strcat(resolved_path, "/"); - } - - strcat(resolved_path, tmp); - abort: - chdir(current_dir_path); - close(fd); - return resolved_path; - } +char *Realpath(const char *_path) +{ + return strdup(_path); +} #elif defined(__WIN32__) || defined(WIN32) -// Mingw has a working equivalent. It only has reversed arguments. - char *Realpath(const char *_path, char *resolved_path) - { - return _fullpath(resolved_path,_path,260); - } +// MSVC / Mingw has a working equivalent. It only has reversed arguments. +char *Realpath(const char *_path) +{ + return _fullpath(NULL, _path, 260); +} #else // Use the stdlib function. - char *Realpath(const char *_path, char *resolved_path) + char *Realpath(const char *_path) { /// POSIX 2004 states : /// If resolved_name is a null pointer, the behavior of realpath() @@ -139,14 +45,12 @@ /// So we assume all platforms now support passing NULL. /// If you find a platform where this is not the case, /// please add a new implementation with ifdef's. + char * resolved_path = NULL; #if defined(__APPLE__) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1060) // realpath() accept NULL as 2nd argument since OSX 10.6 + resolved_path = GFX2_malloc(PATH_MAX); if (resolved_path == NULL) - { - resolved_path = GFX2_malloc(PATH_MAX); - if (resolved_path == NULL) - return NULL; - } + return NULL; #endif return realpath(_path, resolved_path); } diff --git a/src/realpath.h b/src/realpath.h index 63151ff9..642f3a4a 100644 --- a/src/realpath.h +++ b/src/realpath.h @@ -31,6 +31,6 @@ /// @param _path Input path /// @param resolved_path Output path, allocated by caller /// @return (points to resolved_path) -char *Realpath(const char *_path, char *resolved_path); +char *Realpath(const char *_path); #endif diff --git a/src/tests/testio.c b/src/tests/testio.c index 5061a3db..84d4f0fa 100644 --- a/src/tests/testio.c +++ b/src/tests/testio.c @@ -308,7 +308,7 @@ int Test_Realpath(char * errmsg) { char * path; - path = Realpath(tmpdir, NULL); + path = Realpath(tmpdir); if (path == NULL) { snprintf(errmsg, ERRMSG_LENGTH, "Realpath(\"%s\") returned NULL", tmpdir);