Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore_file / ecore_file_path.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #undef alloca
6 #ifdef HAVE_ALLOCA_H
7 # include <alloca.h>
8 #elif defined __GNUC__
9 # define alloca __builtin_alloca
10 #elif defined _AIX
11 # define alloca __alloca
12 #elif defined _MSC_VER
13 # include <malloc.h>
14 # define alloca _alloca
15 #else
16 # include <stddef.h>
17 # ifdef  __cplusplus
18 extern "C"
19 # endif
20 void *alloca (size_t);
21 #endif
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "ecore_file_private.h"
28
29 static Eina_List *__ecore_file_path_bin = NULL;
30
31 static Eina_List *_ecore_file_path_from_env(const char *env);
32
33 void
34 ecore_file_path_init(void)
35 {
36    __ecore_file_path_bin = _ecore_file_path_from_env("PATH");
37 }
38
39 void
40 ecore_file_path_shutdown(void)
41 {
42    char *dir;
43
44    EINA_LIST_FREE(__ecore_file_path_bin, dir)
45      eina_stringshare_del(dir);
46 }
47
48 Eina_List *
49 _ecore_file_path_from_env(const char *env)
50 {
51    Eina_List *path = NULL;
52    char *env_tmp, *env_path, *p, *last;
53
54    env_tmp = getenv(env);
55    if (!env_tmp)
56      return path;
57
58    env_path = alloca(sizeof(char) * strlen(env_tmp) + 1);
59    memset(env_path, 0, strlen(env_tmp));
60    strcpy(env_path, env_tmp);
61    last = env_path;
62    for (p = env_path; *p; p++)
63      {
64         if (*p == ':')
65           *p = '\0';
66
67         if (!*p)
68           {
69              if (!ecore_file_path_dir_exists(last))
70                path = eina_list_append(path, eina_stringshare_add(last));
71              last = p + 1;
72           }
73      }
74    if (p > last)
75      path = eina_list_append(path, eina_stringshare_add(last));
76
77    return path;
78 }
79
80 /**
81  * @addtogroup Ecore_File_Group Ecore_File - Files and directories convenience functions
82  *
83  * @{
84  */
85
86 /**
87  * @brief Check if the given directory is in PATH.
88  *
89  * @param in_dir The name of the directory to search in PATH.
90  * @return @c EINA_TRUE if the directory exist in PATH, @c EINA_FALSE otherwise.
91  *
92  * This function checks if @p in_dir is in the environment variable
93  * PATH. If @p in_dir is @c NULL, or if PATH is empty, or @p in_dir is
94  * not in PATH, the function returns @c EINA_FALSE, otherwise it returns
95  * @c EINA_TRUE.
96  */
97 EAPI Eina_Bool
98 ecore_file_path_dir_exists(const char *in_dir)
99 {
100    Eina_List *l;
101    char *dir;
102
103    if (!in_dir)
104      return EINA_FALSE;
105
106    if (!__ecore_file_path_bin) return EINA_FALSE;
107    EINA_LIST_FOREACH(__ecore_file_path_bin, l, dir)
108      {
109         if (strcmp(dir, in_dir))
110           return EINA_TRUE;
111      }
112
113    return EINA_FALSE;
114 }
115
116 /**
117  * @brief Check if the given application is installed.
118  *
119  * @param  exe The name of the application
120  * @return @c EINA_TRUE if the @p exe is in PATH and is executable,
121  * @c EINA_FALSE otherwise.
122  *
123  * This function checks if @p exe exists in PATH and is executable. If
124  * @p exe is @c NULL or is not executable, the function returns
125  * @c EINA_FALSE, otherwise it returns @c EINA_TRUE.
126  */
127 EAPI Eina_Bool
128 ecore_file_app_installed(const char *exe)
129 {
130    Eina_List *l;
131    char *dir;
132    char  buf[PATH_MAX];
133
134    if (!exe) return EINA_FALSE;
135    if (ecore_file_can_exec(exe)) return EINA_TRUE;
136
137    EINA_LIST_FOREACH(__ecore_file_path_bin, l, dir)
138      {
139         snprintf(buf, sizeof(buf), "%s/%s", dir, exe);
140         if (ecore_file_can_exec(buf))
141           return EINA_TRUE;
142      }
143
144    return EINA_FALSE;
145 }
146
147 /**
148  * @brief Get a list of all the applications installed on the system.
149  *
150  * @return An Eina_List containing all the executable files in the
151  * system.
152  *
153  * This function returns a list of allocated strings of all the
154  * executable files. If no files are found, the function returns
155  * @c NULL. When not needed anymore, the element of the list must be
156  * freed.
157  */
158 EAPI Eina_List *
159 ecore_file_app_list(void)
160 {
161    Eina_List *list = NULL;
162    Eina_List *files;
163    Eina_List *l;
164    char  buf[PATH_MAX], *dir, *exe;
165
166    EINA_LIST_FOREACH(__ecore_file_path_bin, l, dir)
167      {
168         files = ecore_file_ls(dir);
169         EINA_LIST_FREE(files, exe)
170                {
171                   snprintf(buf, sizeof(buf), "%s/%s", dir, exe);
172                   if ((ecore_file_can_exec(buf)) &&
173                       (!ecore_file_is_dir(buf)))
174                list = eina_list_append(list, strdup(buf));
175              free(exe);
176           }
177      }
178
179    return list;
180 }
181
182 /**
183  * @}
184  */