source: remove build warning for 64bit build
[platform/adaptation/emulator/vmodem-daemon-emulator.git] / lib / libcommon / fileio.c
1 /*
2  *  telephony-emulator
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * Sooyoung Ha <yoosah.ha@samsung.com>
8  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23  *
24  * Contributors:
25  * - S-Core Co., Ltd
26  *
27  */
28
29 #include <stdint.h>
30 #include "fileio.h"
31
32 gchar *get_simulator_path(void)
33 {
34     gchar *simulator_path = NULL;
35     int length = 0;
36
37 #ifndef _WIN32
38     simulator_path = (char *)malloc( 512);
39     if(simulator_path == NULL){
40         return NULL;
41     }
42     memset(simulator_path, 0, 512);
43
44     length = readlink("/proc/self/exe", simulator_path, 512);
45     simulator_path[length] = '\0';
46 #else
47     gchar simulator_org_path[512] = {0, };
48     GetModuleFileName(NULL, simulator_org_path, 512);
49     simulator_path = change_path_to_slash(simulator_org_path);
50 #endif  
51
52     /* exception precess */
53
54     if (length < 0) {
55         exit(-1);
56     }
57
58     if (length >= 512) {
59         fprintf(stderr, "Path too long. Truncated.\n");
60         exit(-1);
61     }
62
63     return simulator_path;
64 }
65
66 gchar *get_bin_path(void)
67 {
68     gchar *simulator_path = NULL;
69     simulator_path = get_simulator_path();
70
71     gchar *bin_path = NULL;
72     bin_path = g_path_get_dirname(simulator_path);
73
74     if (simulator_path)
75         g_free(simulator_path);
76
77     return bin_path;
78 }
79
80 gchar *get_path(void)
81 {
82     gchar *simulator_path = NULL;
83     simulator_path = get_simulator_path();
84
85     gchar *path = NULL;
86     path = g_path_get_dirname(simulator_path);
87
88     if (simulator_path)
89         g_free(simulator_path);
90
91     return path;
92 }
93
94 gchar *get_skin_path(void)
95 {
96     gchar *path = NULL;
97     path = get_path();
98     if(path == NULL){
99         return NULL;
100     }
101
102     gchar *skin_path = NULL;
103     skin_path = (char *)malloc( 512);
104     if(skin_path == NULL){
105         return NULL;
106     }
107     memset(skin_path, 0, 512);
108     strncpy(skin_path, path, 512);
109
110     sprintf(skin_path, "%s%s", path, "/skins");
111
112     g_free(path);
113
114     return skin_path;
115 }
116
117 gchar *get_data_path(void)
118 {
119     gchar *path = NULL;
120     path = get_path();
121     if(path == NULL){
122         return NULL;
123     }
124
125     gchar *data_path = NULL;
126     data_path = (char *)malloc( 512);
127     if(data_path == NULL){
128         return NULL;
129     }
130     memset(data_path, 0, 512);
131     strncpy(data_path, path, 512);
132
133     sprintf(data_path, "%s%s", path, "/data");
134
135     g_free(path);
136
137     return data_path;
138 }
139
140 gchar *get_kernel_path(void)
141 {
142     gchar *path = NULL;
143     path = get_data_path();
144     if(path == NULL){
145         return NULL;
146     }
147
148     gchar *kernel_path = NULL;
149     kernel_path = (char *)malloc( 512);
150     if(kernel_path == NULL){
151         g_free(path);
152         return NULL;
153     }
154     memset(kernel_path, 0, 512);
155     strncpy(kernel_path, path, 512);
156
157     sprintf(kernel_path, "%s%s", path, "/kernel-img");
158
159     g_free(path);
160
161     return kernel_path;
162 }
163
164
165 /**
166  * @brief       change_path_to_slash (\, \\ -> /)
167  * @param       org path to change (C:\\test\\test\\test)
168  * @return      changed path (C:/test/test/test)
169  * @date    Nov 19. 2009
170  * */
171 gchar *change_path_to_slash(gchar *org_path)
172 {       
173     if (!org_path || strlen(org_path) < 1)
174         return NULL;
175
176     int length, i = 0;
177     length = strlen(org_path);
178
179     gchar *changed_path = NULL;
180     changed_path = (char *)malloc( 512);
181     memset(changed_path, 0, 512);
182
183     sprintf(changed_path, "%s", org_path);
184
185     for (i = 0 ; i < length; i ++) {
186         if (org_path[i] == '\\') {
187             changed_path[i] = '/';      
188         }
189     }
190
191     return changed_path;
192 }
193
194 gchar *get_conf_path(void)
195 {       
196     gchar *conf_path = NULL;
197
198 #ifndef _WIN32  
199     conf_path = (char *)malloc( 512);
200     if(conf_path == NULL){
201         return NULL;
202     }
203     memset(conf_path, 0, 512);
204
205     gchar *path = NULL;
206     path = get_path();
207     if(path == NULL){
208         g_free(conf_path);
209         return NULL;
210     }
211
212     strncpy(conf_path, path, 512);
213
214     sprintf(conf_path, "%s%s", path, "/conf");
215
216     g_free(path);
217 #else
218     TCHAR APP_PATH[MAX_PATH];
219     gchar org_conf_path[128] = {0, };
220
221     SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, APP_PATH);
222     sprintf(org_conf_path, "%s%s", APP_PATH, PID_DIR);
223     conf_path = change_path_to_slash(org_conf_path);            
224 #endif
225
226     return conf_path;
227 }
228
229
230
231 /**
232  * @brief       get_simulator conf file path
233  * */
234 gchar *get_simulator_conf_filepath(void)
235 {
236     gchar *simulator_conf_filepath = NULL;
237     simulator_conf_filepath = (char *)malloc( 512);
238     memset(simulator_conf_filepath, 0, 512);
239
240 #ifndef _WIN32  
241     if (!strncmp(g_get_user_name(), "root", 4))
242         sprintf(simulator_conf_filepath, "/root%ssimulator.conf", PID_DIR);
243     else
244         sprintf(simulator_conf_filepath, "/home/%s%ssimulator.conf", g_get_user_name(), PID_DIR);
245 #else
246     gchar *conf_path = get_conf_path();
247     sprintf(simulator_conf_filepath, "%ssimulator.conf", conf_path);
248
249     if (conf_path)
250         g_free(conf_path);
251 #endif
252
253     return simulator_conf_filepath;
254 }
255
256
257 /**
258  * @brief       get_default target conf file path
259  * */
260 gchar *get_platform_conf_filepath(void)
261 {
262     gchar *simultor_path = NULL;
263     simultor_path = get_path();
264
265     gchar *default_conf_filepath = NULL;
266     default_conf_filepath = (char *)malloc( 512);
267     memset(default_conf_filepath, 0, 512);
268
269     sprintf(default_conf_filepath, "%s/.default_target.conf", simultor_path);
270
271     if (simultor_path)
272         g_free(simultor_path);
273
274     return default_conf_filepath;
275 }
276
277
278 /**
279   @brief        search target name in directory name
280   @param        directory_name: target directory name of scratchbox
281   @return       target name
282  */
283 char *search_target_name(char *directory_name)
284 {
285     int directory_len = 0;
286     int search_i = 0;
287     char *found_name = NULL;
288
289     directory_len = strlen(directory_name);
290
291     /* search target name pointer */
292     for (search_i = directory_len; search_i > 0; search_i--) {
293         if (directory_name[search_i-1] == '/') {
294             found_name = directory_name + search_i;
295             return found_name;
296         }
297     }
298
299     return NULL;
300 }
301
302 char *find_exec_path(char *line) {
303     uintptr_t loc;
304     uintptr_t find_loc = 0;
305     char *ldpath = NULL;
306     char *start_p = NULL;
307     static char exec_path[512] = "";
308     int nfound = 0;
309     size_t len;
310
311 #if 0
312     // ld path -> factoryfs of bash_profile
313     //ldpath = strstr(line, "LD_LIBRARY_PATH");
314 #endif
315
316     ldpath = strstr(line, "FACTORY_FS");
317     if (ldpath == NULL) {
318         return NULL;
319     }
320
321     find_loc = (uintptr_t)ldpath - (uintptr_t)line;
322
323     for (loc = 0; loc < find_loc;loc++) {
324         if (line[loc] == '#') {
325             return NULL;
326         }
327     }
328
329     for (len = 0; len < strlen(ldpath);len++) {
330
331         if (ldpath[len] == '#') {
332             return NULL;
333         }
334         if (ldpath[len] == '=') {
335             start_p = &ldpath[len];
336             nfound = 1;
337             break;
338         }
339     }
340
341     if (nfound == 0) {
342         return NULL;
343     }
344
345 #if 0
346     // lib directory
347     end_p = strstr(start_p, "/lib");
348     if (end_p == NULL) {
349         return NULL;
350     }
351
352     strncpy(exec_path, start_p+1, (int)end_p - (int)start_p -1);
353 #endif
354
355     strncpy(exec_path, start_p+1, strlen(start_p+1)-1);
356
357
358     return exec_path;
359 }
360
361 char *find_exec_path_by_bash_profile(char *target_path)
362 {
363     char *exec_path;
364     char pathname[BUFSIZ] = "";
365     char line[BUFSIZ];
366     FILE * fp;
367
368     sprintf(pathname, "%s/root/.bash_profile", target_path);
369
370     if ((fp = fopen(pathname, "r")) == NULL) {
371         return NULL;
372     }
373
374     while (fgets(line, BUFSIZ, fp)) {
375         exec_path = find_exec_path(line);
376
377         if (exec_path != NULL) {
378             fclose(fp);
379             return exec_path;
380         }
381     }
382
383     fclose(fp);
384     return NULL;
385 }
386
387
388 char *find_ld_path(char *line) {
389     uintptr_t loc;
390     char *ldpath = NULL;
391     uintptr_t find_loc = 0;
392     size_t len;
393
394     ldpath = strstr(line, "LD_LIBRARY_PATH");
395     if (ldpath == NULL) {
396         return NULL;
397     }
398
399     find_loc = (uintptr_t)ldpath - (uintptr_t)line;
400
401     for (loc = 0; loc < find_loc;loc++) {
402         if (line[loc] == '#') {
403             return NULL;
404         }
405     }
406
407     for (len = 0; len < strlen(ldpath);len++) {
408         if (ldpath[len] == '#') {
409             return NULL;
410         }
411         if (ldpath[len] == '=') {
412             break;
413         }
414     }
415
416     return &ldpath[len] + 1;
417 }
418
419 char *find_ld_path_by_bash_profile(char *target_path)
420 {
421     char *exec_path;
422     char pathname[BUFSIZ] = "";
423     static char line[BUFSIZ] = "";
424     FILE * fp;
425
426     sprintf(pathname, "%s/root/.bash_profile", target_path);
427
428     if ((fp = fopen(pathname, "r")) == NULL) {
429         return NULL;
430     }
431
432     while (fgets(line, BUFSIZ, fp)) {
433         exec_path = find_ld_path(line);
434
435         if (exec_path != NULL) {
436             if (exec_path[strlen(exec_path) - 1] == '\n')
437                 exec_path[strlen(exec_path) - 1] = '\0';
438             fclose(fp);
439             return exec_path;
440         }
441     }
442
443     fclose(fp);
444     return NULL;
445 }
446
447