7100d95e4971037d1f4236a33f8dc713d2d063b8
[framework/uifw/ecore.git] / src / lib / ecore_file / ecore_file_path.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include "ecore_file_private.h"
10
11 static int init = 0;
12 static Ecore_List *__ecore_file_path_bin = NULL;
13
14 static Ecore_List *_ecore_file_path_from_env(const char *env);
15
16 int
17 ecore_file_path_init(void)
18 {
19    if (++init != 1) return init;
20    __ecore_file_path_bin = _ecore_file_path_from_env("PATH");
21    ecore_list_free_cb_set(__ecore_file_path_bin, free);
22    return init;
23 }
24
25 int
26 ecore_file_path_shutdown(void)
27 {
28    if (--init != 0) return init;
29    ecore_list_destroy(__ecore_file_path_bin);
30    __ecore_file_path_bin = NULL;
31    return init;
32 }
33
34 Ecore_List *
35 _ecore_file_path_from_env(const char *env)
36 {
37    Ecore_List *path;
38    char *env_path, *p, *last;
39
40    path = ecore_list_new();
41
42    env_path = getenv(env);
43    if (!env_path)
44      return path;
45
46    env_path = strdup(env_path);
47    last = env_path;
48    for (p = env_path; *p; p++)
49      {
50         if (*p == ':')
51           *p = '\0';
52
53         if (!*p)
54           {
55              if (!ecore_file_path_dir_exists(last))
56                ecore_list_append(path, strdup(last));
57              last = p + 1;
58           }
59      }
60    if (p > last)
61      ecore_list_append(path, strdup(last));
62
63    free(env_path);
64    return path;
65 }
66
67 /**
68  * Check if the given directory is in PATH
69  * @param The name of the directory to search in PATH
70  * @return 1 if the directory exist in PATH, 0 otherwise
71  */
72 EAPI int
73 ecore_file_path_dir_exists(const char *in_dir)
74 {
75    char *dir;
76
77    if (!__ecore_file_path_bin) return 0;
78    ecore_list_first_goto(__ecore_file_path_bin);
79    while ((dir = ecore_list_next(__ecore_file_path_bin)) != NULL)
80      {
81         if (!strcmp(dir, in_dir)) return 1;
82      }
83    return 0;
84 }
85
86 /**
87  * Check if the given application is installed
88  * @param  exe The name of the application
89  * @return 1 if the exe is in PATH and is executable
90  * 
91  * This function check if the given name exist in PATH and is executable 
92  */
93 EAPI int
94 ecore_file_app_installed(const char *exe)
95 {
96    char *dir;
97    char  buf[PATH_MAX];
98
99    if (!exe) return 0;
100    if (ecore_file_can_exec(exe)) return 1;
101
102    ecore_list_first_goto(__ecore_file_path_bin);
103    while ((dir = ecore_list_next(__ecore_file_path_bin)) != NULL)
104      {
105         snprintf(buf, sizeof(buf), "%s/%s", dir, exe);
106         if (ecore_file_can_exec(buf)) return 1;
107      }
108    return 0;
109 }
110
111 /**
112  * Get a list of all the applications installed on the system
113  * @return An Ecore_List containing all the executable files in the system
114  */
115 EAPI Ecore_List *
116 ecore_file_app_list(void)
117 {
118    Ecore_List *list, *files;
119    char  buf[PATH_MAX], *dir, *exe;
120    
121    list = ecore_list_new();
122    if (!list) return NULL;
123    ecore_list_free_cb_set(list, free);
124    ecore_list_first_goto(__ecore_file_path_bin);
125    while ((dir = ecore_list_next(__ecore_file_path_bin)) != NULL)
126      {
127         files = ecore_file_ls(dir);
128         if (files)
129           {
130              ecore_list_first_goto(files);
131              while ((exe = ecore_list_next(files)) != NULL)
132                {
133                   snprintf(buf, sizeof(buf), "%s/%s", dir, exe);
134                   if ((ecore_file_can_exec(buf)) &&
135                       (!ecore_file_is_dir(buf)))
136                     ecore_list_append(list, strdup(buf));
137                }
138              ecore_list_destroy(files);
139           }
140      }
141    return list;
142 }