20a1f10852dedbf973092a21bbd6e82e171d7895
[platform/core/system/swap-probe.git] / helper / dahelper.c
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Jaewon Lim <jaewon81.lim@samsung.com>
9  * Woojin Jung <woojin2.jung@samsung.com>
10  * Juyoung Kim <j0.kim@samsung.com>
11  *
12  * This library is free software; you can redistribute it and/or modify it under
13  * the terms of the GNU Lesser General Public License as published by the
14  * Free Software Foundation; either version 2.1 of the License, or (at your option)
15  * any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this library; if not, write to the Free Software Foundation, Inc., 51
24  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  *
29  */
30
31 #include <wchar.h>                      // for wcslen
32 #include <unistd.h>                     // for unlink
33 #include <sys/types.h>
34 #include <dirent.h>                     // for opendir, readdir
35 #include <assert.h>
36 #include "dahelper.h"
37
38 int app_efl_main_flg = 0;
39
40 const char *lib_string[NUM_ORIGINAL_LIBRARY] = {
41         "libc.so.6",                            //0
42         "libpthread.so.0",                      //1
43         "libelementary.so",                     //2
44         "libosp-uifw.so",                       //3
45         "libosp-appfw.so",                      //4
46         "libosp-web.so",                        //5
47         "libecore_input_evas.so.1",             //6
48         "libdaemon.so.0",                       //7
49         "libcapi-appfw-application.so.0",       //8
50         "libGLESv2.so",                         //9
51         "libEGL.so",                            //10
52         "libosp-net.so",                        //11
53         SELF_LIB_NAME                           //12
54 };
55 void *lib_handle[NUM_ORIGINAL_LIBRARY];
56
57 /* trace info global variable */
58 __traceInfo gTraceInfo =
59 {
60         {
61                 1,                                                      // int eventIndex
62                 PTHREAD_MUTEX_INITIALIZER       // pthread_mutex_t eventMutex
63         },              // __indexInfo
64         {
65                 -1,                                                     // int daemonSock
66                 PTHREAD_MUTEX_INITIALIZER       // ptrhread_mutex_t sockMutex
67         },              // __socketInfo
68         {
69                 {0, },                                          // char appName[128]
70                 0                                                       // unsigned int startTime
71         },              // __appInfo
72         {
73                 0,                                                      // int state
74                 PTHREAD_MUTEX_INITIALIZER       // ptrhread_mutex_t ssMutex
75         },              // __screenshotInfo
76         {
77                 NULL,                                           // map_start
78                 NULL                                            // map_end
79         },              // __mapInfo
80         -1,                                                             // int stateTouch
81         0,                                                              // int init_complete
82         0,                                                              // int custom_chart_callback_count
83         0                                                               // unsigned long optionflag
84 };
85
86 void WcharToChar(char* pstrDest, const wchar_t* pwstrSrc)
87 {
88         int nLen=(int)wcslen(pwstrSrc);
89         wcstombs(pstrDest, pwstrSrc, nLen+1);
90 }
91
92 // return 0 if succeed
93 // return -1 if error occured
94 int remove_indir(const char *dirname)
95 {
96         DIR *dir;
97         struct dirent *entry;
98         char path[MAX_PATH_LENGTH];
99
100         dir = opendir(dirname);
101         if(dir == NULL)
102         {
103                 return -1;
104         }
105
106         while((entry = readdir(dir)) != NULL)
107         {
108                 if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
109                 {
110                         snprintf(path, (size_t) MAX_PATH_LENGTH, "%s/%s", dirname, entry->d_name);
111                         if (entry->d_type != DT_DIR)    // file
112                         {
113                                 unlink(path);
114                         }
115                         else { }        // directory
116                 }
117         }
118         closedir(dir);
119
120         return 0;
121 }
122
123 static int absolute_filepath_p(const char *fname)
124 {
125         return fname[0] == '/';
126 }
127
128 /* Return pointer to static buffer */
129 char *absolutize_filepath(char buffer[PATH_MAX], const char *fname)
130 {
131         char cwd[PATH_MAX];
132
133         assert(fname && "Filename, passed to stdc function is NULL.");
134         if (absolute_filepath_p(fname) || getcwd(cwd, sizeof(cwd)) == NULL)
135                 snprintf(buffer, PATH_MAX, "%s", fname);
136         else
137                 snprintf(buffer, PATH_MAX, "%s/%s", cwd, fname);
138         return buffer;
139 }