]> diplodocus.org Git - nmh/commitdiff
get_file_info(): Flip logic throughout, reducing nesting.
authorRalph Corderoy <ralph@inputplus.co.uk>
Sun, 10 Sep 2017 13:22:29 +0000 (14:22 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Sun, 10 Sep 2017 13:48:05 +0000 (14:48 +0100)
Now a series of small paragraphs, each of which might return on error.
Also, the Cygwin oddity described in the comment needs no special
handling given the recent bug fixes so it can be deleted.

sbr/mime_type.c

index 47c6833415df05ca23ae30114088d940ab7cb8e2..a640110a9f48b1fdbe9554e88500553e311fa7b8 100644 (file)
@@ -114,63 +114,56 @@ mime_type(const char *file_name) {
  * Get information using proc about a file.
  */
 static char *
  * Get information using proc about a file.
  */
 static char *
-get_file_info(const char *proc, const char *file_name) {
-    char *cmd, *cp;
-    char *quotec = "'";
+get_file_info(const char *proc, const char *file_name)
+{
+    char *quotec;
+    char *cmd;
+    FILE *fp;
+    bool ok;
     char buf[max(BUFSIZ, 2048)];
     char buf[max(BUFSIZ, 2048)];
+    char *info;
+    char *needle;
 
 
-    if ((cp = strchr(file_name, '\''))) {
-        /* file_name contains a single quote. */
+    if (strchr(file_name, '\'')) {
         if (strchr(file_name, '"')) {
             inform("filenames containing both single and double quotes "
         if (strchr(file_name, '"')) {
             inform("filenames containing both single and double quotes "
-                   "are unsupported for attachment");
+                "are unsupported for attachment");
             return NULL;
         }
         quotec = "\"";
             return NULL;
         }
         quotec = "\"";
-    }
-
-    cp = NULL;
-    if ((cmd = concat(proc, " ", quotec, file_name, quotec, NULL))) {
-        FILE *fp;
-
-        if ((fp = popen(cmd, "r")) != NULL) {
-
-            buf[0] = '\0';
-            if (fgets(buf, sizeof buf, fp)) {
-                char *eol;
-
-                /* Skip leading <filename>:<whitespace>, if present. */
-                if ((cp = strchr(buf, ':')) != NULL) {
-                    ++cp;
-                    while (*cp  &&  isblank((unsigned char) *cp)) {
-                        ++cp;
-                    }
-                } else {
-                    cp = buf;
-                }
-
-                /* Truncate at newline (LF or CR), if present. */
-                if ((eol = strpbrk(cp, "\n\r")) != NULL) {
-                    *eol = '\0';
-                }
-            } else if (buf[0] == '\0') {
-                /* This can happen on Cygwin if the popen()
-                   mysteriously fails.  Return NULL so that the caller
-                   will use another method to determine the info. */
-                free (cp);
-                cp = NULL;
-            }
+    } else
+        quotec = "'";
 
 
-            (void) pclose(fp);
-        } else {
-            inform("no output from %s", cmd);
-        }
+    cmd = concat(proc, " ", quotec, file_name, quotec, NULL);
+    if (!cmd) {
+        inform("concat with \"%s\" failed, out of memory?", proc);
+        return NULL;
+    }
 
 
+    if ((fp = popen(cmd, "r")) == NULL) {
+        inform("no output from %s", cmd);
         free(cmd);
         free(cmd);
-    } else {
-        inform("concat with \"%s\" failed, out of memory?", proc);
+        return NULL;
     }
 
     }
 
-    return cp  ?  strdup(cp)  :  NULL;
+    ok = fgets(buf, sizeof buf, fp);
+    free(cmd);
+    (void)pclose(fp);
+    if (!ok)
+        return NULL;
+
+    /* s#^.*:[ \t]*##. */
+    info = buf;
+    if ((needle = strchr(info, ':'))) {
+        info = needle + 1;
+        while (isblank((unsigned char)*info))
+            info++;
+    }
+
+    /* s#[\n\r].*##. */
+    if ((needle = strpbrk(info, "\n\r")))
+        *needle = '\0';
+
+    return strdup(info);
 }
 #endif /* MIMETYPEPROC */
 }
 #endif /* MIMETYPEPROC */