changeset 3890:6658a000051d HEAD

Forgot to add in last commit
author Timo Sirainen <tss@iki.fi>
date Sun, 15 Jan 2006 15:20:57 +0200
parents c7462001227b
children 2d0859490a2f
files src/master/askpass.c src/master/askpass.h
diffstat 2 files changed, 60 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/master/askpass.c	Sun Jan 15 15:20:57 2006 +0200
@@ -0,0 +1,54 @@
+/* Copyright (C) 2006 Timo Sirainen */
+
+#include "lib.h"
+#include "askpass.h"
+
+#include <stdio.h>
+#include <termios.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+void askpass(const char *prompt, char *buf, size_t buf_size)
+{
+        struct termios old_tio, tio;
+	bool restore_tio = FALSE;
+	ssize_t ret;
+	size_t pos;
+	char ch;
+	int fd;
+
+	if (!isatty(STDIN_FILENO))
+		i_fatal("stdin isn't a TTY");
+
+	fputs(prompt, stderr);
+	fflush(stderr);
+
+	fd = open("/dev/tty", O_RDONLY);
+	if (fd < 0)
+		i_fatal("open(/dev/tty) failed: %m");
+
+	/* turn off echo */
+	if (tcgetattr(fd, &old_tio) == 0) {
+		restore_tio = TRUE;
+		tio = old_tio;
+		tio.c_lflag &= ~(ECHO | ECHONL);
+		(void)tcsetattr(fd, TCSAFLUSH, &tio);
+	}
+
+	/* read the password */
+	pos = 0;
+	while ((ret = read(fd, &ch, 1)) > 0) {
+		if (pos >= buf_size-1)
+			break;
+		if (ch == '\n' || ch == '\r')
+			break;
+		buf[pos++] = ch;
+	}
+	buf[pos] = '\0';
+
+	if (restore_tio)
+		(void)tcsetattr(fd, TCSAFLUSH, &old_tio);
+
+	fputs("\n", stderr); fflush(stderr);
+	(void)close(fd);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/master/askpass.h	Sun Jan 15 15:20:57 2006 +0200
@@ -0,0 +1,6 @@
+#ifndef __ASKPASS_H
+#define __ASKPASS_H
+
+void askpass(const char *prompt, char *buf, size_t buf_size);
+
+#endif