summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/lib/ecore_file/ecore_file_path.c
blob: de2b4dc4d411ca4938586a056c0b3244772209b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include "ecore_file_private.h"

static int init = 0;
static Ecore_List *__ecore_file_path_bin = NULL;

static Ecore_List *_ecore_file_path_from_env(const char *env);

int
ecore_file_path_init(void)
{
   if (++init != 1) return init;
   __ecore_file_path_bin = _ecore_file_path_from_env("PATH");
   ecore_list_free_cb_set(__ecore_file_path_bin, free);
   return init;
}

int
ecore_file_path_shutdown(void)
{
   if (--init != 0) return init;
   ecore_list_destroy(__ecore_file_path_bin);
   __ecore_file_path_bin = NULL;
   return init;
}

Ecore_List *
_ecore_file_path_from_env(const char *env)
{
   Ecore_List *path;
   char *env_path, *p, *last;

   path = ecore_list_new();

   env_path = getenv(env);
   if (!env_path)
     return path;

   env_path = strdup(env_path);
   last = env_path;
   for (p = env_path; *p; p++)
     {
        if (*p == ':')
          *p = '\0';

        if (!*p)
          {
	     if (!ecore_file_path_dir_exists(last))
	       ecore_list_append(path, strdup(last));
             last = p + 1;
          }
     }
   if (p > last)
     ecore_list_append(path, strdup(last));

   free(env_path);
   return path;
}

/**
 * Check if the given directory is in PATH
 * @param The name of the directory to search in PATH
 * @return 1 if the directory exist in PATH, 0 otherwise
 */
EAPI int
ecore_file_path_dir_exists(const char *in_dir)
{
   char *dir;

   if (!__ecore_file_path_bin) return 0;
   ecore_list_first_goto(__ecore_file_path_bin);
   while ((dir = ecore_list_next(__ecore_file_path_bin)) != NULL)
     {
	if (!strcmp(dir, in_dir)) return 1;
     }
   return 0;
}

/**
 * Check if the given application is installed
 * @param  exe The name of the application
 * @return 1 if the exe is in PATH and is executable
 * 
 * This function check if the given name exist in PATH and is executable 
 */
EAPI int
ecore_file_app_installed(const char *exe)
{
   char *dir;
   char  buf[PATH_MAX];

   if (!exe) return 0;
   if (ecore_file_can_exec(exe)) return 1;

   ecore_list_first_goto(__ecore_file_path_bin);
   while ((dir = ecore_list_next(__ecore_file_path_bin)) != NULL)
     {
	snprintf(buf, sizeof(buf), "%s/%s", dir, exe);
	if (ecore_file_can_exec(buf)) return 1;
     }
   return 0;
}

/**
 * Get a list of all the applications installed on the system
 * @return An Ecore_List containing all the executable files in the system
 */
EAPI Ecore_List *
ecore_file_app_list(void)
{
   Ecore_List *list, *files;
   char  buf[PATH_MAX], *dir, *exe;
   
   list = ecore_list_new();
   if (!list) return NULL;
   ecore_list_free_cb_set(list, free);
   ecore_list_first_goto(__ecore_file_path_bin);
   while ((dir = ecore_list_next(__ecore_file_path_bin)) != NULL)
     {
	files = ecore_file_ls(dir);
	if (files)
	  {
	     ecore_list_first_goto(files);
	     while ((exe = ecore_list_next(files)) != NULL)
	       {
		  snprintf(buf, sizeof(buf), "%s/%s", dir, exe);
		  if ((ecore_file_can_exec(buf)) &&
		      (!ecore_file_is_dir(buf)))
		    ecore_list_append(list, strdup(buf));
	       }
	     ecore_list_destroy(files);
	  }
     }
   return list;
}