From 73251d2a2eef7f3b318fe40463817e47ea1eae4d Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Tue, 25 Jan 2000 14:04:49 +0000 Subject: [PATCH] r180: Updated filer action code to use exec instead of system (stops problems with shell special chars like space). --- ROX-Filer/src/action.c | 17 +++++++++++++---- ROX-Filer/src/dnd.c | 2 +- ROX-Filer/src/filer.c | 2 +- ROX-Filer/src/menu.c | 3 ++- ROX-Filer/src/run.c | 2 +- ROX-Filer/src/support.c | 32 +++++++++++++++++++++++++++++--- ROX-Filer/src/support.h | 5 +++-- ROX-Filer/src/type.c | 2 +- 8 files changed, 51 insertions(+), 14 deletions(-) diff --git a/ROX-Filer/src/action.c b/ROX-Filer/src/action.c index 5495742e..bb81b2bd 100644 --- a/ROX-Filer/src/action.c +++ b/ROX-Filer/src/action.c @@ -601,8 +601,12 @@ static gboolean do_copy(char *path, char *dest) } else { - g_string_sprintf(message, "cp -dpRf %s %s", path, dest_path); - if (system(message->str)) + char *argv[] = {"cp", "-dpRf", NULL, NULL, NULL}; + + argv[2] = path; + argv[3] = dest_path; + + if (fork_exec_wait(argv)) { g_string_sprintf(message, "!ERROR: %s\n", "Copy failed\n"); @@ -619,6 +623,8 @@ static gboolean do_move(char *path, char *dest) char *dest_path; char *leaf; gboolean retval = TRUE; + char *argv[] = {"mv", "-f", NULL, NULL, NULL}; + int error; leaf = strrchr(path, '/'); if (!leaf) @@ -676,8 +682,10 @@ static gboolean do_move(char *path, char *dest) } } - g_string_sprintf(message, "mv -f %s %s", path, dest); - if (system(message->str) == 0) + argv[2] = path; + argv[3] = dest; + + if (fork_exec_wait(argv) == 0) { g_string_sprintf(message, "+%s", path); g_string_truncate(message, leaf - path); @@ -723,6 +731,7 @@ static gboolean do_link(char *path, char *dest) } /* Mount/umount this item */ +/* XXX: Spaces? */ static void do_mount(FilerWindow *filer_window, DirItem *item) { char *command; diff --git a/ROX-Filer/src/dnd.c b/ROX-Filer/src/dnd.c index 2fb5016c..5baeba6c 100644 --- a/ROX-Filer/src/dnd.c +++ b/ROX-Filer/src/dnd.c @@ -914,7 +914,7 @@ static void run_with_files(char *path, GSList *uri_list) argv[argc++] = NULL; - if (!spawn_full(argv, getenv("HOME"), 0)) + if (!spawn_full(argv, getenv("HOME"))) delayed_error("ROX-Filer", "Failed to fork() child process"); } diff --git a/ROX-Filer/src/filer.c b/ROX-Filer/src/filer.c index f3477f1d..a7d1cf65 100644 --- a/ROX-Filer/src/filer.c +++ b/ROX-Filer/src/filer.c @@ -1091,7 +1091,7 @@ void filer_openitem(FilerWindow *filer_window, int item_number, OpenFlags flags) argv[0] = full_path; - if (spawn_full(argv, getenv("HOME"), 0)) + if (spawn_full(argv, getenv("HOME"))) { if (close_window) { diff --git a/ROX-Filer/src/menu.c b/ROX-Filer/src/menu.c index 9e996791..21e6d4da 100644 --- a/ROX-Filer/src/menu.c +++ b/ROX-Filer/src/menu.c @@ -631,6 +631,7 @@ static gboolean copy_cb(char *initial, char *path) command = g_string_new(NULL); g_string_sprintf(command, "cp -a %s %s", initial, path); + /* XXX: Use system. In fact, use action! */ if (system(command->str)) { @@ -1142,7 +1143,7 @@ static void xterm_here(gpointer data, guint action, GtkWidget *widget) g_return_if_fail(window_with_focus != NULL); - if (!spawn_full(argv, window_with_focus->path, 0)) + if (!spawn_full(argv, window_with_focus->path)) report_error("ROX-Filer", "Failed to fork() child " "process"); } diff --git a/ROX-Filer/src/run.c b/ROX-Filer/src/run.c index 25198af8..2bc5741d 100644 --- a/ROX-Filer/src/run.c +++ b/ROX-Filer/src/run.c @@ -38,7 +38,7 @@ void run_app(char *path) apprun = g_string_new(path); argv[0] = g_string_append(apprun, "/AppRun")->str; - if (!spawn_full(argv, getenv("HOME"), 0)) + if (!spawn_full(argv, getenv("HOME"))) report_error("ROX-Filer", "Failed to fork() child process"); g_string_free(apprun, TRUE); diff --git a/ROX-Filer/src/support.c b/ROX-Filer/src/support.c index 01c285c2..87419e9f 100644 --- a/ROX-Filer/src/support.c +++ b/ROX-Filer/src/support.c @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -95,13 +96,13 @@ char *our_host_name() /* fork() and run a new program. * Returns the new PID, or 0 on failure. */ -int spawn(char **argv) +pid_t spawn(char **argv) { - return spawn_full(argv, NULL, 0); + return spawn_full(argv, NULL); } /* As spawn(), but cd to dir first (if dir is non-NULL) */ -int spawn_full(char **argv, char *dir, SpawnFlags flags) +pid_t spawn_full(char **argv, char *dir) { int child; @@ -201,3 +202,28 @@ char *format_size(unsigned long size) return buffer; } + +/* Fork and exec argv. Wait and return the child's exit status. + * -1 if spawn fails. + */ +int fork_exec_wait(char **argv) +{ + pid_t child; + int status = -1; + + child = spawn_full(argv, NULL); + + while (child) + { + if (waitpid(child, &status, 0) == -1) + { + if (errno != EINTR) + return -1; + } + else + break; + }; + + return status; +} + diff --git a/ROX-Filer/src/support.h b/ROX-Filer/src/support.h index 4a3fdb98..4f63bc4d 100644 --- a/ROX-Filer/src/support.h +++ b/ROX-Filer/src/support.h @@ -16,11 +16,12 @@ typedef enum {NONE} SpawnFlags; char *pathdup(char *path); GString *make_path(char *dir, char *leaf); char *our_host_name(); -int spawn(char **argv); -int spawn_full(char **argv, char *dir, SpawnFlags flags); +pid_t spawn(char **argv); +pid_t spawn_full(char **argv, char *dir); void debug_free_string(void *data); char *user_name(uid_t uid); char *group_name(gid_t gid); char *format_size(unsigned long size); +int fork_exec_wait(char **argv); #endif /* _SUPPORT_H */ diff --git a/ROX-Filer/src/type.c b/ROX-Filer/src/type.c index fef3f6a2..181ae508 100644 --- a/ROX-Filer/src/type.c +++ b/ROX-Filer/src/type.c @@ -223,7 +223,7 @@ gboolean type_open(char *path, MIME_type *type) else argv[0] = open; - if (!spawn_full(argv, getenv("HOME"), 0)) + if (!spawn_full(argv, getenv("HOME"))) { report_error("ROX-Filer", "Failed to fork() child process"); -- 2.11.4.GIT