2
* FreeRDP: A Remote Desktop Protocol Client
5
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
25
#include <freerdp/types.h>
26
#include <freerdp/settings.h>
27
#include <freerdp/utils/memory.h>
28
#include <freerdp/utils/string.h>
34
#define getcwd _getcwd
37
#include <freerdp/utils/file.h>
40
#define PATH_SEPARATOR_STR "/"
41
#define PATH_SEPARATOR_CHR '/'
42
#define HOME_ENV_VARIABLE "HOME"
45
#define PATH_SEPARATOR_STR "\\"
46
#define PATH_SEPARATOR_CHR '\\'
47
#define HOME_ENV_VARIABLE "HOMEPATH"
51
#define SHARED_LIB_SUFFIX ".dll"
53
#define SHARED_LIB_SUFFIX ".dylib"
55
#define SHARED_LIB_SUFFIX ".so"
58
#define FREERDP_CONFIG_DIR ".freerdp"
60
#define PARENT_PATH ".." PATH_SEPARATOR_STR
62
void freerdp_mkdir(char* path)
65
mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
67
CreateDirectoryA(path, 0);
71
boolean freerdp_check_file_exists(char* file)
73
struct stat stat_info;
75
if (stat(file, &stat_info) != 0)
81
char* freerdp_get_home_path(rdpSettings* settings)
83
if (settings->home_path == NULL)
84
settings->home_path = getenv(HOME_ENV_VARIABLE);
85
if (settings->home_path == NULL)
86
settings->home_path = xstrdup("/");
88
return settings->home_path;
91
char* freerdp_get_config_path(rdpSettings* settings)
95
path = (char*) xmalloc(strlen(settings->home_path) + sizeof(FREERDP_CONFIG_DIR) + 2);
96
sprintf(path, "%s/%s", settings->home_path, FREERDP_CONFIG_DIR);
98
if (!freerdp_check_file_exists(path))
101
settings->config_path = path;
106
char* freerdp_get_current_path(rdpSettings* settings)
108
if (settings->current_path == NULL)
109
settings->current_path = getcwd(NULL, 0);
111
return settings->current_path;
114
char* freerdp_construct_path(char* base_path, char* relative_path)
118
int base_path_length;
119
int relative_path_length;
121
base_path_length = strlen(base_path);
122
relative_path_length = strlen(relative_path);
123
length = base_path_length + relative_path_length + 1;
125
path = xmalloc(length + 1);
126
sprintf(path, "%s" PATH_SEPARATOR_STR "%s", base_path, relative_path);
131
char* freerdp_append_shared_library_suffix(char* file_path)
135
int file_path_length;
136
int shared_lib_suffix_length;
138
if (file_path == NULL)
141
file_path_length = strlen(file_path);
142
shared_lib_suffix_length = strlen(SHARED_LIB_SUFFIX);
144
if (file_path_length >= shared_lib_suffix_length)
146
p = &file_path[file_path_length - shared_lib_suffix_length];
148
if (strcmp(p, SHARED_LIB_SUFFIX) != 0)
150
path = xmalloc(file_path_length + shared_lib_suffix_length + 1);
151
sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
155
path = xstrdup(file_path);
160
path = xstrdup(file_path);
166
char* freerdp_get_parent_path(char* base_path, int depth)
174
if (base_path == NULL)
178
return xstrdup(base_path);
180
base_length = strlen(base_path);
182
p = &base_path[base_length];
184
for (i = base_length - 1; ((i >= 0) && (depth > 0)); i--)
186
if (base_path[i] == PATH_SEPARATOR_CHR)
193
length = (p - base_path);
195
path = (char*) xmalloc(length + 1);
196
memcpy(path, base_path, length);
202
boolean freerdp_path_contains_separator(char* path)
207
if (strchr(path, PATH_SEPARATOR_CHR) == NULL)
213
/* detects if we are running from the source tree */
215
boolean freerdp_detect_development_mode(rdpSettings* settings)
219
char* development_path = NULL;
220
boolean development_mode = false;
222
if (freerdp_check_file_exists(".git"))
225
development_mode = true;
227
else if (freerdp_check_file_exists(PARENT_PATH ".git"))
230
development_mode = true;
232
else if (freerdp_check_file_exists(PARENT_PATH PARENT_PATH ".git"))
235
development_mode = true;
238
current_path = freerdp_get_current_path(settings);
240
if (development_mode)
241
development_path = freerdp_get_parent_path(current_path, depth);
243
settings->development_mode = development_mode;
244
settings->development_path = development_path;
246
return settings->development_mode;
249
void freerdp_detect_paths(rdpSettings* settings)
251
freerdp_get_home_path(settings);
252
freerdp_get_config_path(settings);
253
freerdp_detect_development_mode(settings);
256
printf("home path: %s\n", settings->home_path);
257
printf("config path: %s\n", settings->config_path);
258
printf("current path: %s\n", settings->current_path);
260
if (settings->development_mode)
261
printf("development path: %s\n", settings->development_path);