1
/* ============================================================================
2
* Copyright (C) 1998 - 2002, Daniel Stenberg, <daniel@haxx.se>, et al.
4
* Redistribution and use are freely permitted provided that:
6
* 1) This header remain in tact.
7
* 2) The prototypes for getpass and getpass_r are not changed from:
8
* char *getpass(const char *prompt)
9
* char *getpass_r(const char *prompt, char* buffer, int buflen)
10
* 3) This source code is not used outside of this(getpass.c) file.
11
* 4) Any changes to this(getpass.c) source code are made publicly available.
13
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
14
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
16
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
17
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
* POSSIBILITY OF SUCH DAMAGE.
23
* ============================================================================
25
* $Id: getpass.c,v 1.5 2003/05/06 15:26:42 andy Exp $
27
* The spirit of this license is to allow use of this source code in any
28
* project be it open or closed but still encourage the use of the open,
29
* library based equivilents.
32
* Angus Mackay <amackay@gus.ml.org>
35
* Daniel Stenberg <daniel@haxx.se>
38
#include "setup.h" /* setup.h is required for read() prototype */
40
#ifndef HAVE_GETPASS_R
50
char *getpass_r(const char *prompt, char *buffer, size_t buflen)
55
/* VMS does not work because of warnings on icc */
56
/* $DESCRIPTOR(ttdesc, "TT");
59
if ((sts = sys$assign(&ttdesc, &chan,0,0)) & 1) {
60
if (((sts = sys$qiow(0, chan, IO$_READPROMPT | IO$M_NOECHO, &iosb, 0, 0, buffer, buflen, 0, 0, prompt, strlen(prompt))) & 1) && (iosb.iosb$w_status&1)) {
61
buffer[iosb.iosb$w_bcnt] = '\0';
63
sts = sys$dassgn(chan);
66
return buffer; /* we always return success */
70
# if !defined(HAVE_TCGETATTR) && !defined(HAVE_TCSETATTR)
71
# undef HAVE_TERMIOS_H
76
# define RETSIGTYPE void
93
/* The last #include file should be: */
98
char *getpass_r(const char *prompt, char *buffer, size_t buflen)
101
char infp_fclose = 0;
103
RETSIGTYPE (*sigint)();
105
RETSIGTYPE (*sigtstp)();
110
#ifdef HAVE_TERMIOS_H
112
struct termios noecho;
114
# ifdef HAVE_TERMIO_H
116
struct termio noecho;
121
sigint = signal(SIGINT, SIG_IGN);
123
* this is needed by the emx system, SIGTSTP is not a supported signal */
125
sigtstp = signal(SIGTSTP, SIG_IGN);
128
infp=fopen("/dev/tty", "r");
137
outfd = fileno(outfp);
140
#ifdef HAVE_TERMIOS_H
141
tcgetattr(outfd, &orig);
144
noecho.c_lflag &= ~ECHO;
145
tcsetattr(outfd, TCSANOW, &noecho);
147
# ifdef HAVE_TERMIO_H
148
ioctl(outfd, TCGETA, &orig);
150
noecho.c_lflag &= ~ECHO;
151
ioctl(outfd, TCSETA, &noecho);
156
fputs(prompt, outfp);
159
bytes_read=read(infd, buffer, buflen);
160
buffer[bytes_read > 0 ? (bytes_read -1) : 0] = '\0';
162
/* print a new line if needed */
163
#ifdef HAVE_TERMIOS_H
166
# ifdef HAVE_TERMIO_H
173
* reset term charectaristics, use TCSAFLUSH incase the
174
* user types more than buflen
176
#ifdef HAVE_TERMIOS_H
177
tcsetattr(outfd, TCSAFLUSH, &orig);
179
# ifdef HAVE_TERMIO_H
180
ioctl(outfd, TCSETA, &orig);
185
signal(SIGINT, sigint);
187
signal(SIGTSTP, sigtstp);
193
return buffer; /* we always return success */
199
char *getpass_r(const char *prompt, char *buffer, int buflen)
202
printf("%s", prompt);
204
for(i=0; i<buflen; i++) {
205
buffer[i] = (char)getch();
206
if ( buffer[i] == '\r' ) {
211
/* if user didn't hit ENTER, terminate buffer */
215
return buffer; /* we always return success */
219
#endif /* ifndef HAVE_GETPASS_R */
222
/* for consistensy, here's the old-style function: */
223
char *getpass(const char *prompt)
225
static char buf[256];
226
return getpass_r(prompt, buf, sizeof(buf));
232
* eval: (load-file "../curl-mode.el")
235
* vim: et sw=2 ts=2 sts=2 tw=78