doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
getpass.c
Go to the documentation of this file.
1 /* ============================================================================
2  *
3  * Redistribution and use are freely permitted provided that:
4  *
5  * 1) This header remain in tact.
6  * 2) The prototypes for getpass and getpass_r are not changed from:
7  * char *getpass(const char *prompt)
8  * char *getpass_r(const char *prompt, char* buffer, int buflen)
9  * 3) This source code is not used outside of this(getpass.c) file.
10  * 4) Any changes to this(getpass.c) source code are made publicly available.
11  *
12  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
13  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
14  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15  * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
16  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  * POSSIBILITY OF SUCH DAMAGE.
22  * ============================================================================
23  *
24  * $Id: getpass.c,v 1.6 2004/03/10 16:03:12 bagder Exp $
25  *
26  * The spirit of this license is to allow use of this source code in any
27  * project be it open or closed but still encourage the use of the open,
28  * library based equivilents.
29  *
30  * Author(s):
31  * Angus Mackay <amackay@gus.ml.org>
32  * Daniel Stenberg <daniel@haxx.se>
33  */
34 
35 #include "setup.h" /* setup.h is required for read() prototype */
36 
37 #ifndef HAVE_GETPASS_R
38 
39 #include "getpass.h"
40 
41 #ifndef WIN32
42 #ifdef VMS
43 #include <stdio.h>
44 #include <string.h>
45 #include descrip
46 #include starlet
47 #include iodef
48 #include iosbdef
49 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
50 {
51  long sts;
52  short chan;
53  struct _iosb iosb;
54  $DESCRIPTOR(ttdesc, "TT");
55 
56  buffer[0]='\0';
57  sts = sys$assign(&ttdesc, &chan,0,0);
58  if (sts & 1) {
59  sts = sys$qiow(0, chan,
60  IO$_READPROMPT | IO$M_NOECHO,
61  &iosb, 0, 0, buffer, buflen, 0, 0,
62  prompt, strlen(prompt));
63 
64  if((sts & 1) && (iosb.iosb$w_status&1))
65  buffer[iosb.iosb$w_bcnt] = '\0';
66 
67  sts = sys$dassgn(chan);
68  }
69  return buffer; /* we always return success */
70 }
71 #else /* VMS */
72 #ifdef HAVE_TERMIOS_H
73 # if !defined(HAVE_TCGETATTR) && !defined(HAVE_TCSETATTR)
74 # undef HAVE_TERMIOS_H
75 # endif
76 #endif
77 
78 #ifndef RETSIGTYPE
79 # define RETSIGTYPE void
80 #endif
81 
82 #ifdef HAVE_UNISTD_H
83 #include <unistd.h>
84 #endif
85 #include <stdio.h>
86 #include <signal.h>
87 #ifdef HAVE_TERMIOS_H
88 # include <termios.h>
89 #else
90 # ifdef HAVE_TERMIO_H
91 # include <termio.h>
92 # else
93 # endif
94 #endif
95 
96 /* The last #include file should be: */
97 #ifdef CURLDEBUG
98 #include "../lib/memdebug.h"
99 #endif
100 
101 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
102 {
103  FILE *infp;
104  char infp_fclose = 0;
105  FILE *outfp;
106  RETSIGTYPE (*sigint)(int);
107 #ifdef SIGTSTP
108  RETSIGTYPE (*sigtstp)(int);
109 #endif
110  size_t bytes_read;
111  int infd;
112  int outfd;
113 #ifdef HAVE_TERMIOS_H
114  struct termios orig;
115  struct termios noecho;
116 #else
117 # ifdef HAVE_TERMIO_H
118  struct termio orig;
119  struct termio noecho;
120 # else
121 # endif
122 #endif
123 
124  sigint = signal(SIGINT, SIG_IGN);
125 #ifdef SIGTSTP
126  sigtstp = signal(SIGTSTP, SIG_IGN);
127 #endif
128 
129  infp=fopen("/dev/tty", "r");
130  if( NULL == infp )
131  infp = stdin;
132  else
133  infp_fclose = 1;
134 
135  outfp = stderr;
136 
137  infd = fileno(infp);
138  outfd = fileno(outfp);
139 
140  /* dissable echo */
141 #ifdef HAVE_TERMIOS_H
142  tcgetattr(outfd, &orig);
143 
144  noecho = orig;
145  noecho.c_lflag &= ~ECHO;
146  tcsetattr(outfd, TCSANOW, &noecho);
147 #else
148 # ifdef HAVE_TERMIO_H
149  ioctl(outfd, TCGETA, &orig);
150  noecho = orig;
151  noecho.c_lflag &= ~ECHO;
152  ioctl(outfd, TCSETA, &noecho);
153 # else
154 # endif
155 #endif
156 
157  fputs(prompt, outfp);
158  fflush(outfp);
159 
160  bytes_read=read(infd, buffer, buflen);
161  buffer[bytes_read > 0 ? (bytes_read -1) : 0] = '\0';
162 
163  /* print a new line if needed */
164 #ifdef HAVE_TERMIOS_H
165  fputs("\n", outfp);
166 #else
167 # ifdef HAVE_TERMIO_H
168  fputs("\n", outfp);
169 # else
170 # endif
171 #endif
172 
173  /*
174  * reset term charectaristics, use TCSAFLUSH incase the
175  * user types more than buflen
176  */
177 #ifdef HAVE_TERMIOS_H
178  tcsetattr(outfd, TCSAFLUSH, &orig);
179 #else
180 # ifdef HAVE_TERMIO_H
181  ioctl(outfd, TCSETA, &orig);
182 # else
183 # endif
184 #endif
185 
186  signal(SIGINT, sigint);
187 #ifdef SIGTSTP
188  signal(SIGTSTP, sigtstp);
189 #endif
190 
191  if(infp_fclose)
192  fclose(infp);
193 
194  return buffer; /* we always return success */
195 }
196 #endif /* VMS */
197 #else /* WIN32 */
198 #include <stdio.h>
199 #include <conio.h>
200 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
201 {
202  size_t i;
203  printf("%s", prompt);
204 
205  for(i=0; i<buflen; i++) {
206  buffer[i] = getch();
207  if ( buffer[i] == '\r' ) {
208  buffer[i] = 0;
209  break;
210  }
211  else
212  if ( buffer[i] == '\b')
213  /* remove this letter and if this is not the first key, remove the
214  previous one as well */
215  i = i - (i>=1?2:1);
216  }
217  /* if user didn't hit ENTER, terminate buffer */
218  if (i==buflen)
219  buffer[buflen-1]=0;
220 
221  return buffer; /* we always return success */
222 }
223 #endif
224 
225 #endif /* ifndef HAVE_GETPASS_R */
226 
227 #if 0
228 /* for consistensy, here's the old-style function: */
229 char *getpass(const char *prompt)
230 {
231  static char buf[256];
232  return getpass_r(prompt, buf, sizeof(buf));
233 }
234 #endif
case const int
Definition: Callbacks.cpp:52
int fileno(FILE *stream)
int i
Definition: process.py:33
#define ioctl(a, b, c, d)
Definition: amigaos.h:41
idSys * sys
Definition: maya_main.cpp:48
#define NULL
Definition: Lib.h:88
char * getpass_r(const char *prompt, char *buffer, size_t buflen)
Definition: getpass.c:101
GLuint buffer
Definition: glext.h:3108
#define RETSIGTYPE
Definition: getpass.c:79