bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * A function to read the passphrase either from the terminal or from |
| 3 | * an open file descriptor. |
| 4 | * |
| 5 | * Public domain. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <unistd.h> |
| 12 | #include <sys/ioctl.h> |
| 13 | #include <sys/stat.h> |
| 14 | |
| 15 | #include "c.h" |
| 16 | #include "xgetpass.h" |
| 17 | |
| 18 | char *xgetpass(int pfd, const char *prompt) |
| 19 | { |
| 20 | char *pass = NULL; |
| 21 | int len = 0, i; |
| 22 | |
| 23 | if (pfd < 0) /* terminal */ |
| 24 | return getpass(prompt); |
| 25 | |
| 26 | for (i=0; ; i++) { |
| 27 | if (i >= len-1) { |
| 28 | char *tmppass = pass; |
| 29 | len += 128; |
| 30 | |
| 31 | pass = realloc(tmppass, len); |
| 32 | if (!pass) { |
| 33 | pass = tmppass; /* the old buffer hasn't changed */ |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | if (pass && (read(pfd, pass + i, 1) != 1 || |
| 38 | pass[i] == '\n' || pass[i] == 0)) |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | if (pass) |
| 43 | pass[i] = '\0'; |
| 44 | return pass; |
| 45 | } |
| 46 | |