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);
86 return settings->home_path;
89 char* freerdp_get_config_path(rdpSettings* settings)
93 path = (char*) xmalloc(strlen(settings->home_path) + sizeof(FREERDP_CONFIG_DIR) + 2);
94 sprintf(path, "%s/%s", settings->home_path, FREERDP_CONFIG_DIR);
96 if (!freerdp_check_file_exists(path))
99 settings->config_path = path;
104 char* freerdp_get_current_path(rdpSettings* settings)
106 if (settings->current_path == NULL)
107 settings->current_path = getcwd(NULL, 0);
109 return settings->current_path;
112 char* freerdp_construct_path(char* base_path, char* relative_path)
116 int base_path_length;
117 int relative_path_length;
119 base_path_length = strlen(base_path);
120 relative_path_length = strlen(relative_path);
121 length = base_path_length + relative_path_length + 1;
123 path = xmalloc(length + 1);
124 sprintf(path, "%s" PATH_SEPARATOR_STR "%s", base_path, relative_path);
129 char* freerdp_append_shared_library_suffix(char* file_path)
133 int file_path_length;
134 int shared_lib_suffix_length;
136 if (file_path == NULL)
139 file_path_length = strlen(file_path);
140 shared_lib_suffix_length = strlen(SHARED_LIB_SUFFIX);
142 if (file_path_length >= shared_lib_suffix_length)
144 p = &file_path[file_path_length - shared_lib_suffix_length];
146 if (strcmp(p, SHARED_LIB_SUFFIX) != 0)
148 path = xmalloc(file_path_length + shared_lib_suffix_length + 1);
149 sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
153 path = xstrdup(file_path);
158 path = xstrdup(file_path);
164 char* freerdp_get_parent_path(char* base_path, int depth)
172 if (base_path == NULL)
176 return xstrdup(base_path);
178 base_length = strlen(base_path);
180 p = &base_path[base_length];
182 for (i = base_length - 1; ((i >= 0) && (depth > 0)); i--)
184 if (base_path[i] == PATH_SEPARATOR_CHR)
191 length = (p - base_path);
193 path = (char*) xmalloc(length + 1);
194 memcpy(path, base_path, length);
200 boolean freerdp_path_contains_separator(char* path)
205 if (strchr(path, PATH_SEPARATOR_CHR) == NULL)
211 /* detects if we are running from the source tree */
213 boolean freerdp_detect_development_mode(rdpSettings* settings)
217 char* development_path = NULL;
218 boolean development_mode = false;
220 if (freerdp_check_file_exists(".git"))
223 development_mode = true;
225 else if (freerdp_check_file_exists(PARENT_PATH ".git"))
228 development_mode = true;
230 else if (freerdp_check_file_exists(PARENT_PATH PARENT_PATH ".git"))
233 development_mode = true;
236 current_path = freerdp_get_current_path(settings);
238 if (development_mode)
239 development_path = freerdp_get_parent_path(current_path, depth);
241 settings->development_mode = development_mode;
242 settings->development_path = development_path;
244 return settings->development_mode;
247 void freerdp_detect_paths(rdpSettings* settings)
249 freerdp_get_home_path(settings);
250 freerdp_get_config_path(settings);
251 freerdp_detect_development_mode(settings);
254 printf("home path: %s\n", settings->home_path);
255 printf("config path: %s\n", settings->config_path);
256 printf("current path: %s\n", settings->current_path);
258 if (settings->development_mode)
259 printf("development path: %s\n", settings->development_path);