aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/lib/ecore_file/ecore_file_path.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/ecore/src/lib/ecore_file/ecore_file_path.c')
-rw-r--r--libraries/ecore/src/lib/ecore_file/ecore_file_path.c184
1 files changed, 184 insertions, 0 deletions
diff --git a/libraries/ecore/src/lib/ecore_file/ecore_file_path.c b/libraries/ecore/src/lib/ecore_file/ecore_file_path.c
new file mode 100644
index 0000000..ade3bc6
--- /dev/null
+++ b/libraries/ecore/src/lib/ecore_file/ecore_file_path.c
@@ -0,0 +1,184 @@
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
18extern "C"
19# endif
20void *alloca (size_t);
21#endif
22
23#include <stdio.h>
24#include <string.h>
25
26#include "ecore_file_private.h"
27
28static Eina_List *__ecore_file_path_bin = NULL;
29
30static Eina_List *_ecore_file_path_from_env(const char *env);
31
32void
33ecore_file_path_init(void)
34{
35 __ecore_file_path_bin = _ecore_file_path_from_env("PATH");
36}
37
38void
39ecore_file_path_shutdown(void)
40{
41 char *dir;
42
43 EINA_LIST_FREE(__ecore_file_path_bin, dir)
44 eina_stringshare_del(dir);
45}
46
47Eina_List *
48_ecore_file_path_from_env(const char *env)
49{
50 Eina_List *path = NULL;
51 char *env_tmp, *env_path, *p, *last;
52
53 env_tmp = getenv(env);
54 if (!env_tmp)
55 return path;
56
57 env_path = alloca(sizeof(char) * strlen(env_tmp) + 1);
58 memset(env_path, 0, strlen(env_tmp));
59 strcpy(env_path, env_tmp);
60 last = env_path;
61 for (p = env_path; *p; p++)
62 {
63 if (*p == ':')
64 *p = '\0';
65
66 if (!*p)
67 {
68 if (!ecore_file_path_dir_exists(last))
69 path = eina_list_append(path, eina_stringshare_add(last));
70 last = p + 1;
71 }
72 }
73 if (p > last)
74 path = eina_list_append(path, eina_stringshare_add(last));
75
76 return path;
77}
78
79/**
80 * @addtogroup Ecore_File_Group Ecore_File - Files and directories convenience functions
81 *
82 * @{
83 */
84
85/**
86 * @brief Check if the given directory is in PATH.
87 *
88 * @param The name of the directory to search in PATH.
89 * @return EINA_TRUE if the directory exist in PATH, EINA_FALSE otherwise.
90 *
91 * This function checks if @p in_dir is in the environment variable
92 * PATH. If @p in_dir is @c NULL, or if PATH is empty, or @p in_dir is
93 * not in PATH, the function returns EINA_FALSE, otherwise it returns
94 * EINA_TRUE.
95 */
96EAPI Eina_Bool
97ecore_file_path_dir_exists(const char *in_dir)
98{
99 Eina_List *l;
100 char *dir;
101
102 if (!in_dir)
103 return EINA_FALSE;
104
105 if (!__ecore_file_path_bin) return EINA_FALSE;
106 EINA_LIST_FOREACH(__ecore_file_path_bin, l, dir)
107 {
108 if (strcmp(dir, in_dir))
109 return EINA_TRUE;
110 }
111
112 return EINA_FALSE;
113}
114
115/**
116 * @brief Check if the given application is installed.
117 *
118 * @param exe The name of the application
119 * @return EINA_TRUE if the exe is in PATH and is executable,
120 * EINA_FALSE otherwise.
121 *
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 * EINA_FALSE, otherwise it returns EINA_TRUE.
126 */
127EAPI Eina_Bool
128ecore_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 */
158EAPI Eina_List *
159ecore_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 */