From 92b2a3c90af03a839fab06efb96429c9d4976ddd Mon Sep 17 00:00:00 2001 From: Jon Trowbridge Date: Thu, 6 Dec 2001 23:13:56 +0000 Subject: Added. Executes the given file and returns its output as a string. 2001-12-06 Jon Trowbridge * e-msg-composer.c (executed_file_output): Added. Executes the given file and returns its output as a string. (get_file_content): Stats the file to see if it is executable. If it is, execute it and return the string. If not, just read the file and return the contents. svn path=/trunk/; revision=14917 --- composer/ChangeLog | 8 ++++++ composer/e-msg-composer.c | 67 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/composer/ChangeLog b/composer/ChangeLog index d8445443cb..548c2f98dd 100644 --- a/composer/ChangeLog +++ b/composer/ChangeLog @@ -1,3 +1,11 @@ +2001-12-06 Jon Trowbridge + + * e-msg-composer.c (executed_file_output): Added. Executes the + given file and returns its output as a string. + (get_file_content): Stats the file to see if it is executable. + If it is, execute it and return the string. If not, just read + the file and return the contents. + 2001-11-30 Jeffrey Stedfast * e-msg-composer.c (setup_ui): Change the FileSend tooltip the be diff --git a/composer/e-msg-composer.c b/composer/e-msg-composer.c index f1d3645d7c..b52295314c 100644 --- a/composer/e-msg-composer.c +++ b/composer/e-msg-composer.c @@ -44,6 +44,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -684,28 +687,70 @@ read_file_content (gint fd) } static char * -get_file_content (const gchar *file_name, gboolean convert, guint flags) +executed_file_output (const char *file_name) { + GByteArray *contents; + FILE *in; + char buf[4096]; + int n; + char *body; + + g_return_val_if_fail (file_name && *file_name, NULL); + + in = popen (file_name, "r"); + if (in == NULL) + return NULL; + + contents = g_byte_array_new (); + while ((n = fread (buf, 1, 4096, in)) > 0) { + g_byte_array_append (contents, buf, n); + } + g_byte_array_append (contents, "\0", 1); + + body = (n < 0) ? NULL : (char *) contents->data; + g_byte_array_free (contents, (n < 0)); + + pclose (in); + + return body; +} + +static char * +get_file_content (const char *file_name, gboolean convert, guint flags) +{ + struct stat statbuf; gint fd; char *raw; char *html; + char *msg = NULL; + + if (stat (file_name, &statbuf) == -1) + return g_strdup (""); + + if (statbuf.st_mode & S_IXUSR) { + + raw = executed_file_output (file_name); + if (raw == NULL) { + msg = g_strdup_printf (_("Error while executing file %s:\n" + "%s"), file_name, g_strerror (errno)); + } - fd = open (file_name, O_RDONLY | O_CREAT, 0775); - - raw = read_file_content (fd); - - if (raw == NULL) { - char *msg; + } else { - msg = g_strdup_printf (_("Error while reading file %s:\n" - "%s"), file_name, g_strerror (errno)); + fd = open (file_name, O_RDONLY | O_CREAT, 0775); + raw = read_file_content (fd); + if (raw == NULL) { + msg = g_strdup_printf (_("Error while reading file %s:\n" + "%s"), file_name, g_strerror (errno)); + } + close (fd); + } + if (msg != NULL) { gnome_error_dialog (msg); g_free (msg); - close (fd); return g_strdup (""); } - close (fd); html = convert ? e_text_to_html (raw, flags) : raw; -- cgit v1.2.3