[balsa] Clean up identity code



commit 0edb637a3f1e2500623931e0828cab6759f822f9
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Mon Apr 17 21:57:11 2017 -0400

    Clean up identity code
    
        * libbalsa/identity.c (libbalsa_identity_get_signature): use
        g_spawn_async instead of popen and libbalsa_readfile_nostat.
        * libbalsa/misc.c: libbalsa_readfile_nostat is no longer needed.
        * libbalsa/misc.h: ditto.

 ChangeLog           |    9 ++++++++
 libbalsa/identity.c |   54 +++++++++++++++++++++++---------------------------
 libbalsa/misc.c     |   46 -------------------------------------------
 libbalsa/misc.h     |    2 -
 4 files changed, 34 insertions(+), 77 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index c4e9380..8bf4919 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2017-04-17  Peter Bloomfield  <pbloomfield bellsouth net>
+
+       Clean up identity code
+
+       * libbalsa/identity.c (libbalsa_identity_get_signature): use
+       g_spawn_async instead of popen and libbalsa_readfile_nostat.
+       * libbalsa/misc.c: libbalsa_readfile_nostat is no longer needed.
+       * libbalsa/misc.h: ditto.
+
 2017-04-15  Peter Bloomfield  <pbloomfield bellsouth net>
 
        Fix some more conversion specifier mismatches
diff --git a/libbalsa/identity.c b/libbalsa/identity.c
index 3d85b5a..04971a3 100644
--- a/libbalsa/identity.c
+++ b/libbalsa/identity.c
@@ -320,6 +320,7 @@ gchar*
 libbalsa_identity_get_signature(LibBalsaIdentity* identity, GtkWindow *parent)
 {
     gchar *ret = NULL, *path;
+    gchar *retval;
 
     if (identity->signature_path == NULL ||
         *identity->signature_path == '\0')
@@ -327,52 +328,47 @@ libbalsa_identity_get_signature(LibBalsaIdentity* identity, GtkWindow *parent)
 
     path = libbalsa_expand_path(identity->signature_path);
     if(identity->sig_executable){
-        FILE *fp;
-
-        /* signature is executable */
-       fp = popen(path,"r");
-        if (fp) {
-            libbalsa_readfile_nostat(fp, &ret);
-            pclose(fp);
-        } else
-            libbalsa_information_parented
-                (parent, LIBBALSA_INFORMATION_ERROR,
-                 _("Error executing signature generator %s"),
-                 identity->signature_path);
+        GError *error = NULL;
+        gchar *argv[] = {"/bin/sh", "-c", path, NULL};
+
+        if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_DEFAULT, NULL, NULL,
+                          &ret, NULL, NULL, &error)) {
+            libbalsa_information_parented(parent, LIBBALSA_INFORMATION_ERROR,
+                                          _("Error executing signature generator “%s”: %s"),
+                                          identity->signature_path, error->message);
+            g_error_free(error);
+        }
     } else {
        GError *error = NULL;
 
        if (!g_file_get_contents(path, &ret, NULL, &error)) {
             libbalsa_information_parented(parent, LIBBALSA_INFORMATION_ERROR,
-               _("Cannot read signature file “%s”: %s"), identity->signature_path, error->message);
+                                          _("Cannot read signature file “%s”: %s"),
+                                          identity->signature_path, error->message);
                g_error_free(error);
        }
     }
-    if (ret != NULL) {
-        if(!libbalsa_utf8_sanitize(&ret, FALSE, NULL))
-            libbalsa_information_parented
-                (parent, LIBBALSA_INFORMATION_ERROR,
-                 _("Signature in %s is not a UTF-8 text."),
-                 identity->signature_path);
-    }
     g_free(path);
 
     if(ret == NULL) return NULL;
 
+    if (!libbalsa_utf8_sanitize(&ret, FALSE, NULL)) {
+        libbalsa_information_parented(parent, LIBBALSA_INFORMATION_ERROR,
+                                      _("Signature in %s is not a UTF-8 text."),
+                                      identity->signature_path);
+    }
+
     /* Prepend the separator if needed... */
 
     if (identity->sig_separator
-        && strncmp(ret, "--\n", 3)
-        && strncmp(ret, "-- \n", 4)) {
-        gchar *sig_tmp = g_strconcat("\n-- \n", ret, NULL);
-        g_free(ret);
-        ret = sig_tmp;
+        && !(g_str_has_prefix(ret, "--\n") || g_str_has_prefix(ret, "-- \n"))) {
+        retval = g_strconcat("\n-- \n", ret, NULL);
     } else {
-        gchar *sig_tmp = g_strconcat("\n", ret, NULL);
-        g_free(ret);
-        ret = sig_tmp;
+        retval = g_strconcat("\n", ret, NULL);
     }
-    return ret;
+    g_free(ret);
+
+    return retval;
 }
 
 void
diff --git a/libbalsa/misc.c b/libbalsa/misc.c
index 8aed2d2..33133a4 100644
--- a/libbalsa/misc.c
+++ b/libbalsa/misc.c
@@ -115,52 +115,6 @@ libbalsa_get_domainname(void)
     return NULL;
 }
 
-/* readfile_nostat is identical to readfile except it reads to EOF.
-   This enables the use of pipes to programs for such things as
-   the signature file.
-*/
-size_t libbalsa_readfile_nostat(FILE * fp, char **buf)
-{
-    size_t size;
-    GString *gstr;
-    char rbuf[512];
-    size_t rlen = 512;
-    int r;
-    int fd;
-
-    *buf = NULL;
-    fd = fileno(fp);
-    if (!fp)
-       return 0;
-
-    r = read(fd, rbuf, rlen-1);
-    if (r <= 0)
-       return 0;
-
-    rbuf[r] = '\0';
-    gstr = g_string_new(rbuf);
-
-    do {
-       if ((r = read(fd, rbuf, rlen-1)) != 0) {
-           /* chbm: if your sig is larger than 512 you deserve this */
-           if ((r < 0) && (errno != EAGAIN) && (errno != EINTR)) {
-               perror("Error reading file:");
-               g_string_free(gstr, TRUE);
-               return -1;
-           };
-           if (r > 0) {
-               rbuf[r] = '\0'; 
-               g_string_append(gstr,rbuf);
-           }
-       }
-    } while( r != 0 );
-
-    size = gstr->len;
-    *buf = g_string_free(gstr, FALSE);
-
-    return size;
-}
-
 /* libbalsa_find_word:
    searches given word delimited by blanks or string boundaries in given
    string. IS NOT case-sensitive.
diff --git a/libbalsa/misc.h b/libbalsa/misc.h
index ed21667..ec63140 100644
--- a/libbalsa/misc.h
+++ b/libbalsa/misc.h
@@ -91,8 +91,6 @@ GtkWidget *libbalsa_charset_button_new(void);
 LibBalsaTextAttribute libbalsa_text_attr_string(const gchar * string);
 LibBalsaTextAttribute libbalsa_text_attr_file(const gchar * filename);
 
-size_t libbalsa_readfile_nostat(FILE * fp, char **buf);
-
 gchar *libbalsa_get_domainname(void);
 #define libbalsa_urlencode(str) (g_uri_escape_string((str), NULL, FALSE))
 #define libbalsa_urldecode(str) (g_uri_unescape_string((str), NULL))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]