Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-utils / file.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * File Utils
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24
25 #include <freerdp/types.h>
26 #include <freerdp/settings.h>
27 #include <freerdp/utils/memory.h>
28 #include <freerdp/utils/string.h>
29
30 #ifndef _WIN32
31 #include <unistd.h>
32 #else
33 #include <direct.h>
34 #define getcwd                  _getcwd
35 #endif
36
37 #include <freerdp/utils/file.h>
38
39 #ifndef _WIN32
40 #define PATH_SEPARATOR_STR      "/"
41 #define PATH_SEPARATOR_CHR      '/'
42 #define HOME_ENV_VARIABLE       "HOME"
43 #else
44 #include <windows.h>
45 #define PATH_SEPARATOR_STR      "\\"
46 #define PATH_SEPARATOR_CHR      '\\'
47 #define HOME_ENV_VARIABLE       "HOMEPATH"
48 #endif
49
50 #ifdef _WIN32
51 #define SHARED_LIB_SUFFIX       ".dll"
52 #elif __APPLE__
53 #define SHARED_LIB_SUFFIX       ".dylib"
54 #else
55 #define SHARED_LIB_SUFFIX       ".so"
56 #endif
57
58 #define FREERDP_CONFIG_DIR      ".freerdp"
59
60 #define PARENT_PATH             ".." PATH_SEPARATOR_STR
61
62 void freerdp_mkdir(char* path)
63 {
64 #ifndef _WIN32
65                 mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
66 #else
67                 CreateDirectoryA(path, 0);
68 #endif
69 }
70
71 boolean freerdp_check_file_exists(char* file)
72 {
73         struct stat stat_info;
74
75         if (stat(file, &stat_info) != 0)
76                 return false;
77
78         return true;
79 }
80
81 char* freerdp_get_home_path(rdpSettings* settings)
82 {
83         if (settings->home_path == NULL)
84                 settings->home_path = getenv(HOME_ENV_VARIABLE);
85
86         return settings->home_path;
87 }
88
89 char* freerdp_get_config_path(rdpSettings* settings)
90 {
91         char* path;
92
93         path = (char*) xmalloc(strlen(settings->home_path) + sizeof(FREERDP_CONFIG_DIR) + 2);
94         sprintf(path, "%s/%s", settings->home_path, FREERDP_CONFIG_DIR);
95
96         if (!freerdp_check_file_exists(path))
97                 freerdp_mkdir(path);
98
99         settings->config_path = path;
100
101         return path;
102 }
103
104 char* freerdp_get_current_path(rdpSettings* settings)
105 {
106         if (settings->current_path == NULL)
107                 settings->current_path = getcwd(NULL, 0);
108
109         return settings->current_path;
110 }
111
112 char* freerdp_construct_path(char* base_path, char* relative_path)
113 {
114         char* path;
115         int length;
116         int base_path_length;
117         int relative_path_length;
118
119         base_path_length = strlen(base_path);
120         relative_path_length = strlen(relative_path);
121         length = base_path_length + relative_path_length + 1;
122
123         path = xmalloc(length + 1);
124         sprintf(path, "%s" PATH_SEPARATOR_STR "%s", base_path, relative_path);
125
126         return path;
127 }
128
129 char* freerdp_append_shared_library_suffix(char* file_path)
130 {
131         char* p;
132         char* path = NULL;
133         int file_path_length;
134         int shared_lib_suffix_length;
135
136         if (file_path == NULL)
137                 return NULL;
138
139         file_path_length = strlen(file_path);
140         shared_lib_suffix_length = strlen(SHARED_LIB_SUFFIX);
141
142         if (file_path_length >= shared_lib_suffix_length)
143         {
144                 p = &file_path[file_path_length - shared_lib_suffix_length];
145
146                 if (strcmp(p, SHARED_LIB_SUFFIX) != 0)
147                 {
148                         path = xmalloc(file_path_length + shared_lib_suffix_length + 1);
149                         sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
150                 }
151                 else
152                 {
153                         path = xstrdup(file_path);
154                 }
155         }
156         else
157         {
158                 path = xstrdup(file_path);
159         }
160
161         return path;
162 }
163
164 char* freerdp_get_parent_path(char* base_path, int depth)
165 {
166         int i;
167         char* p;
168         char* path;
169         int length;
170         int base_length;
171
172         if (base_path == NULL)
173                 return NULL;
174
175         if (depth <= 0)
176                 return xstrdup(base_path);
177
178         base_length = strlen(base_path);
179
180         p = &base_path[base_length];
181
182         for (i = base_length - 1; ((i >= 0) && (depth > 0)); i--)
183         {
184                 if (base_path[i] == PATH_SEPARATOR_CHR)
185                 {
186                         p = &base_path[i];
187                         depth--;
188                 }
189         }
190
191         length = (p - base_path);
192
193         path = (char*) xmalloc(length + 1);
194         memcpy(path, base_path, length);
195         path[length] = '\0';
196
197         return path;
198 }
199
200 boolean freerdp_path_contains_separator(char* path)
201 {
202         if (path == NULL)
203                 return false;
204
205         if (strchr(path, PATH_SEPARATOR_CHR) == NULL)
206                 return false;
207
208         return true;
209 }
210
211 /* detects if we are running from the source tree */
212
213 boolean freerdp_detect_development_mode(rdpSettings* settings)
214 {
215         int depth = 0;
216         char* current_path;
217         char* development_path = NULL;
218         boolean development_mode = false;
219
220         if (freerdp_check_file_exists(".git"))
221         {
222                 depth = 0;
223                 development_mode = true;
224         }
225         else if (freerdp_check_file_exists(PARENT_PATH ".git"))
226         {
227                 depth = 1;
228                 development_mode = true;
229         }
230         else if (freerdp_check_file_exists(PARENT_PATH PARENT_PATH ".git"))
231         {
232                 depth = 2;
233                 development_mode = true;
234         }
235
236         current_path = freerdp_get_current_path(settings);
237
238         if (development_mode)
239                 development_path = freerdp_get_parent_path(current_path, depth);
240
241         settings->development_mode = development_mode;
242         settings->development_path = development_path;
243
244         return settings->development_mode;
245 }
246
247 void freerdp_detect_paths(rdpSettings* settings)
248 {
249         freerdp_get_home_path(settings);
250         freerdp_get_config_path(settings);
251         freerdp_detect_development_mode(settings);
252
253 #if 0
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);
257
258         if (settings->development_mode)
259                 printf("development path: %s\n", settings->development_path);
260 #endif
261 }