Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / src / getpass.c
blobcf077412f566df3c07a012b7c7d80da2bc24406d
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
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
25 problems. */
27 #include "setup.h"
29 #ifndef HAVE_GETPASS_R
30 /* this file is only for systems without getpass_r() */
32 #include <stdio.h>
33 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
39 #include "getpass.h"
41 #ifdef HAVE_FCNTL_H
42 #include <fcntl.h>
43 #endif
44 #ifdef HAVE_TERMIOS_H
45 #include <termios.h>
46 #else
47 #ifdef HAVE_TERMIO_H
48 #include <termio.h>
49 #endif
50 #endif
52 /* The last #include file should be: */
53 #if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
54 #include "memdebug.h"
55 #endif
57 #ifdef VMS
58 /* VMS implementation */
59 #include descrip
60 #include starlet
61 #include iodef
62 /* #include iosbdef */
63 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
65 long sts;
66 short chan;
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; */
71 struct _iosb
73 short int iosb$w_status; /* status */
74 short int iosb$w_bcnt; /* byte count */
75 int unused; /* unused */
76 } iosb;
78 $DESCRIPTOR(ttdesc, "TT");
80 buffer[0]='\0';
81 sts = sys$assign(&ttdesc, &chan,0,0);
82 if (sts & 1) {
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 */
95 #define DONE
96 #endif /* VMS */
99 #ifdef WIN32
100 /* Windows implementation */
101 #include <conio.h>
102 #endif
104 #ifdef __SYMBIAN32__
105 #define getch() getchar()
106 #endif
108 #if defined(WIN32) || defined(__SYMBIAN32__)
110 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
112 size_t i;
113 fputs(prompt, stderr);
115 for(i=0; i<buflen; i++) {
116 buffer[i] = getch();
117 if ( buffer[i] == '\r' || buffer[i] == '\n' ) {
118 buffer[i] = 0;
119 break;
121 else
122 if ( buffer[i] == '\b')
123 /* remove this letter and if this is not the first key, remove the
124 previous one as well */
125 i = i - (i>=1?2:1);
127 #ifndef __SYMBIAN32__
128 /* since echo is disabled, print a newline */
129 fputs("\n", stderr);
130 #endif
131 /* if user didn't hit ENTER, terminate buffer */
132 if (i==buflen)
133 buffer[buflen-1]=0;
135 return buffer; /* we always return success */
137 #define DONE
138 #endif /* WIN32 || __SYMBIAN32__ */
140 #ifdef NETWARE
141 /* NetWare implementation */
142 #ifdef __NOVELL_LIBC__
143 #include <screen.h>
144 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
146 return getpassword(prompt, buffer, buflen);
148 #else
149 #include <nwconio.h>
150 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
152 size_t i = 0;
154 printf("%s", prompt);
155 do {
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 */
160 if (i > 1) {
161 printf("\b \b");
162 i = i - 2;
163 } else {
164 RingTheBell();
165 i = i - 1;
167 } else if (buffer[i-1] != 13) {
168 putchar('*');
170 } while ((buffer[i-1] != 13) && (i < buflen));
171 buffer[i-1] = 0;
172 printf("\r\n");
173 return buffer;
175 #endif /* __NOVELL_LIBC__ */
176 #define DONE
177 #endif /* NETWARE */
179 #ifndef DONE /* not previously provided */
181 #ifdef HAVE_TERMIOS_H
182 #define struct_term struct termios
183 #else
184 #ifdef HAVE_TERMIO_H
185 #define struct_term struct termio
186 #else
187 #undef struct_term
188 #endif
189 #endif
191 static bool ttyecho(bool enable, int fd)
193 #ifdef struct_term
194 static struct_term withecho;
195 static struct_term noecho;
196 #endif
197 if(!enable) {
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);
202 noecho = withecho;
203 noecho.c_lflag &= ~ECHO;
204 tcsetattr(fd, TCSANOW, &noecho);
205 #else /* HAVE_TERMIOS_H */
206 #ifdef HAVE_TERMIO_H
207 ioctl(fd, TCGETA, &withecho);
208 noecho = 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 */
215 #endif
216 #endif
217 return TRUE; /* disabled */
219 else {
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 */
225 #ifdef HAVE_TERMIO_H
226 ioctl(fd, TCSETA, &withecho);
227 #else
228 /* neither HAVE_TERMIO_H nor HAVE_TERMIOS_H */
229 return FALSE; /* not enabled */
230 #endif
231 #endif
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 */
240 ssize_t nread;
241 bool disabled;
242 int fd=open("/dev/tty", O_RDONLY);
243 if(-1 == fd)
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);
250 if(nread > 0)
251 password[--nread]=0; /* zero terminate where enter is stored */
252 else
253 password[0]=0; /* got nothing */
255 if(disabled) {
256 /* if echo actually was disabled, add a newline */
257 fputs("\n", stderr);
258 (void)ttyecho(TRUE, fd); /* enable echo */
261 if(1 != fd)
262 close(fd);
264 return password; /* return pointer to buffer */
267 #endif /* DONE */
268 #endif /* HAVE_GETPASS_R */