2005-06-11 06:29:09 -07:00
|
|
|
/*
|
|
|
|
* vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
|
|
|
|
*/
|
|
|
|
#include "ecore_file_private.h"
|
2005-09-27 19:17:30 -07:00
|
|
|
#include <ctype.h>
|
2005-06-11 06:29:09 -07:00
|
|
|
|
2005-09-07 01:50:25 -07:00
|
|
|
static int init = 0;
|
|
|
|
static Ecore_List *__ecore_file_path_bin;
|
2005-06-11 06:29:09 -07:00
|
|
|
|
|
|
|
static Ecore_List *_ecore_file_path_from_env(const char *env);
|
|
|
|
|
|
|
|
int
|
|
|
|
ecore_file_path_init(void)
|
|
|
|
{
|
2005-09-07 01:57:08 -07:00
|
|
|
if (++init != 1) return init;
|
2005-06-11 06:29:09 -07:00
|
|
|
__ecore_file_path_bin = _ecore_file_path_from_env("PATH");
|
2005-09-07 01:50:25 -07:00
|
|
|
return init;
|
2005-06-11 06:29:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ecore_file_path_shutdown(void)
|
|
|
|
{
|
2005-09-07 01:57:08 -07:00
|
|
|
if (--init != 0) return init;
|
2005-06-11 06:29:09 -07:00
|
|
|
ecore_list_destroy(__ecore_file_path_bin);
|
2005-09-07 01:50:25 -07:00
|
|
|
return init;
|
2005-06-11 06:29:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ecore_list_append(path, strdup(last));
|
2005-09-27 22:57:41 -07:00
|
|
|
last = p + 1;
|
2005-06-11 06:29:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p > last)
|
2005-09-27 22:57:41 -07:00
|
|
|
ecore_list_append(path, strdup(last));
|
2005-06-11 06:29:09 -07:00
|
|
|
|
|
|
|
free(env_path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-09-27 22:57:41 -07:00
|
|
|
ecore_file_app_installed(const char *exe)
|
2005-06-11 06:29:09 -07:00
|
|
|
{
|
|
|
|
char *dir;
|
2005-09-29 10:25:38 -07:00
|
|
|
char buf[PATH_MAX], buf2[PATH_MAX];
|
2005-06-11 06:29:09 -07:00
|
|
|
|
2005-09-27 22:57:41 -07:00
|
|
|
if (!exe) return 0;
|
2005-09-29 10:25:38 -07:00
|
|
|
if (realpath(exe, buf) && ecore_file_can_exec(buf)) return 1;
|
2005-06-11 06:29:09 -07:00
|
|
|
|
|
|
|
ecore_list_goto_first(__ecore_file_path_bin);
|
|
|
|
while ((dir = ecore_list_next(__ecore_file_path_bin)) != NULL)
|
|
|
|
{
|
2005-09-27 22:57:41 -07:00
|
|
|
snprintf(buf, sizeof(buf), "%s/%s", dir, exe);
|
2005-09-29 10:25:38 -07:00
|
|
|
if (realpath(buf, buf2) && ecore_file_can_exec(buf2)) return 1;
|
2005-06-11 06:29:09 -07:00
|
|
|
}
|
2005-09-27 22:57:41 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-11 06:29:09 -07:00
|
|
|
|
2005-09-27 22:57:41 -07:00
|
|
|
char *
|
|
|
|
ecore_file_app_exe_get(const char *app)
|
|
|
|
{
|
|
|
|
char *p, *pp, *exe1 = NULL, *exe2 = NULL;
|
|
|
|
char *exe;
|
|
|
|
int in_quot_dbl = 0, in_quot_sing = 0;
|
|
|
|
|
|
|
|
p = (char *)app;
|
|
|
|
while ((*p) && (isspace(*p))) p++;
|
|
|
|
exe1 = p;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
if (in_quot_sing)
|
|
|
|
{
|
|
|
|
if (*p == '\'')
|
|
|
|
in_quot_sing = 0;
|
|
|
|
}
|
|
|
|
else if (in_quot_dbl)
|
|
|
|
{
|
|
|
|
if (*p == '\"')
|
|
|
|
in_quot_dbl = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*p == '\'')
|
|
|
|
in_quot_sing = 1;
|
|
|
|
else if (*p == '\"')
|
|
|
|
in_quot_dbl = 1;
|
|
|
|
if ((isspace(*p)) && (!((p > app) && (p[-1] != '\\'))))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
exe2 = p;
|
|
|
|
if (exe2 == exe1) return NULL;
|
|
|
|
exe = malloc(exe2 - exe1 + 1);
|
|
|
|
if (!exe) return NULL;
|
|
|
|
p = exe1;
|
|
|
|
in_quot_dbl = 0;
|
|
|
|
in_quot_sing = 0;
|
|
|
|
pp = exe;
|
|
|
|
while (*p)
|
2005-09-27 19:26:08 -07:00
|
|
|
{
|
2005-09-27 22:57:41 -07:00
|
|
|
if (in_quot_sing)
|
2005-09-27 19:08:52 -07:00
|
|
|
{
|
2005-09-27 22:57:41 -07:00
|
|
|
if (*p == '\'')
|
|
|
|
in_quot_sing = 0;
|
|
|
|
else
|
2005-09-27 19:08:52 -07:00
|
|
|
{
|
2005-09-27 22:57:41 -07:00
|
|
|
*pp = *p;
|
|
|
|
pp++;
|
2005-09-27 19:08:52 -07:00
|
|
|
}
|
|
|
|
}
|
2005-09-27 22:57:41 -07:00
|
|
|
else if (in_quot_dbl)
|
|
|
|
{
|
|
|
|
if (*p == '\"')
|
|
|
|
in_quot_dbl = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* techcincally this is wrong. double quotes also accept
|
|
|
|
* special chars:
|
|
|
|
*
|
|
|
|
* $, `, \
|
|
|
|
*/
|
|
|
|
*pp = *p;
|
|
|
|
pp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* technically we should handle special chars:
|
|
|
|
*
|
|
|
|
* $, `, \, etc.
|
|
|
|
*/
|
|
|
|
if ((p > app) && (p[-1] == '\\'))
|
|
|
|
{
|
|
|
|
if (*p != '\n')
|
|
|
|
{
|
|
|
|
*pp = *p;
|
|
|
|
pp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (*p == '\'')
|
|
|
|
in_quot_sing = 1;
|
|
|
|
else if (*p == '\"')
|
|
|
|
in_quot_dbl = 1;
|
|
|
|
else if (isspace(*p))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pp = *p;
|
|
|
|
pp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p++;
|
2005-09-27 19:08:52 -07:00
|
|
|
}
|
2005-09-27 22:57:41 -07:00
|
|
|
*pp = 0;
|
|
|
|
return exe;
|
2005-06-11 06:29:09 -07:00
|
|
|
}
|