1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: getpass.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
22 ***************************************************************************/
24 /* This file is a reimplementation of the previous one, due to license
29 #ifndef HAVE_GETPASS_R
30 /* this file is only for systems without getpass_r() */
52 /* The last #include file should be: */
53 #if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
58 /* VMS implementation */
62 /* #include iosbdef */
63 char *getpass_r(const char *prompt
, char *buffer
, size_t buflen
)
68 /* MSK, 23-JAN-2004, iosbdef.h wasn't in VAX V7.2 or CC 6.4 */
69 /* distribution so I created this. May revert back later to */
70 /* struct _iosb iosb; */
73 short int iosb$w_status
; /* status */
74 short int iosb$w_bcnt
; /* byte count */
75 int unused
; /* unused */
78 $
DESCRIPTOR(ttdesc
, "TT");
81 sts
= sys$
assign(&ttdesc
, &chan
,0,0);
83 sts
= sys$
qiow(0, chan
,
84 IO$_READPROMPT
| IO$M_NOECHO
,
85 &iosb
, 0, 0, buffer
, buflen
, 0, 0,
86 prompt
, strlen(prompt
));
88 if((sts
& 1) && (iosb
.iosb$w_status
&1))
89 buffer
[iosb
.iosb$w_bcnt
] = '\0';
91 sts
= sys$
dassgn(chan
);
93 return buffer
; /* we always return success */
100 /* Windows implementation */
105 #define getch() getchar()
108 #if defined(WIN32) || defined(__SYMBIAN32__)
110 char *getpass_r(const char *prompt
, char *buffer
, size_t buflen
)
113 fputs(prompt
, stderr
);
115 for(i
=0; i
<buflen
; i
++) {
117 if ( buffer
[i
] == '\r' || buffer
[i
] == '\n' ) {
122 if ( buffer
[i
] == '\b')
123 /* remove this letter and if this is not the first key, remove the
124 previous one as well */
127 #ifndef __SYMBIAN32__
128 /* since echo is disabled, print a newline */
131 /* if user didn't hit ENTER, terminate buffer */
135 return buffer
; /* we always return success */
138 #endif /* WIN32 || __SYMBIAN32__ */
141 /* NetWare implementation */
142 #ifdef __NOVELL_LIBC__
144 char *getpass_r(const char *prompt
, char *buffer
, size_t buflen
)
146 return getpassword(prompt
, buffer
, buflen
);
150 char *getpass_r(const char *prompt
, char *buffer
, size_t buflen
)
154 printf("%s", prompt
);
156 buffer
[i
++] = getch();
157 if (buffer
[i
-1] == '\b') {
158 /* remove this letter and if this is not the first key,
159 remove the previous one as well */
167 } else if (buffer
[i
-1] != 13) {
170 } while ((buffer
[i
-1] != 13) && (i
< buflen
));
175 #endif /* __NOVELL_LIBC__ */
179 #ifndef DONE /* not previously provided */
181 #ifdef HAVE_TERMIOS_H
182 #define struct_term struct termios
185 #define struct_term struct termio
191 static bool ttyecho(bool enable
, int fd
)
194 static struct_term withecho
;
195 static struct_term noecho
;
198 /* disable echo by extracting the current 'withecho' mode and remove the
199 ECHO bit and set back the struct */
200 #ifdef HAVE_TERMIOS_H
201 tcgetattr(fd
, &withecho
);
203 noecho
.c_lflag
&= ~ECHO
;
204 tcsetattr(fd
, TCSANOW
, &noecho
);
205 #else /* HAVE_TERMIOS_H */
207 ioctl(fd
, TCGETA
, &withecho
);
209 noecho
.c_lflag
&= ~ECHO
;
210 ioctl(fd
, TCSETA
, &noecho
);
211 #else /* HAVE_TERMIO_H */
212 /* neither HAVE_TERMIO_H nor HAVE_TERMIOS_H, we can't disable echo! */
213 (void)fd
; /* prevent compiler warning on unused variable */
214 return FALSE
; /* not disabled */
217 return TRUE
; /* disabled */
220 /* re-enable echo, assumes we disabled it before (and set the structs we
221 now use to reset the terminal status) */
222 #ifdef HAVE_TERMIOS_H
223 tcsetattr(fd
, TCSAFLUSH
, &withecho
);
224 #else /* HAVE_TERMIOS_H */
226 ioctl(fd
, TCSETA
, &withecho
);
228 /* neither HAVE_TERMIO_H nor HAVE_TERMIOS_H */
229 return FALSE
; /* not enabled */
232 return TRUE
; /* enabled */
236 char *getpass_r(const char *prompt
, /* prompt to display */
237 char *password
, /* buffer to store password in */
238 size_t buflen
) /* size of buffer to store password in */
242 int fd
=open("/dev/tty", O_RDONLY
);
244 fd
= 1; /* use stdin if the tty couldn't be used */
246 disabled
= ttyecho(FALSE
, fd
); /* disable terminal echo */
248 fputs(prompt
, stderr
);
249 nread
=read(fd
, password
, buflen
);
251 password
[--nread
]=0; /* zero terminate where enter is stored */
253 password
[0]=0; /* got nothing */
256 /* if echo actually was disabled, add a newline */
258 (void)ttyecho(TRUE
, fd
); /* enable echo */
264 return password
; /* return pointer to buffer */
268 #endif /* HAVE_GETPASS_R */