tttm

git clone https://orangeshoelaces.net/git/tttm.git

cb05a5f83d82d6c7e503e9ba49474d9b6372e634

Author: Vasily Kolobkov on 06/03/2016

Committer: Vasily Kolobkov on 06/03/2016

Start sink up by Bernstein chaining

Stats

tttm.c | 29 +++-----
1 file changed, 9 insertions(+), 20 deletions(-)

Patch

diff --git a/tttm.c b/tttm.c
index ef0ce96..402be3d 100644
--- a/tttm.c
+++ b/tttm.c
@@ -22,16 +22,14 @@
 /* Retry fetching mail at most this many times after getting a nil fetch.  */
 #define MAXREFC 2
 
-static int	pipemsg(struct msgd *, int, const char *);
-static int	sink_init(const char *, pid_t *, int *);
+static int	pipemsg(struct msgd *, int, char **);
+static int	sink_init(char **, pid_t *, int *);
 static int	sink_free(pid_t, int);
 static void	fin(struct imapctx *, int, const char *);
 static void	terr(int, const char *);
 static void	twarn(int, const char *);
 static void	usage(void);
 
-extern char **environ;
-
 static size_t	 page;
 static char	 stortpl[] = "/tmp/tttm.XXXXXX";
 static char	*userkey = "IMAP_USER";
@@ -42,17 +40,13 @@ main(int argc, char **argv)
 {
 	int e;
 	struct imapctx con;
-	char *user, *pass, *sink;
+	char *user, *pass;
 	int stor;
 	int refetchc, batchsz, mpiped;
 	struct msgd bag[BATCHSZ], *m, *bend;
 	long sysvar;
 
-	argc--;
-	argv++;
-	if (argc == 1) {
-		sink = *argv;
-	} else {
+	if (argc < 2) {
 		usage();
 	}
 
@@ -116,7 +110,7 @@ main(int argc, char **argv)
 		}
 		refetchc = 0;
 		for (m = bag; m < bend; m++) {
-			if (pipemsg(m, stor, sink) == -1)
+			if (pipemsg(m, stor, argv + 1) == -1)
 				break;
 		}
 		mpiped = m - bag;
@@ -146,7 +140,7 @@ main(int argc, char **argv)
 }
 
 int
-pipemsg(struct msgd *m, int stor, const char *cmd)
+pipemsg(struct msgd *m, int stor, char **argv)
 {
 	int e, sin;
 	void (*origpipe)(int);
@@ -164,7 +158,7 @@ pipemsg(struct msgd *m, int stor, const char *cmd)
 		warn("failed to install SIGPIPE handler");
 		goto exit;
 	}
-	if (sink_init(cmd, &sid, &sin) == -1)
+	if (sink_init(argv, &sid, &sin) == -1)
 		goto csig;
 
 	for (scur = m->off, send = scur + m->len; scur < send; scur += wlen) {
@@ -213,11 +207,10 @@ pipemsg(struct msgd *m, int stor, const char *cmd)
 }
 
 int
-sink_init(const char *cmd, pid_t *sid, int *sin)
+sink_init(char **argv, pid_t *sid, int *sin)
 {
 	int e;
 	int pd[2], r, w;
-	char *args[4];
 
 	e = -1;
 	if (pipe(pd) == -1) {
@@ -237,11 +230,7 @@ sink_init(const char *cmd, pid_t *sid, int *sin)
 			warn("failed to setup sink");
 			_exit(1);
 		}
-		args[0] = "sh";
-		args[1] = "-c";
-		args[2] = (char *)cmd;
-		args[3] = 0;
-		execve("/bin/sh", args, environ);
+		execvp(argv[0], argv);
 		warn("failed to launch sink");
 		_exit(1);
 	}