Suprisingly some OSes do not have getpass()
drop a dumb version in lib/getpass.c for missing plaforms. * configure.in: Check for getpass (); * lib/getpass.c: New file. * lib/Makefile.am: Updated.
Showing
2 changed files
with
102 additions
and
3 deletions
... | @@ -7,9 +7,9 @@ INCLUDES = -I${top_srcdir}/include | ... | @@ -7,9 +7,9 @@ INCLUDES = -I${top_srcdir}/include |
7 | libmailutils_a_SOURCES = basename.c daemon.c getopt.c getopt1.c md5.c \ | 7 | libmailutils_a_SOURCES = basename.c daemon.c getopt.c getopt1.c md5.c \ |
8 | mu_dbm.c getline.c xstrdup.c xmalloc.c argcv.c | 8 | mu_dbm.c getline.c xstrdup.c xmalloc.c argcv.c |
9 | 9 | ||
10 | EXTRA_DIST = alloca.c fnmatch.c setenv.c snprintf.c strchrnul.c strndup.c \ | 10 | EXTRA_DIST = alloca.c fnmatch.c fgetpwent.c getpass.c malloc.c obstack.c \ |
11 | strnlen.c strtok_r.c strsignal.c xstrtol.c vasprintf.c malloc.c realloc.c \ | 11 | realloc.c setenv.c snprintf.c strchrnul.c strndup.c strnlen.c strncasecmp.c \ |
12 | fgetpwent.c obstack.c | 12 | strcasecmp.c strtok_r.c strsignal.c xstrtol.c vasprintf.c |
13 | 13 | ||
14 | noinst_HEADERS = argcv.h error.h fnmatch.h getline.h getopt.h md5.h mu_dbm.h\ | 14 | noinst_HEADERS = argcv.h error.h fnmatch.h getline.h getopt.h md5.h mu_dbm.h\ |
15 | regex.h snprintf.h xalloc.h xstrtol.h obstack.h | 15 | regex.h snprintf.h xalloc.h xstrtol.h obstack.h | ... | ... |
lib/getpass.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Library Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | #include <stdio.h> | ||
19 | #include <string.h> | ||
20 | #include <stdlib.h> | ||
21 | #include <termios.h> | ||
22 | |||
23 | /* Alain: Parts originally from GNU Lib C. */ | ||
24 | |||
25 | static void | ||
26 | echo_off(int fd, struct termios *stored_settings) | ||
27 | { | ||
28 | struct termios new_settings; | ||
29 | tcgetattr (fd, stored_settings); | ||
30 | new_settings = *stored_settings; | ||
31 | new_settings.c_lflag &= (~ECHO); | ||
32 | tcsetattr (fd, TCSANOW, &new_settings); | ||
33 | } | ||
34 | |||
35 | static void | ||
36 | echo_on(int fd, struct termios *stored_settings) | ||
37 | { | ||
38 | tcsetattr (fd, TCSANOW, stored_settings); | ||
39 | } | ||
40 | |||
41 | char * | ||
42 | getpass (const char * prompt) | ||
43 | { | ||
44 | FILE *in, *out; | ||
45 | struct termios stored_settings; | ||
46 | static char *buf; | ||
47 | static size_t buf_size; | ||
48 | char *pbuf; | ||
49 | |||
50 | /* First pass initialize the buffer. */ | ||
51 | if (buf_size == 0) | ||
52 | { | ||
53 | buf_size = 256; | ||
54 | buf = calloc (1, buf_size); | ||
55 | if (buf == NULL) | ||
56 | return NULL; | ||
57 | } | ||
58 | else | ||
59 | memset (buf, '\0', buf_size); | ||
60 | |||
61 | /* Turn echoing off if it is on now. */ | ||
62 | echo_off (fileno (stdin), &stored_settings); | ||
63 | |||
64 | /* Write the prompt. */ | ||
65 | fputs (prompt, stdout); | ||
66 | fflush (stdout); | ||
67 | |||
68 | /* Read the password. */ | ||
69 | pbuf = fgets (buf, buf_size, stdin); | ||
70 | if (pbuf) | ||
71 | { | ||
72 | size_t nread = strlen (pbuf); | ||
73 | if (nread && pbuf[nread - 1] == '\n') | ||
74 | { | ||
75 | /* Remove the newline. */ | ||
76 | pbuf[nread - 1] = '\0'; | ||
77 | /* Write the newline that was not echoed. */ | ||
78 | putc ('\n', stdout); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | /* Restore the original setting. */ | ||
83 | echo_on (fileno (stdin), &stored_settings); | ||
84 | |||
85 | return pbuf; | ||
86 | } | ||
87 | |||
88 | #ifdef _GETPASS_STANDALONE_TEST | ||
89 | |||
90 | int | ||
91 | main () | ||
92 | { | ||
93 | char *p; | ||
94 | p = getpass ("my prompt: "); | ||
95 | if (p) | ||
96 | printf ("Passwd: %s\n", p); | ||
97 | return 0; | ||
98 | } | ||
99 | #endif |
-
Please register or sign in to post a comment