Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/master/askpass.c @ 9186:f70192e7ff3c HEAD
Added tag 1.2.0 for changeset 9ca9361d74ce
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 01 Jul 2009 21:52:54 +0200 |
parents | b9faf4db2a9f |
children | 00cd9aacd03c |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
1 /* Copyright (c) 2006-2009 Dovecot authors, see the included COPYING file */ |
3890 | 2 |
3 #include "lib.h" | |
4 #include "askpass.h" | |
5 | |
6 #include <stdio.h> | |
7 #include <termios.h> | |
8 #include <fcntl.h> | |
9 #include <unistd.h> | |
10 | |
11 void askpass(const char *prompt, char *buf, size_t buf_size) | |
12 { | |
13 struct termios old_tio, tio; | |
14 bool restore_tio = FALSE; | |
15 ssize_t ret; | |
16 size_t pos; | |
17 char ch; | |
18 int fd; | |
19 | |
20 if (!isatty(STDIN_FILENO)) | |
21 i_fatal("stdin isn't a TTY"); | |
22 | |
23 fputs(prompt, stderr); | |
24 fflush(stderr); | |
25 | |
26 fd = open("/dev/tty", O_RDONLY); | |
27 if (fd < 0) | |
28 i_fatal("open(/dev/tty) failed: %m"); | |
29 | |
30 /* turn off echo */ | |
31 if (tcgetattr(fd, &old_tio) == 0) { | |
32 restore_tio = TRUE; | |
33 tio = old_tio; | |
34 tio.c_lflag &= ~(ECHO | ECHONL); | |
35 (void)tcsetattr(fd, TCSAFLUSH, &tio); | |
36 } | |
37 | |
38 /* read the password */ | |
39 pos = 0; | |
40 while ((ret = read(fd, &ch, 1)) > 0) { | |
41 if (pos >= buf_size-1) | |
42 break; | |
43 if (ch == '\n' || ch == '\r') | |
44 break; | |
45 buf[pos++] = ch; | |
46 } | |
47 buf[pos] = '\0'; | |
48 | |
49 if (restore_tio) | |
50 (void)tcsetattr(fd, TCSAFLUSH, &old_tio); | |
51 | |
52 fputs("\n", stderr); fflush(stderr); | |
53 (void)close(fd); | |
54 } |