]> diplodocus.org Git - mdfrm/commitdiff
Break main up, now it only does the chdir(2) calls and then hands
authorepg <>
Mon, 2 Sep 2002 00:55:47 +0000 (00:55 +0000)
committerepg <>
Mon, 2 Sep 2002 00:55:47 +0000 (00:55 +0000)
off to the new frm function, which uses the new scan_message
function.

mdfrm.c

diff --git a/mdfrm.c b/mdfrm.c
index fb69052..89c2ae2 100644 (file)
--- a/mdfrm.c
+++ b/mdfrm.c
@@ -29,7 +29,8 @@ enum {
     SUBJLEN = 9,                /* "Subject: " */
 
     /* Number of characters for each field we actually output.  This
-     * must also be changed in the printf functions at the end.
+     * must also be changed in the printf functions at the end of the
+     * frm function.
      */
     FROMCOUNT = 24,
     SUBJCOUNT = 53
@@ -37,34 +38,57 @@ enum {
 
 static char rcsid[] = "$Id$";
 
-int
-main(int argc, char *argv[])
+static void
+scan_message(char *filename, char *fromheader, char *subjheader)
 {
-    char *dirname;
-    char buf[PATH_MAX];
-    DIR *dir;
-    struct dirent *dirent;
-    char *filename;
     FILE *file;
-    char *line, *fromheader, *subjheader;
+    char *line;
     size_t len, fromcount, subjcount;
 
-    if (argv[1] && argv[1][0] == '-' && argv[1][1] == 'v') {
-        puts(rcsid);
-        return 0;
+    if (!(file = fopen(filename, "r"))) {
+        err(EX_OSERR, "Failed fopen(%s)", filename);
     }
 
-    if (!(dirname = argv[1])) {
-        if (!(dirname = getenv("MAILDIR"))) {
-            if (!(dirname = getenv("HOME"))) {
-                errx(EX_UNAVAILABLE, "Could not find maildir.");
+    fromheader[0] = subjheader[0] = '\0';
+    fromcount = subjcount = 0;
+    while ((line = fgetln(file, &len))) {
+        if (len <= 1) {
+            break;
+        }
+        line[len - 1] = '\0';
+
+        if (len > FROMLEN && strncmp(line, "From: ", FROMLEN) == 0) {
+            line += FROMLEN;
+            len -= FROMLEN;
+            if (len < FROMCOUNT) {
+                fromcount = len;
             } else {
-                snprintf(buf, PATH_MAX, "%s/Maildir", dirname);
-                dirname = buf;
+                fromcount = FROMCOUNT;
             }
+            strncpy(fromheader, line, fromcount);
+        } else if (len > SUBJLEN && strncmp(line, "Subject: ", SUBJLEN) == 0) {
+            line += SUBJLEN;
+            len -= SUBJLEN;
+            if (len < SUBJCOUNT) {
+                subjcount = len;
+            } else {
+                subjcount = SUBJCOUNT;
+            }
+            strncpy(subjheader, line, subjcount);
         }
     }
 
+    fclose(file);
+}
+
+static void
+frm(char *dirname)
+{
+    DIR *dir;
+    struct dirent *dirent;
+    char *filename;
+    char *fromheader, *subjheader;
+
     if (chdir(dirname) != 0) {
         err(EX_OSERR, "Failed chdir(%s)", dirname);
     }
@@ -86,46 +110,36 @@ main(int argc, char *argv[])
             continue;
         }
 
-        if (!(file = fopen(filename, "r"))) {
-            err(EX_OSERR, "Failed fopen(%s)", filename);
-        }
-
-        fromheader[0] = subjheader[0] = '\0';
-        fromcount = subjcount = 0;
-        while ((line = fgetln(file, &len))) {
-            if (len <= 1) {
-                break;
-            }
-            line[len - 1] = '\0';
-
-            if (len > FROMLEN
-                && strncmp(line, "From: ", FROMLEN) == 0) {
-                line += FROMLEN;
-                len -= FROMLEN;
-                if (len < FROMCOUNT) {
-                    fromcount = len;
-                } else {
-                    fromcount = FROMCOUNT;
-                }
-                strncpy(fromheader, line, fromcount);
-            } else if (len > SUBJLEN
-                       && strncmp(line, "Subject: ", SUBJLEN) == 0) {
-                line += SUBJLEN;
-                len -= SUBJLEN;
-                if (len < SUBJCOUNT) {
-                    subjcount = len;
-                } else {
-                    subjcount = SUBJCOUNT;
-                }
-                strncpy(subjheader, line, subjcount);
-            }
-        }
-
-        fclose(file);
+        scan_message(filename, fromheader, subjheader);
 
         printf("%-24s  ", fromheader);
         printf("%-53s\n", subjheader);
     }
+}
+
+int
+main(int argc, char *argv[])
+{
+    char *dirname;
+    char buf[PATH_MAX];
+
+    if (argv[1] && argv[1][0] == '-' && argv[1][1] == 'v') {
+        puts(rcsid);
+        return 0;
+    }
+
+    if (!(dirname = argv[1])) {
+        if (!(dirname = getenv("MAILDIR"))) {
+            if (!(dirname = getenv("HOME"))) {
+                errx(EX_UNAVAILABLE, "Could not find maildir.");
+            } else {
+                snprintf(buf, PATH_MAX, "%s/Maildir", dirname);
+                dirname = buf;
+            }
+        }
+    }
+
+    frm(dirname);
 
     return 0;
 }