~ubuntu-branches/ubuntu/vivid/freerdp/vivid

« back to all changes in this revision

Viewing changes to libfreerdp-utils/file.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-11-11 12:20:50 UTC
  • mfrom: (1.2.5)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20141111122050-7z628f4ab38qxad5
Tags: upstream-1.1.0~git20140921.1.440916e+dfsg1
ImportĀ upstreamĀ versionĀ 1.1.0~git20140921.1.440916e+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
        if (settings->home_path == NULL)
86
 
                settings->home_path = xstrdup("/");
87
 
 
88
 
        return settings->home_path;
89
 
}
90
 
 
91
 
char* freerdp_get_config_path(rdpSettings* settings)
92
 
{
93
 
        char* path;
94
 
 
95
 
        path = (char*) xmalloc(strlen(settings->home_path) + sizeof(FREERDP_CONFIG_DIR) + 2);
96
 
        sprintf(path, "%s/%s", settings->home_path, FREERDP_CONFIG_DIR);
97
 
 
98
 
        if (!freerdp_check_file_exists(path))
99
 
                freerdp_mkdir(path);
100
 
 
101
 
        settings->config_path = path;
102
 
 
103
 
        return path;
104
 
}
105
 
 
106
 
char* freerdp_get_current_path(rdpSettings* settings)
107
 
{
108
 
        if (settings->current_path == NULL)
109
 
                settings->current_path = getcwd(NULL, 0);
110
 
 
111
 
        return settings->current_path;
112
 
}
113
 
 
114
 
char* freerdp_construct_path(char* base_path, char* relative_path)
115
 
{
116
 
        char* path;
117
 
        int length;
118
 
        int base_path_length;
119
 
        int relative_path_length;
120
 
 
121
 
        base_path_length = strlen(base_path);
122
 
        relative_path_length = strlen(relative_path);
123
 
        length = base_path_length + relative_path_length + 1;
124
 
 
125
 
        path = xmalloc(length + 1);
126
 
        sprintf(path, "%s" PATH_SEPARATOR_STR "%s", base_path, relative_path);
127
 
 
128
 
        return path;
129
 
}
130
 
 
131
 
char* freerdp_append_shared_library_suffix(char* file_path)
132
 
{
133
 
        char* p;
134
 
        char* path = NULL;
135
 
        int file_path_length;
136
 
        int shared_lib_suffix_length;
137
 
 
138
 
        if (file_path == NULL)
139
 
                return NULL;
140
 
 
141
 
        file_path_length = strlen(file_path);
142
 
        shared_lib_suffix_length = strlen(SHARED_LIB_SUFFIX);
143
 
 
144
 
        if (file_path_length >= shared_lib_suffix_length)
145
 
        {
146
 
                p = &file_path[file_path_length - shared_lib_suffix_length];
147
 
 
148
 
                if (strcmp(p, SHARED_LIB_SUFFIX) != 0)
149
 
                {
150
 
                        path = xmalloc(file_path_length + shared_lib_suffix_length + 1);
151
 
                        sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
152
 
                }
153
 
                else
154
 
                {
155
 
                        path = xstrdup(file_path);
156
 
                }
157
 
        }
158
 
        else
159
 
        {
160
 
                path = xstrdup(file_path);
161
 
        }
162
 
 
163
 
        return path;
164
 
}
165
 
 
166
 
char* freerdp_get_parent_path(char* base_path, int depth)
167
 
{
168
 
        int i;
169
 
        char* p;
170
 
        char* path;
171
 
        int length;
172
 
        int base_length;
173
 
 
174
 
        if (base_path == NULL)
175
 
                return NULL;
176
 
 
177
 
        if (depth <= 0)
178
 
                return xstrdup(base_path);
179
 
 
180
 
        base_length = strlen(base_path);
181
 
 
182
 
        p = &base_path[base_length];
183
 
 
184
 
        for (i = base_length - 1; ((i >= 0) && (depth > 0)); i--)
185
 
        {
186
 
                if (base_path[i] == PATH_SEPARATOR_CHR)
187
 
                {
188
 
                        p = &base_path[i];
189
 
                        depth--;
190
 
                }
191
 
        }
192
 
 
193
 
        length = (p - base_path);
194
 
 
195
 
        path = (char*) xmalloc(length + 1);
196
 
        memcpy(path, base_path, length);
197
 
        path[length] = '\0';
198
 
 
199
 
        return path;
200
 
}
201
 
 
202
 
boolean freerdp_path_contains_separator(char* path)
203
 
{
204
 
        if (path == NULL)
205
 
                return false;
206
 
 
207
 
        if (strchr(path, PATH_SEPARATOR_CHR) == NULL)
208
 
                return false;
209
 
 
210
 
        return true;
211
 
}
212
 
 
213
 
/* detects if we are running from the source tree */
214
 
 
215
 
boolean freerdp_detect_development_mode(rdpSettings* settings)
216
 
{
217
 
        int depth = 0;
218
 
        char* current_path;
219
 
        char* development_path = NULL;
220
 
        boolean development_mode = false;
221
 
 
222
 
        if (freerdp_check_file_exists(".git"))
223
 
        {
224
 
                depth = 0;
225
 
                development_mode = true;
226
 
        }
227
 
        else if (freerdp_check_file_exists(PARENT_PATH ".git"))
228
 
        {
229
 
                depth = 1;
230
 
                development_mode = true;
231
 
        }
232
 
        else if (freerdp_check_file_exists(PARENT_PATH PARENT_PATH ".git"))
233
 
        {
234
 
                depth = 2;
235
 
                development_mode = true;
236
 
        }
237
 
 
238
 
        current_path = freerdp_get_current_path(settings);
239
 
 
240
 
        if (development_mode)
241
 
                development_path = freerdp_get_parent_path(current_path, depth);
242
 
 
243
 
        settings->development_mode = development_mode;
244
 
        settings->development_path = development_path;
245
 
 
246
 
        return settings->development_mode;
247
 
}
248
 
 
249
 
void freerdp_detect_paths(rdpSettings* settings)
250
 
{
251
 
        freerdp_get_home_path(settings);
252
 
        freerdp_get_config_path(settings);
253
 
        freerdp_detect_development_mode(settings);
254
 
 
255
 
#if 0
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);
259
 
 
260
 
        if (settings->development_mode)
261
 
                printf("development path: %s\n", settings->development_path);
262
 
#endif
263
 
}