[dovecot-cvs] dovecot/src/master askpass.c, NONE, 1.1 askpass.h, NONE, 1.1

cras at dovecot.org cras at dovecot.org
Sun Jan 15 15:20:59 EET 2006


Update of /var/lib/cvs/dovecot/src/master
In directory talvi:/tmp/cvs-serv20337

Added Files:
	askpass.c askpass.h 
Log Message:
Forgot to add in last commit



--- NEW FILE: askpass.c ---
/* 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);
}

--- NEW FILE: askpass.h ---
#ifndef __ASKPASS_H
#define __ASKPASS_H

void askpass(const char *prompt, char *buf, size_t buf_size);

#endif



More information about the dovecot-cvs mailing list