From 35c8f904dfbc0b2133e36d73fb013bf07d865477 Mon Sep 17 00:00:00 2001 From: Kim Woelders Date: Sun, 10 Sep 2006 14:29:05 +0000 Subject: [PATCH] Refactor/simplify, use execApplication in stead of plain fork/exec. SVN revision: 25691 --- src/E.h | 3 +- src/actions.c | 221 +++++++++++++++++++++++-------------------------- src/main.c | 33 +++----- src/mod-misc.c | 2 +- 4 files changed, 118 insertions(+), 141 deletions(-) diff --git a/src/E.h b/src/E.h index bcb8a1d9..9430a8d5 100644 --- a/src/E.h +++ b/src/E.h @@ -553,7 +553,8 @@ void ActionsHandleMotion(void); int ActionsEnd(EWin * ewin); void About(void); -int execApplication(const char *params); +#define EXEC_SET_LANG 0x01 +int execApplication(const char *params, int flags); void Espawn(int argc, char **argv); void EspawnCmd(const char *cmd); diff --git a/src/actions.c b/src/actions.c index 81ae6407..ec8bf122 100644 --- a/src/actions.c +++ b/src/actions.c @@ -26,141 +26,130 @@ #include "file.h" #include "user.h" -static void -runApp(const char *exe, const char *params) +int +execApplication(const char *params, int flags) { + char exe[FILEPATH_LEN_MAX]; char *sh; char *path; char *real_exec; int fd; + if (!params) + return -1; + + sscanf(params, "%4000s", exe); + if (exe[0] == '\0') + return -1; + if (fork()) - return; + return 0; setsid(); /* Close all file descriptors except the std 3 */ for (fd = 3; fd < 1024; fd++) close(fd); - LangExport(); + /* Set up env stuff */ + if (flags & EXEC_SET_LANG) + LangExport(); sh = usershell(getuid()); - if (exe) + + path = pathtoexec(exe); + if (path) { - path = pathtoexec(exe); - if (path) - { - Efree(path); - real_exec = Emalloc(strlen(params) + 6); - if (!real_exec) - return; - sprintf(real_exec, "exec %s", params); - execl(sh, sh, "-c", real_exec, NULL); - exit(0); - } + Efree(path); - if (!Mode.wm.startup) - { - path = pathtofile(exe); - if (!path) - { - /* absolute path */ - if (isabspath(exe)) - DialogAlertOK(_ - ("There was an error running the program:\n" - "%s\n" - "This program could not be executed.\n" - "This is because the file does not exist.\n"), - exe); - /* relative path */ - else - DialogAlertOK(_ - ("There was an error running the program:\n" - "%s\n" - "This program could not be executed.\n" - "This is most probably because this " - "program is not in the\n" - "path for your shell which is %s. " - "I suggest you read the manual\n" - "page for that shell and read up how to " - "change or add to your\n" - "execution path.\n"), exe, sh); - } - else - /* it is a node on the filing sys */ - { - /* it's a file */ - if (isfile(path)) - { - /* can execute it */ - if (canexec(path)) - DialogAlertOK(_ - ("There was an error running the program:\n" - "%s\n" - "This program could not be executed.\n" - "I am unsure as to why you could not " - "do this. The file exists,\n" - "is a file, and you are allowed to " - "execute it. I suggest you look\n" - "into this.\n"), path); - /* not executable file */ - else - DialogAlertOK(_ - ("There was an error running the program:\n" - "%s\n" - "This program could not be executed.\n" - "This is because the file exists, is a " - "file, but you are unable\n" - "to execute it because you do not " - "have execute " - "access to this file.\n"), path); - } - /* it's not a file */ - else - { - /* its a dir */ - if (isdir(path)) - DialogAlertOK(_ - ("There was an error running the program:\n" - "%s\n" - "This program could not be executed.\n" - "This is because the file is in fact " - "a directory.\n"), path); - /* its not a file or a dir */ - else - DialogAlertOK(_ - ("There was an error running the program:\n" - "%s\n" - "This program could not be executed.\n" - "This is because the file is not a " - "regular file.\n"), path); - } - Efree(path); - } - } - exit(100); + real_exec = Emalloc(strlen(params) + 6); + if (!real_exec) + return -1; + sprintf(real_exec, "exec %s", params); + + execl(sh, sh, "-c", real_exec, NULL); + /* We should not get here - invalid shell? */ } - real_exec = Emalloc(strlen(params) + 6); - if (!real_exec) - return; - sprintf(real_exec, "exec %s", params); - execl(sh, sh, "-c", real_exec, NULL); - exit(0); -} -int -execApplication(const char *params) -{ - char exe[FILEPATH_LEN_MAX]; - const char *s = params; - - if (!s) - return -1; - - sscanf(s, "%4000s", exe); - runApp(exe, s); - - return 0; + if (!Mode.wm.startup) + { + path = pathtofile(exe); + if (!path) + { + /* absolute path */ + if (isabspath(exe)) + DialogAlertOK(_ + ("There was an error running the program:\n" + "%s\n" + "This program could not be executed.\n" + "This is because the file does not exist.\n"), + exe); + /* relative path */ + else + DialogAlertOK(_ + ("There was an error running the program:\n" + "%s\n" + "This program could not be executed.\n" + "This is most probably because this " + "program is not in the\n" + "path for your shell which is %s. " + "I suggest you read the manual\n" + "page for that shell and read up how to " + "change or add to your\n" + "execution path.\n"), exe, sh); + } + else + /* it is a node on the filing sys */ + { + /* it's a file */ + if (isfile(path)) + { + /* can execute it */ + if (canexec(path)) + DialogAlertOK(_ + ("There was an error running the program:\n" + "%s\n" + "This program could not be executed.\n" + "I am unsure as to why you could not " + "do this. The file exists,\n" + "is a file, and you are allowed to " + "execute it. I suggest you look\n" + "into this.\n"), path); + /* not executable file */ + else + DialogAlertOK(_ + ("There was an error running the program:\n" + "%s\n" + "This program could not be executed.\n" + "This is because the file exists, is a " + "file, but you are unable\n" + "to execute it because you do not " + "have execute " + "access to this file.\n"), path); + } + /* it's not a file */ + else + { + /* its a dir */ + if (isdir(path)) + DialogAlertOK(_ + ("There was an error running the program:\n" + "%s\n" + "This program could not be executed.\n" + "This is because the file is in fact " + "a directory.\n"), path); + /* its not a file or a dir */ + else + DialogAlertOK(_ + ("There was an error running the program:\n" + "%s\n" + "This program could not be executed.\n" + "This is because the file is not a " + "regular file.\n"), path); + } + Efree(path); + } + } + exit(100); } void diff --git a/src/main.c b/src/main.c index 78ef7d30..9b42cbd9 100644 --- a/src/main.c +++ b/src/main.c @@ -443,39 +443,26 @@ EoptHelp(void) static void RunDocBrowser(void) { - char file[FILEPATH_LEN_MAX]; + char buf[FILEPATH_LEN_MAX]; - Esnprintf(file, sizeof(file), "%s/edox", EDirBin()); - if (!canexec(file)) + Esnprintf(buf, sizeof(buf), "%s/edox", EDirBin()); + if (!canexec(buf)) return; - Esnprintf(file, sizeof(file), "%s/E-docs", EDirRoot()); - if (!canread(file)) + Esnprintf(buf, sizeof(buf), "%s/E-docs", EDirRoot()); + if (!canread(buf)) return; - if (fork()) - return; - - Esnprintf(file, sizeof(file), "exec %s/edox %s/E-docs", - EDirBin(), EDirRoot()); - - execl(usershell(getuid()), usershell(getuid()), "-c", (char *)file, NULL); - - exit(0); + Esnprintf(buf, sizeof(buf), "%s/edox %s/E-docs", EDirBin(), EDirRoot()); + execApplication(buf, 0); } static void RunMenuGen(void) { - char file[FILEPATH_LEN_MAX]; + char buf[FILEPATH_LEN_MAX]; - if (fork()) - return; - - LangExport(); - - Esnprintf(file, sizeof(file), "exec %s/scripts/e_gen_menu", EDirRoot()); - execl(usershell(getuid()), usershell(getuid()), "-c", (char *)file, NULL); - exit(0); + Esnprintf(buf, sizeof(buf), "%s/scripts/e_gen_menu", EDirRoot()); + execApplication(buf, EXEC_SET_LANG); } static void diff --git a/src/mod-misc.c b/src/mod-misc.c index 9cb4b8a6..47b79e9f 100644 --- a/src/mod-misc.c +++ b/src/mod-misc.c @@ -173,7 +173,7 @@ static void MiscIpcExec(const char *params, Client * c __UNUSED__) { if (params) - execApplication(params); + execApplication(params, EXEC_SET_LANG); else IpcPrintf("exec what?\n"); }