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