From e2c1805d64cdce90de71e51851a712c666abe2f8 Mon Sep 17 00:00:00 2001 From: Alexpux Date: Mon, 4 Aug 2014 15:26:27 +0400 Subject: [PATCH] pathtools: Split sources. --- mingw-w64-pathtools/main.c | 89 ++++++++++++++++++++++++++ mingw-w64-pathtools/pathtools.c | 103 +----------------------------- mingw-w64-pathtools/pathtools.h | 62 ++++++++++++++++++ mingw-w64-pathtools/pathtools.qbs | 2 +- 4 files changed, 154 insertions(+), 102 deletions(-) create mode 100644 mingw-w64-pathtools/main.c create mode 100644 mingw-w64-pathtools/pathtools.h diff --git a/mingw-w64-pathtools/main.c b/mingw-w64-pathtools/main.c new file mode 100644 index 0000000000..aa34d6f6e8 --- /dev/null +++ b/mingw-w64-pathtools/main.c @@ -0,0 +1,89 @@ +/* + .Some useful path tools. + Written by Ray Donnelly in 2014. + Licensed under CC0. No warranty. + */ + +#include "pathtools.h" + +#define X509_PRIVATE_DIR "/mingw64/ssl/private" + +const char * +X509_get_default_private_dir(void) +{ +#if defined(__MINGW32__) + static char * stored_path[PATH_MAX]; + static int stored = 0; + if (stored == 0) + { + char const * relocated = get_relocated_single_path (X509_PRIVATE_DIR); + strncpy (stored_path, relocated, PATH_MAX); + stored_path[PATH_MAX-1] = '\0'; + free ((void *)relocated); + stored = 1; + } + return stored_path; +#else + return (X509_PRIVATE_DIR); +#endif +} + +int main(int argc, char *argv[]) +{ +#define BINDIR "/mingw64/bin" +#define DATADIR "/mingw64/share" + + char exe_path[PATH_MAX]; + get_executable_path (argv[0], &exe_path[0], sizeof(exe_path)/sizeof(exe_path[0])); + printf ("executable path is %s\n", exe_path); + + char * rel_to_datadir = get_relative_path (BINDIR, DATADIR); + if (strrchr (exe_path, '/') != NULL) + { + strrchr (exe_path, '/')[1] = '\0'; + } + strcat (exe_path, rel_to_datadir); + simplify_path (&exe_path[0]); + printf("real path of DATADIR is %s\n", exe_path); + + if (argc >= 2) + { + get_relative_path_debug (argv[argc-2], argv[argc-1], 0); + } + get_relative_path_debug ("/a/b/c/d", "/a/b/c", ".."); + get_relative_path_debug ("/a/b/c/d/", "/a/b/c/", "../"); + get_relative_path_debug ("/", "/", "/"); + get_relative_path_debug ("/a/testone/c/d", "/a/testtwo/c", "../../../testtwo/c"); + get_relative_path_debug ("/a/testone/c/d/", "/a/testtwo/c/", "../../../testtwo/c/"); + get_relative_path_debug ("/home/part2/part3/part4", "/work/proj1/proj2", "../../../../work/proj1/proj2"); + + simplify_path_debug ("a/b/..", "a"); + simplify_path_debug ("a/b/c/../../", "a/"); + simplify_path_debug ("a/../a/..", ""); + simplify_path_debug ("../a/../a/", "../a/"); + + simplify_path_debug ("./././", "./"); + simplify_path_debug ("/test/", "/test/"); + simplify_path_debug (".", "."); + simplify_path_debug ("..", ".."); + simplify_path_debug ("../", "../"); + simplify_path_debug ("././.", "."); + simplify_path_debug ("../..", "../.."); + simplify_path_debug ("/", "/"); + simplify_path_debug ("./test/", "./test/"); + simplify_path_debug ("./test", "./test"); + simplify_path_debug ("/test", "/test"); + simplify_path_debug ("../test", "../test"); + simplify_path_debug ("../../test", "../../test"); + simplify_path_debug ("../test/..", ".."); + simplify_path_debug (".././../", "../../"); + + sanitise_path_debug ("C:\\windows\\path", "C:/windows/path"); + sanitise_path_debug ("", ""); + sanitise_path_debug ("\\\\", "/"); + + char const * win_path = X509_get_default_private_dir (); + printf ("%s -> %s\n", X509_PRIVATE_DIR, win_path); + + return 0; +} diff --git a/mingw-w64-pathtools/pathtools.c b/mingw-w64-pathtools/pathtools.c index 86aa4bc780..94ac5bf49e 100644 --- a/mingw-w64-pathtools/pathtools.c +++ b/mingw-w64-pathtools/pathtools.c @@ -4,34 +4,7 @@ Licensed under CC0. No warranty. */ -#if defined(__APPLE__) -#include -#else -#include -#endif -#include -#include -#include -#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__) -#include -#endif -#include - -/* If you don't define this, then get_executable_path() - can only use argv[0] which will often not work well */ -#define IMPLEMENT_SYS_GET_EXECUTABLE_PATH - -#if defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) -#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__) -/* Nothing needed, unistd.h is enough. */ -#elif defined(__APPLE__) -#include -#elif defined(_WIN32) -#define WIN32_MEAN_AND_LEAN -#include -#include -#endif -#endif /* defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) */ +#include "pathtools.h" static char * malloc_copy_string (char const * original) @@ -337,7 +310,7 @@ get_executable_path(char const * argv0, char * result, ssize_t max_size) #elif defined(_WIN32) unsigned long bufsize = (unsigned long)max_size; system_result_size = GetModuleFileNameA(NULL, system_result, bufsize); - if (system_result_size == 0 || system_result_size == bufsize) + if (system_result_size == 0 || system_result_size == (ssize_t)bufsize) { /* Error, possibly not enough space. */ system_result_size = -1; @@ -435,75 +408,3 @@ get_relocated_single_path(char const * unix_path) strcat(new_path, unix_part); return new_path; } - -#define X509_PRIVATE_DIR "/mingw64/ssl/private" -const char * -X509_get_default_private_dir(void) -{ -#ifdef __MINGW32__ - return get_relocated_single_path (X509_PRIVATE_DIR); -#else - return (X509_PRIVATE_DIR); -#endif -} - -int main(int argc, char *argv[]) -{ -#define BINDIR "/mingw64/bin" -#define DATADIR "/mingw64/share" - - char exe_path[PATH_MAX]; - get_executable_path (argv[0], &exe_path[0], sizeof(exe_path)/sizeof(exe_path[0])); - printf ("executable path is %s\n", exe_path); - - char * rel_to_datadir = get_relative_path (BINDIR, DATADIR); - if (strrchr (exe_path, '/') != NULL) - { - strrchr (exe_path, '/')[1] = '\0'; - } - strcat (exe_path, rel_to_datadir); - simplify_path (&exe_path[0]); - printf("real path of DATADIR is %s\n", exe_path); - - if (argc >= 2) - { - get_relative_path_debug (argv[argc-2], argv[argc-1], 0); - } - get_relative_path_debug ("/a/b/c/d", "/a/b/c", ".."); - get_relative_path_debug ("/a/b/c/d/", "/a/b/c/", "../"); - get_relative_path_debug ("/", "/", "/"); - get_relative_path_debug ("/a/testone/c/d", "/a/testtwo/c", "../../../testtwo/c"); - get_relative_path_debug ("/a/testone/c/d/", "/a/testtwo/c/", "../../../testtwo/c/"); - get_relative_path_debug ("/home/part2/part3/part4", "/work/proj1/proj2", "../../../../work/proj1/proj2"); - - simplify_path_debug ("a/b/..", "a"); - simplify_path_debug ("a/b/c/../../", "a/"); - simplify_path_debug ("a/../a/..", ""); - simplify_path_debug ("../a/../a/", "../a/"); - - simplify_path_debug ("./././", "./"); - simplify_path_debug ("/test/", "/test/"); - simplify_path_debug (".", "."); - simplify_path_debug ("..", ".."); - simplify_path_debug ("../", "../"); - simplify_path_debug ("././.", "."); - simplify_path_debug ("../..", "../.."); - simplify_path_debug ("/", "/"); - simplify_path_debug ("./test/", "./test/"); - simplify_path_debug ("./test", "./test"); - simplify_path_debug ("/test", "/test"); - simplify_path_debug ("../test", "../test"); - simplify_path_debug ("../../test", "../../test"); - simplify_path_debug ("../test/..", ".."); - simplify_path_debug (".././../", "../../"); - - sanitise_path_debug ("C:\\windows\\path", "C:/windows/path"); - sanitise_path_debug ("", ""); - sanitise_path_debug ("\\\\", "/"); - - char const * win_path = X509_get_default_private_dir (); - printf ("%s -> %s\n", X509_PRIVATE_DIR, win_path); - free ((void *)win_path); - - return 0; -} diff --git a/mingw-w64-pathtools/pathtools.h b/mingw-w64-pathtools/pathtools.h new file mode 100644 index 0000000000..17fa7cf254 --- /dev/null +++ b/mingw-w64-pathtools/pathtools.h @@ -0,0 +1,62 @@ +/* + .Some useful path tools. + Written by Ray Donnelly in 2014. + Licensed under CC0. No warranty. + */ + +#ifndef PATHTOOLS_H +#define PATHTOOLS_H + +#if defined(__APPLE__) +#include +#else +#include +#endif +#include +#include +#include +#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__) +#include +#endif +#include + +/* If you don't define this, then get_executable_path() + can only use argv[0] which will often not work well */ +#define IMPLEMENT_SYS_GET_EXECUTABLE_PATH + +#if defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) +#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__) +/* Nothing needed, unistd.h is enough. */ +#elif defined(__APPLE__) +#include +#elif defined(_WIN32) +#define WIN32_MEAN_AND_LEAN +#include +#include +#endif +#endif /* defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) */ + +/* These functions are used to support relocation.*/ + +/* Returns the malloc'ed string. Caller should free using free(void*) */ +//static char * malloc_copy_string (char const * original); + +/* Uses a host OS specific function to determine the path of the executable, + if IMPLEMENT_SYS_GET_EXECUTABLE_PATH is defined, otherwise uses argv0. */ +int get_executable_path(char const * argv0, char * result, ssize_t max_size); + +/* Where possible, removes occourances of '.' and 'path/..' */ +void simplify_path (char * path); + +/* mallocs and returns the path to get from from to to. Caller should free using free(void*) */ +char * get_relative_path (char const * from, char const * to); + +/* returns relocated path from single unix path. Caller should free using free(void*) */ +const char * get_relocated_single_path(char const * unix_path); + +/* Debug functions */ +void get_relative_path_debug (char const * from, char const * to, char const * expected); +void simplify_path_debug (const char * input, const char * expected); +void sanitise_path_debug(char const * path, char const * expected); + +#endif diff --git a/mingw-w64-pathtools/pathtools.qbs b/mingw-w64-pathtools/pathtools.qbs index 7b111d1f27..83f8ecf02c 100644 --- a/mingw-w64-pathtools/pathtools.qbs +++ b/mingw-w64-pathtools/pathtools.qbs @@ -3,7 +3,7 @@ import qbs CppApplication { type: "application" // To suppress bundle generation on Mac consoleApplication: true - files: "pathtools.c" + files: ['main.c', 'pathtools.h', 'pathtools.c'] Group { // Properties for the produced executable fileTagsFilter: product.type