svn update: 48958 (latest:48959)
[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 <stdio.h>
10 #include <string.h>
11
12 #include "ecore_file_private.h"
13
14 static Eina_List *__ecore_file_path_bin = NULL;
15
16 static Eina_List *_ecore_file_path_from_env(const char *env);
17
18 void
19 ecore_file_path_init(void)
20 {
21    __ecore_file_path_bin = _ecore_file_path_from_env("PATH");
22 }
23
24 void
25 ecore_file_path_shutdown(void)
26 {
27    char *dir;
28
29    EINA_LIST_FREE(__ecore_file_path_bin, dir)
30      free(dir);
31 }
32
33 Eina_List *
34 _ecore_file_path_from_env(const char *env)
35 {
36    Eina_List *path = NULL;
37    char *env_path, *p, *last;
38
39    env_path = getenv(env);
40    if (!env_path)
41      return path;
42
43    env_path = strdup(env_path);
44    last = env_path;
45    for (p = env_path; *p; p++)
46      {
47         if (*p == ':')
48           *p = '\0';
49
50         if (!*p)
51           {
52              if (!ecore_file_path_dir_exists(last))
53                path = eina_list_append(path, strdup(last));
54              last = p + 1;
55           }
56      }
57    if (p > last)
58      path = eina_list_append(path, strdup(last));
59
60    free(env_path);
61    return path;
62 }
63
64 /**
65  * Check if the given directory is in PATH
66  * @param The name of the directory to search in PATH
67  * @return 1 if the directory exist in PATH, 0 otherwise
68  */
69 EAPI int
70 ecore_file_path_dir_exists(const char *in_dir)
71 {
72    Eina_List *l;
73    char *dir;
74
75    if (!__ecore_file_path_bin) return 0;
76    EINA_LIST_FOREACH(__ecore_file_path_bin, l, dir)
77      {
78         if (strcmp(dir, in_dir))
79           return 1;
80      }
81
82    return 0;
83 }
84
85 /**
86  * Check if the given application is installed
87  * @param  exe The name of the application
88  * @return 1 if the exe is in PATH and is executable
89  * 
90  * This function check if the given name exist in PATH and is executable 
91  */
92 EAPI int
93 ecore_file_app_installed(const char *exe)
94 {
95    Eina_List *l;
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    EINA_LIST_FOREACH(__ecore_file_path_bin, l, dir)
103      {
104         snprintf(buf, sizeof(buf), "%s/%s", dir, exe);
105         if (ecore_file_can_exec(buf))
106           return 1;
107      }
108
109    return 0;
110 }
111
112 /**
113  * Get a list of all the applications installed on the system
114  * @return An Eina_List containing all the executable files in the system
115  */
116 EAPI Eina_List *
117 ecore_file_app_list(void)
118 {
119    Eina_List *list = NULL;
120    Eina_List *files;
121    Eina_List *l;
122    char  buf[PATH_MAX], *dir, *exe;
123    
124    EINA_LIST_FOREACH(__ecore_file_path_bin, l, dir)
125      {
126         files = ecore_file_ls(dir);
127         EINA_LIST_FREE(files, exe)
128                {
129                   snprintf(buf, sizeof(buf), "%s/%s", dir, exe);
130                   if ((ecore_file_can_exec(buf)) &&
131                       (!ecore_file_is_dir(buf)))
132                list = eina_list_append(list, strdup(buf));
133              free(exe);
134           }
135      }
136
137    return list;
138 }