Refactor various file property test functions.
SVN revision: 27987
This commit is contained in:
parent
01d8e455b3
commit
f600c9bce5
|
@ -67,11 +67,8 @@ execApplication(const char *params, int flags)
|
|||
|
||||
sh = usershell(getuid());
|
||||
|
||||
path = pathtoexec(exe);
|
||||
if (path)
|
||||
if (path_canexec(exe))
|
||||
{
|
||||
Efree(path);
|
||||
|
||||
real_exec = Emalloc(strlen(params) + 6);
|
||||
if (!real_exec)
|
||||
return -1;
|
||||
|
@ -83,7 +80,7 @@ execApplication(const char *params, int flags)
|
|||
|
||||
if (!Mode.wm.startup)
|
||||
{
|
||||
path = pathtofile(exe);
|
||||
path = path_test(exe, EFILE_ANY);
|
||||
if (!path)
|
||||
{
|
||||
/* absolute path */
|
||||
|
|
257
src/file.c
257
src/file.c
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
|
||||
* Copyright (C) 2007 Kim Woelders
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
|
@ -123,115 +124,43 @@ E_rm(const char *s)
|
|||
unlink(s);
|
||||
}
|
||||
|
||||
#if 0 /* Unused */
|
||||
void
|
||||
E_cp(const char *s, const char *ss)
|
||||
int
|
||||
file_test(const char *s, unsigned int test)
|
||||
{
|
||||
int i;
|
||||
FILE *f, *ff;
|
||||
unsigned char buf[1];
|
||||
struct stat st;
|
||||
int mode;
|
||||
|
||||
if ((!s) || (!ss) || (!*s) || (!*ss))
|
||||
return;
|
||||
if (!exists(s))
|
||||
return;
|
||||
i = filesize(s);
|
||||
f = fopen(s, "r");
|
||||
if (!f)
|
||||
return;
|
||||
ff = fopen(ss, "w");
|
||||
if (!ff)
|
||||
if (!s || !*s)
|
||||
return 0;
|
||||
|
||||
#define EFILE_ALL (EFILE_ANY | EFILE_REG | EFILE_DIR)
|
||||
if (test & EFILE_ALL)
|
||||
{
|
||||
fclose(f);
|
||||
return;
|
||||
if (stat(s, &st) < 0)
|
||||
return 0;
|
||||
if ((test & EFILE_REG) && !S_ISREG(st.st_mode))
|
||||
return 0;
|
||||
if ((test & EFILE_DIR) && !S_ISDIR(st.st_mode))
|
||||
return 0;
|
||||
}
|
||||
while (fread(buf, 1, 1, f))
|
||||
fwrite(buf, 1, 1, ff);
|
||||
fclose(f);
|
||||
fclose(ff);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0 /* Unused */
|
||||
char *
|
||||
cwd(void)
|
||||
{
|
||||
char *s;
|
||||
char ss[FILEPATH_LEN_MAX];
|
||||
#define EPERM_ALL (EPERM_R | EPERM_W | EPERM_X)
|
||||
if (test & EPERM_ALL)
|
||||
{
|
||||
mode = 0;
|
||||
if (test & EPERM_R)
|
||||
mode |= R_OK;
|
||||
if (test & EPERM_W)
|
||||
mode |= W_OK;
|
||||
if (test & EPERM_X)
|
||||
mode |= X_OK;
|
||||
if (access(s, mode))
|
||||
return 0;
|
||||
}
|
||||
|
||||
getcwd(ss, FILEPATH_LEN_MAX);
|
||||
s = Estrdup(ss);
|
||||
return s;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
exists(const char *s)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if ((!s) || (!*s))
|
||||
return 0;
|
||||
if (stat(s, &st) < 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
isdir(const char *s)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if ((!s) || (!*s))
|
||||
return 0;
|
||||
if (stat(s, &st) < 0)
|
||||
return 0;
|
||||
if (S_ISDIR(st.st_mode))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
isfile(const char *s)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if ((!s) || (!*s))
|
||||
return 0;
|
||||
if (stat(s, &st) < 0)
|
||||
return 0;
|
||||
if (S_ISREG(st.st_mode))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
canread(const char *s)
|
||||
{
|
||||
if ((!s) || (!*s))
|
||||
return 0;
|
||||
|
||||
return 1 + access(s, R_OK);
|
||||
}
|
||||
|
||||
int
|
||||
canwrite(const char *s)
|
||||
{
|
||||
if ((!s) || (!*s))
|
||||
return 0;
|
||||
|
||||
return 1 + access(s, W_OK);
|
||||
}
|
||||
|
||||
int
|
||||
canexec(const char *s)
|
||||
{
|
||||
if ((!s) || (!*s))
|
||||
return 0;
|
||||
|
||||
return 1 + access(s, X_OK);
|
||||
}
|
||||
|
||||
time_t
|
||||
moddate(const char *s)
|
||||
{
|
||||
|
@ -312,7 +241,7 @@ isabspath(const char *path)
|
|||
}
|
||||
|
||||
const char *
|
||||
FileExtension(const char *file)
|
||||
fileext(const char *file)
|
||||
{
|
||||
const char *p;
|
||||
|
||||
|
@ -348,115 +277,57 @@ fullfileof(const char *path)
|
|||
}
|
||||
|
||||
char *
|
||||
pathtoexec(const char *file)
|
||||
path_test(const char *file, unsigned int test)
|
||||
{
|
||||
char *p, *cp, *ep;
|
||||
char *s;
|
||||
int len, exelen;
|
||||
char *cp, *ep;
|
||||
char *s, *p;
|
||||
unsigned int len, exelen;
|
||||
|
||||
if (!file)
|
||||
return NULL;
|
||||
|
||||
if (isabspath(file))
|
||||
{
|
||||
if (canexec(file))
|
||||
if (file_test(file, test))
|
||||
return Estrdup(file);
|
||||
return NULL;
|
||||
}
|
||||
p = getenv("PATH");
|
||||
if (!p)
|
||||
cp = getenv("PATH");
|
||||
if (!cp)
|
||||
return Estrdup(file);
|
||||
if (!file)
|
||||
return NULL;
|
||||
|
||||
cp = p;
|
||||
exelen = strlen(file);
|
||||
while ((ep = strchr(cp, ':')) != NULL)
|
||||
s = NULL;
|
||||
ep = cp;
|
||||
for (; ep; cp = ep + 1)
|
||||
{
|
||||
len = ep - cp;
|
||||
s = Emalloc(len + 1);
|
||||
if (s)
|
||||
{
|
||||
strncpy(s, cp, len);
|
||||
s[len] = 0;
|
||||
s = Erealloc(s, len + 2 + exelen);
|
||||
if (!s)
|
||||
return NULL;
|
||||
strcat(s, "/");
|
||||
strcat(s, file);
|
||||
if (canexec(s))
|
||||
return s;
|
||||
Efree(s);
|
||||
}
|
||||
cp = ep + 1;
|
||||
}
|
||||
len = strlen(cp);
|
||||
s = Emalloc(len + 1);
|
||||
if (s)
|
||||
{
|
||||
strncpy(s, cp, len);
|
||||
s[len] = 0;
|
||||
s = Erealloc(s, len + 2 + exelen);
|
||||
if (!s)
|
||||
return NULL;
|
||||
strcat(s, "/");
|
||||
strcat(s, file);
|
||||
if (canexec(s))
|
||||
ep = strchr(cp, ':');
|
||||
len = (ep) ? (unsigned int)(ep - cp) : strlen(cp);
|
||||
if (len == 0)
|
||||
continue;
|
||||
p = Erealloc(s, len + exelen + 2);
|
||||
if (!p)
|
||||
break;
|
||||
s = p;
|
||||
memcpy(s, cp, len);
|
||||
s[len] = '/';
|
||||
memcpy(s + len + 1, file, exelen + 1);
|
||||
if (file_test(s, test))
|
||||
return s;
|
||||
Efree(s);
|
||||
}
|
||||
if (s)
|
||||
Efree(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *
|
||||
pathtofile(const char *file)
|
||||
int
|
||||
path_canexec(const char *file)
|
||||
{
|
||||
char *p, *cp, *ep;
|
||||
char *s;
|
||||
int len, exelen;
|
||||
|
||||
if (isabspath(file))
|
||||
{
|
||||
if (exists(file))
|
||||
return Estrdup(file);
|
||||
}
|
||||
p = getenv("PATH");
|
||||
if (!p)
|
||||
return Estrdup(file);
|
||||
if (!file)
|
||||
return NULL;
|
||||
cp = p;
|
||||
exelen = strlen(file);
|
||||
while ((ep = strchr(cp, ':')) != NULL)
|
||||
{
|
||||
len = ep - cp;
|
||||
s = Emalloc(len + 1);
|
||||
if (s)
|
||||
{
|
||||
strncpy(s, cp, len);
|
||||
s[len] = 0;
|
||||
s = Erealloc(s, len + 2 + exelen);
|
||||
if (!s)
|
||||
return NULL;
|
||||
strcat(s, "/");
|
||||
strcat(s, file);
|
||||
if (exists(s))
|
||||
return s;
|
||||
Efree(s);
|
||||
}
|
||||
cp = ep + 1;
|
||||
}
|
||||
len = strlen(cp);
|
||||
s = Emalloc(len + 1);
|
||||
if (s)
|
||||
{
|
||||
strncpy(s, cp, len);
|
||||
s[len] = 0;
|
||||
s = Erealloc(s, len + 2 + exelen);
|
||||
if (!s)
|
||||
return NULL;
|
||||
strcat(s, "/");
|
||||
strcat(s, file);
|
||||
if (exists(s))
|
||||
return s;
|
||||
Efree(s);
|
||||
}
|
||||
return NULL;
|
||||
s = path_test(file, EFILE_REG | EPERM_X);
|
||||
if (!s)
|
||||
return 0;
|
||||
Efree(s);
|
||||
return 1;
|
||||
}
|
||||
|
|
31
src/file.h
31
src/file.h
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
|
||||
* Copyright (C) 2007 Kim Woelders
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
|
@ -30,22 +31,34 @@ void E_mv(const char *s, const char *ss);
|
|||
void E_rm(const char *s);
|
||||
char **E_ls(const char *dir, int *num);
|
||||
|
||||
int exists(const char *s);
|
||||
int isdir(const char *s);
|
||||
int isfile(const char *s);
|
||||
int canread(const char *s);
|
||||
int canwrite(const char *s);
|
||||
int canexec(const char *s);
|
||||
#define EFILE_ANY 0x01
|
||||
#define EFILE_REG 0x02
|
||||
#define EFILE_DIR 0x04
|
||||
|
||||
#define EPERM_R 0x10
|
||||
#define EPERM_W 0x20
|
||||
#define EPERM_X 0x40
|
||||
#define EPERM_RWX (EPERM_R | EPERM_W | EPERM_X)
|
||||
|
||||
int file_test(const char *s, unsigned int test);
|
||||
|
||||
#define exists(s) file_test(s, EFILE_ANY)
|
||||
#define isdir(s) file_test(s, EFILE_DIR)
|
||||
#define isfile(s) file_test(s, EFILE_REG)
|
||||
#define canread(s) file_test(s, EFILE_REG | EPERM_R)
|
||||
#define canexec(s) file_test(s, EFILE_REG | EPERM_X)
|
||||
|
||||
time_t moddate(const char *s);
|
||||
int fileinode(const char *s);
|
||||
int filedev_map(int dev);
|
||||
int filedev(const char *s);
|
||||
|
||||
int isabspath(const char *s);
|
||||
const char *FileExtension(const char *file);
|
||||
const char *fileext(const char *s);
|
||||
char *fileof(const char *s);
|
||||
char *fullfileof(const char *s);
|
||||
char *pathtoexec(const char *file);
|
||||
char *pathtofile(const char *file);
|
||||
|
||||
char *path_test(const char *file, unsigned int test);
|
||||
int path_canexec(const char *file);
|
||||
|
||||
#endif /* _FILE_H_ */
|
||||
|
|
38
src/main.c
38
src/main.c
|
@ -453,7 +453,7 @@ RunDocBrowser(void)
|
|||
Esnprintf(buf, sizeof(buf), "%s/edox", EDirBin());
|
||||
if (!canexec(buf))
|
||||
return;
|
||||
Esnprintf(buf, sizeof(buf), "%s/E-docs", EDirRoot());
|
||||
Esnprintf(buf, sizeof(buf), "%s/E-docs/MAIN", EDirRoot());
|
||||
if (!canread(buf))
|
||||
return;
|
||||
|
||||
|
@ -577,36 +577,12 @@ Etmp(char *s)
|
|||
static void
|
||||
EDirCheck(const char *dir)
|
||||
{
|
||||
if (!isdir(dir))
|
||||
{
|
||||
Alert(_("The directory %s is apparently not a directory\n"
|
||||
"This is a fatal condition.\n" "Please remove this file\n"),
|
||||
dir);
|
||||
EExit(1);
|
||||
}
|
||||
if (!canexec(dir))
|
||||
{
|
||||
Alert(_("Do not have execute access to %s\n"
|
||||
"This is a fatal condition.\n"
|
||||
"Please check the ownership and permissions of this\n"
|
||||
"directory and take steps to rectify this.\n"), dir);
|
||||
EExit(1);
|
||||
}
|
||||
if (!canread(dir))
|
||||
{
|
||||
Alert(_("Do not have read access to %s\n" "This is a fatal condition.\n"
|
||||
"Please check the ownership and permissions of this\n"
|
||||
"directory and take steps to rectify this.\n"), dir);
|
||||
EExit(1);
|
||||
}
|
||||
if (!canwrite(dir))
|
||||
{
|
||||
Alert(_("Do not have write access to %s\n"
|
||||
"This is a fatal condition.\n"
|
||||
"Please check the ownership and permissions of this\n"
|
||||
"directory and take steps to rectify this.\n"), dir);
|
||||
EExit(1);
|
||||
}
|
||||
if (file_test(dir, EFILE_DIR | EPERM_RWX))
|
||||
return;
|
||||
|
||||
Alert(_("%s must be a directory in which you have\n"
|
||||
"read, write, and execute permission.\n"));
|
||||
EExit(1);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -175,7 +175,7 @@ MenuLoadFromDirectory(Menu * m)
|
|||
if ((*(list[i]) == '.') || (stat(ss, &st) < 0))
|
||||
continue;
|
||||
|
||||
ext = FileExtension(ss);
|
||||
ext = fileext(ss);
|
||||
if (S_ISDIR(st.st_mode))
|
||||
{
|
||||
Esnprintf(s, sizeof(s), "%s/%s:%s", dir, list[i], MenuGetName(m));
|
||||
|
@ -323,7 +323,7 @@ FillFlatFileMenu(Menu * m, const char *name, const char *file)
|
|||
else
|
||||
{
|
||||
char *txt = NULL, *icon = NULL, *act = NULL;
|
||||
char *params = NULL, *tmp = NULL, wd[4096];
|
||||
char *params = NULL, wd[4096];
|
||||
|
||||
MenuItem *mi;
|
||||
ImageClass *icc = NULL;
|
||||
|
@ -333,7 +333,6 @@ FillFlatFileMenu(Menu * m, const char *name, const char *file)
|
|||
icon = field(s, 1);
|
||||
act = field(s, 2);
|
||||
params = field(s, 3);
|
||||
tmp = NULL;
|
||||
if (icon && exists(icon))
|
||||
{
|
||||
Esnprintf(wd, sizeof(wd), "__FM.%s", icon);
|
||||
|
@ -344,10 +343,8 @@ FillFlatFileMenu(Menu * m, const char *name, const char *file)
|
|||
if ((act) && (!strcmp(act, "exec")) && (params))
|
||||
{
|
||||
word(params, 1, wd);
|
||||
tmp = pathtoexec(wd);
|
||||
if (tmp)
|
||||
if (path_canexec(wd))
|
||||
{
|
||||
Efree(tmp);
|
||||
Esnprintf(s, sizeof(s), "exec %s", params);
|
||||
mi = MenuItemCreate(txt, icc, s, NULL);
|
||||
MenuAddItem(m, mi);
|
||||
|
@ -490,7 +487,6 @@ MenuCreateFromGnome(const char *name, Menu * parent, MenuStyle * ms,
|
|||
if (f)
|
||||
{
|
||||
char *iname, *exec, *texec, *en_name;
|
||||
char *tmp;
|
||||
|
||||
iname = exec = texec = en_name = NULL;
|
||||
|
||||
|
@ -522,14 +518,12 @@ MenuCreateFromGnome(const char *name, Menu * parent, MenuStyle * ms,
|
|||
fclose(f);
|
||||
if ((iname) && (exec))
|
||||
{
|
||||
tmp = NULL;
|
||||
if (texec)
|
||||
tmp = pathtoexec(texec);
|
||||
if ((tmp) || (!texec))
|
||||
{
|
||||
if (tmp)
|
||||
Efree(tmp);
|
||||
int ok = 1;
|
||||
|
||||
if (texec)
|
||||
ok = path_canexec(texec);
|
||||
if (ok)
|
||||
{
|
||||
Esnprintf(s, sizeof(s), "exec %s", exec);
|
||||
mi = MenuItemCreate(iname, NULL, s, NULL);
|
||||
MenuAddItem(m, mi);
|
||||
|
|
|
@ -1898,17 +1898,12 @@ MenuConfigLoad(FILE * fs)
|
|||
if (!strcmp(s2, "exec"))
|
||||
{
|
||||
char buf[1024];
|
||||
char *path;
|
||||
|
||||
params = atword(s, 3);
|
||||
if (params)
|
||||
{
|
||||
sscanf(atword(s, 3), "%1000s", buf);
|
||||
path = pathtoexec(buf);
|
||||
if (path)
|
||||
Efree(path);
|
||||
else
|
||||
ok = 0;
|
||||
ok = path_canexec(buf);
|
||||
}
|
||||
}
|
||||
if (ok)
|
||||
|
|
|
@ -142,7 +142,7 @@ append_merge_dir(char *dir, char ***list, int *count)
|
|||
}
|
||||
else if (isfile(ss))
|
||||
{
|
||||
if (!FileExtension(ss) || strcmp(FileExtension(ss), "etheme"))
|
||||
if (!fileext(ss) || strcmp(fileext(ss), "etheme"))
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue