1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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 ***************************************************************************/
23 /* This file is a reimplementation of the previous one, due to license
28 #ifndef HAVE_GETPASS_R
29 /* this file is only for systems without getpass_r() */
51 /* The last #include file should be: */
52 #if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
57 /* VMS implementation */
61 /* #include iosbdef */
62 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
67 /* MSK, 23-JAN-2004, iosbdef.h wasn't in VAX V7.2 or CC 6.4 */
68 /* distribution so I created this. May revert back later to */
69 /* struct _iosb iosb; */
72 short int iosb$w_status; /* status */
73 short int iosb$w_bcnt; /* byte count */
74 int unused; /* unused */
77 $DESCRIPTOR(ttdesc, "TT");
80 sts = sys$assign(&ttdesc, &chan,0,0);
82 sts = sys$qiow(0, chan,
83 IO$_READPROMPT | IO$M_NOECHO,
84 &iosb, 0, 0, buffer, buflen, 0, 0,
85 prompt, strlen(prompt));
87 if((sts & 1) && (iosb.iosb$w_status&1))
88 buffer[iosb.iosb$w_bcnt] = '\0';
90 sts = sys$dassgn(chan);
92 return buffer; /* we always return success */
99 /* Windows implementation */
104 #define getch() getchar()
107 #if defined(WIN32) || defined(__SYMBIAN32__)
109 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
112 fputs(prompt, stderr);
114 for(i=0; i<buflen; i++) {
115 buffer[i] = (char)getch();
116 if ( buffer[i] == '\r' || buffer[i] == '\n' ) {
121 if ( buffer[i] == '\b')
122 /* remove this letter and if this is not the first key, remove the
123 previous one as well */
126 #ifndef __SYMBIAN32__
127 /* since echo is disabled, print a newline */
130 /* if user didn't hit ENTER, terminate buffer */
134 return buffer; /* we always return success */
137 #endif /* WIN32 || __SYMBIAN32__ */
140 /* NetWare implementation */
141 #ifdef __NOVELL_LIBC__
143 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
145 return getpassword(prompt, buffer, buflen);
149 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
153 printf("%s", prompt);
155 buffer[i++] = getch();
156 if (buffer[i-1] == '\b') {
157 /* remove this letter and if this is not the first key,
158 remove the previous one as well */
166 } else if (buffer[i-1] != 13) {
169 } while ((buffer[i-1] != 13) && (i < buflen));
174 #endif /* __NOVELL_LIBC__ */
178 #ifndef DONE /* not previously provided */
180 #ifdef HAVE_TERMIOS_H
181 #define struct_term struct termios
184 #define struct_term struct termio
190 static bool ttyecho(bool enable, int fd)
193 static struct_term withecho;
194 static struct_term noecho;
197 /* disable echo by extracting the current 'withecho' mode and remove the
198 ECHO bit and set back the struct */
199 #ifdef HAVE_TERMIOS_H
200 tcgetattr(fd, &withecho);
202 noecho.c_lflag &= ~ECHO;
203 tcsetattr(fd, TCSANOW, &noecho);
204 #else /* HAVE_TERMIOS_H */
206 ioctl(fd, TCGETA, &withecho);
208 noecho.c_lflag &= ~ECHO;
209 ioctl(fd, TCSETA, &noecho);
210 #else /* HAVE_TERMIO_H */
211 /* neither HAVE_TERMIO_H nor HAVE_TERMIOS_H, we can't disable echo! */
212 (void)fd; /* prevent compiler warning on unused variable */
213 return FALSE; /* not disabled */
216 return TRUE; /* disabled */
219 /* re-enable echo, assumes we disabled it before (and set the structs we
220 now use to reset the terminal status) */
221 #ifdef HAVE_TERMIOS_H
222 tcsetattr(fd, TCSAFLUSH, &withecho);
223 #else /* HAVE_TERMIOS_H */
225 ioctl(fd, TCSETA, &withecho);
227 /* neither HAVE_TERMIO_H nor HAVE_TERMIOS_H */
228 return FALSE; /* not enabled */
231 return TRUE; /* enabled */
235 char *getpass_r(const char *prompt, /* prompt to display */
236 char *password, /* buffer to store password in */
237 size_t buflen) /* size of buffer to store password in */
241 int fd=open("/dev/tty", O_RDONLY);
243 fd = 1; /* use stdin if the tty couldn't be used */
245 disabled = ttyecho(FALSE, fd); /* disable terminal echo */
247 fputs(prompt, stderr);
248 nread=read(fd, password, buflen);
250 password[--nread]=0; /* zero terminate where enter is stored */
252 password[0]=0; /* got nothing */
255 /* if echo actually was disabled, add a newline */
257 (void)ttyecho(TRUE, fd); /* enable echo */
263 return password; /* return pointer to buffer */
267 #endif /* HAVE_GETPASS_R */