diff --git a/include/freerdp/settings.h b/include/freerdp/settings.h index 3556f38a9..0d2a0d0e9 100644 --- a/include/freerdp/settings.h +++ b/include/freerdp/settings.h @@ -289,7 +289,11 @@ struct rdp_settings uint32 pdu_source; /* 114 */ UNICONV* uniconv; /* 115 */ boolean server_mode; /* 116 */ - uint32 paddingE[144 - 117]; /* 117 */ + char* config_path; /* 117 */ + char* current_path; /* 118 */ + char* development_path; /* 119 */ + boolean development_mode; /* 120 */ + uint32 paddingE[144 - 121]; /* 121 */ /* Security */ boolean encryption; /* 144 */ diff --git a/include/freerdp/utils/file.h b/include/freerdp/utils/file.h index 74a86b58d..2236760e4 100644 --- a/include/freerdp/utils/file.h +++ b/include/freerdp/utils/file.h @@ -21,7 +21,16 @@ #define __FILE_UTILS_H #include +#include +#include FREERDP_API void freerdp_mkdir(char* path); +FREERDP_API boolean freerdp_check_file_exists(char* file); +FREERDP_API char* freerdp_get_home_path(rdpSettings* settings); +FREERDP_API char* freerdp_get_config_path(rdpSettings* settings); +FREERDP_API char* freerdp_get_current_path(rdpSettings* settings); +FREERDP_API char* freerdp_get_parent_path(char* base_path, int depth); +FREERDP_API boolean freerdp_detect_development_mode(rdpSettings* settings); +FREERDP_API void freerdp_detect_paths(rdpSettings* settings); #endif /* __FILE_UTILS_H */ diff --git a/libfreerdp-core/settings.c b/libfreerdp-core/settings.c index 04b1b39a1..c7d0f21fa 100644 --- a/libfreerdp-core/settings.c +++ b/libfreerdp-core/settings.c @@ -26,6 +26,7 @@ #endif #include +#include static const char client_dll[] = "C:\\Windows\\System32\\mstscax.dll"; @@ -174,6 +175,8 @@ rdpSettings* settings_new(void* instance) settings->server_random = xnew(rdpBlob); settings->server_certificate = xnew(rdpBlob); + + freerdp_detect_paths(settings); } return settings; diff --git a/libfreerdp-utils/file.c b/libfreerdp-utils/file.c index 32b42ebf6..f98a825e8 100644 --- a/libfreerdp-utils/file.c +++ b/libfreerdp-utils/file.c @@ -19,14 +19,32 @@ #include #include +#include +#include #include +#include +#include +#include +#include + #include -#ifdef _WIN32 +#ifndef _WIN32 +#define PATH_SEPARATOR_STR "/" +#define PATH_SEPARATOR_CHR '/' +#define HOME_ENV_VARIABLE "HOME" +#else #include +#define PATH_SEPARATOR_STR "\\" +#define PATH_SEPARATOR_CHR '\\' +#define HOME_ENV_VARIABLE "HOMEPATH" #endif +#define FREERDP_CONFIG_DIR ".freerdp" + +#define PARENT_PATH ".." PATH_SEPARATOR_STR + void freerdp_mkdir(char* path) { #ifndef _WIN32 @@ -36,3 +54,131 @@ void freerdp_mkdir(char* path) #endif } +boolean freerdp_check_file_exists(char* file) +{ + struct stat stat_info; + + if (stat(file, &stat_info) != 0) + return false; + + return true; +} + +char* freerdp_get_home_path(rdpSettings* settings) +{ + if (settings->home_path == NULL) + settings->home_path = getenv(HOME_ENV_VARIABLE); + + return settings->home_path; +} + +char* freerdp_get_config_path(rdpSettings* settings) +{ + char* path; + + path = (char*) xmalloc(strlen(settings->home_path) + sizeof(FREERDP_CONFIG_DIR) + 2); + sprintf(path, "%s/%s", settings->home_path, FREERDP_CONFIG_DIR); + + if (!freerdp_check_file_exists(path)) + freerdp_mkdir(path); + + settings->config_path = path; + + return path; +} + +char* freerdp_get_current_path(rdpSettings* settings) +{ + if (settings->current_path == NULL) + settings->current_path = getcwd(NULL, 0); + + return settings->current_path; +} + +char* freerdp_get_parent_path(char* base_path, int depth) +{ + int i; + char* p; + char* path; + int length; + int base_length; + + if (base_path == NULL) + return NULL; + + if (depth <= 0) + return xstrdup(base_path); + + base_length = strlen(base_path); + + p = &base_path[base_length]; + + for (i = base_length - 1; ((i >= 0) && (depth > 0)); i--) + { + if (base_path[i] == PATH_SEPARATOR_CHR) + { + p = &base_path[i]; + depth--; + } + } + + length = (p - base_path); + + path = (char*) xmalloc(length + 1); + memcpy(path, base_path, length); + path[length] = '\0'; + + return path; +} + +/* detects if we are running from the source tree */ + +boolean freerdp_detect_development_mode(rdpSettings* settings) +{ + int depth = 0; + char* current_path; + char* development_path = NULL; + boolean development_mode = false; + + if (freerdp_check_file_exists(".git")) + { + depth = 0; + development_mode = true; + } + else if (freerdp_check_file_exists(PARENT_PATH ".git")) + { + depth = 1; + development_mode = true; + } + else if (freerdp_check_file_exists(PARENT_PATH PARENT_PATH ".git")) + { + depth = 2; + development_mode = true; + } + + current_path = freerdp_get_current_path(settings); + + if (development_mode) + development_path = freerdp_get_parent_path(current_path, depth); + + settings->development_mode = development_mode; + settings->development_path = development_path; + + return settings->development_mode; +} + +void freerdp_detect_paths(rdpSettings* settings) +{ + freerdp_get_home_path(settings); + freerdp_get_config_path(settings); + freerdp_detect_development_mode(settings); + +#if 0 + printf("home path: %s\n", settings->home_path); + printf("config path: %s\n", settings->config_path); + printf("current path: %s\n", settings->current_path); + + if (settings->development_mode) + printf("development path: %s\n", settings->development_path); +#endif +}