about summary refs log tree commit diff
path: root/mlist.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-12-13 15:21:00 +0100
committerChristian Neukirchen <chneukirchen@gmail.com>2016-12-13 15:21:00 +0100
commit6464ba584539365219f84326012fee87f71bde54 (patch)
tree19edcfcf7cf6145d0c368a2c52713cec40dad6dc /mlist.c
parent2ef903a06547f9461dd6769baf7a19f4dd17dc39 (diff)
downloadmblaze-6464ba584539365219f84326012fee87f71bde54.tar.gz
mblaze-6464ba584539365219f84326012fee87f71bde54.tar.xz
mblaze-6464ba584539365219f84326012fee87f71bde54.zip
mlist: read directory names from stdin when used non-interactively
Closes #7.
Diffstat (limited to 'mlist.c')
-rw-r--r--mlist.c108
1 files changed, 57 insertions, 51 deletions
diff --git a/mlist.c b/mlist.c
index 0fade35..0f7bd88 100644
--- a/mlist.c
+++ b/mlist.c
@@ -11,6 +11,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "blaze822.h"
+
 #define lc(c) ((c) | 0x20)
 #define uc(c) ((c) & 0xdf)
 
@@ -158,6 +160,56 @@ listdir(char *dir)
 }
 #endif
 
+void
+listarg(char *arg)
+{
+	struct stat st;
+	if (stat(arg, &st) < 0)
+		return;
+	if (S_ISDIR(st.st_mode)) {
+		char subdir[PATH_MAX];
+		struct stat st2;
+		int maildir = 0;
+
+		long gcount = icount;
+		long gunseen = iunseen;
+		long gflagged = iflagged;
+		long gmatched = imatched;
+
+		icount = 0;
+		iunseen = 0;
+		iflagged = 0;
+
+		snprintf(subdir, sizeof subdir, "%s/cur", arg);
+		if (stat(subdir, &st2) == 0) {
+			maildir = 1;
+			if (Cflag >= 0 && Nflag <= 0)
+				listdir(subdir);
+		}
+
+		snprintf(subdir, sizeof subdir, "%s/new", arg);
+		if (stat(subdir, &st2) == 0) {
+			maildir = 1;
+			if (Nflag >= 0 && Cflag <= 0)
+				listdir(subdir);
+		}
+
+		if (!maildir)
+			listdir(arg);
+
+		if (iflag && imatched)
+			printf("%6ld unseen  %3ld flagged  %6ld msg  %s\n",
+			    iunseen, iflagged, icount, arg);
+
+		icount = gcount;
+		iunseen = gunseen;
+		iflagged = gflagged;
+		imatched = gmatched;
+	} else if (S_ISREG(st.st_mode)) {
+		list(0, arg);
+	}
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -189,14 +241,11 @@ main(int argc, char *argv[])
 			    "Usage: mlist [-DFPRST] [-X str]\n"
 			    "             [-dfprst] [-x str]\n"
 			    "             [-N | -n | -C | -c]\n"
-			    "             [-i] dirs...\n"
+			    "             [-i] [dirs...]\n"
 			);
 			exit(1);
 		}
 
-	if (optind == argc)
-		goto usage;
-
         int i;
 	
 	for (i = 0, flagsum = 0, flagset = 0; (size_t)i < sizeof flags; i++) {
@@ -206,53 +255,10 @@ main(int argc, char *argv[])
 			flagsum++;
 	}
 
-        for (i = optind; i < argc; i++) {
-		struct stat st;
-		if (stat(argv[i], &st) < 0)
-			continue;
-		if (S_ISDIR(st.st_mode)) {
-			char subdir[PATH_MAX];
-			struct stat st2;
-			int maildir = 0;
-
-			long gcount = icount;
-			long gunseen = iunseen;
-			long gflagged = iflagged;
-			long gmatched = imatched;
-
-			icount = 0;
-			iunseen = 0;
-			iflagged = 0;
-
-			snprintf(subdir, sizeof subdir, "%s/cur", argv[i]);
-			if (stat(subdir, &st2) == 0) {
-				maildir = 1;
-				if (Cflag >= 0 && Nflag <= 0)
-					listdir(subdir);
-			}
-
-			snprintf(subdir, sizeof subdir, "%s/new", argv[i]);
-			if (stat(subdir, &st2) == 0) {
-				maildir = 1;
-				if (Nflag >= 0 && Cflag <= 0)
-					listdir(subdir);
-			}
-
-			if (!maildir)
-				listdir(argv[i]);
-
-			if (iflag && imatched)
-				printf("%6ld unseen  %3ld flagged  %6ld msg  %s\n",
-				    iunseen, iflagged, icount, argv[i]);
-
-			icount = gcount;
-			iunseen = gunseen;
-			iflagged = gflagged;
-			imatched = gmatched;
-		} else if (S_ISREG(st.st_mode)) {
-			list(0, argv[i]);
-		}
-        }
+	if (optind == argc && isatty(0))
+		goto usage;
+	else
+		blaze822_loop(argc-optind, argv+optind, listarg);
 
 	if (iflag && imatched)
 		printf("%6ld unseen  %3ld flagged  %6ld msg\n",