Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / hcrypto / ui.c
blob3314907dac67d995464da5237930b1432b7dac2d
1 /* $NetBSD: ui.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
3 /*
4 * Copyright (c) 1997 - 2000, 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include <config.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <signal.h>
42 #ifdef HAVE_TERMIOS_H
43 #include <termios.h>
44 #endif
45 #include <krb5/roken.h>
47 #include <ui.h>
48 #ifdef HAVE_CONIO_H
49 #include <conio.h>
50 #endif
52 static sig_atomic_t intr_flag;
54 static void
55 intr(int sig)
57 intr_flag++;
60 #ifdef HAVE_CONIO_H
63 * Windows does console slightly different then then unix case.
66 static int
67 read_string(const char *preprompt, const char *prompt,
68 char *buf, size_t len, int echo)
70 int of = 0;
71 int c;
72 char *p;
73 void (*oldsigintr)(int);
75 _cprintf("%s%s", preprompt, prompt);
77 oldsigintr = signal(SIGINT, intr);
79 p = buf;
80 while(intr_flag == 0){
81 c = ((echo)? _getche(): _getch());
82 if(c == '\n' || c == '\r')
83 break;
84 if(of == 0)
85 *p++ = c;
86 of = (p == buf + len);
88 if(of)
89 p--;
90 *p = 0;
92 if(echo == 0){
93 printf("\n");
96 signal(SIGINT, oldsigintr);
98 if(intr_flag)
99 return -2;
100 if(of)
101 return -1;
102 return 0;
105 #else /* !HAVE_CONIO_H */
107 #ifndef NSIG
108 #define NSIG 47
109 #endif
111 static int
112 read_string(const char *preprompt, const char *prompt,
113 char *buf, size_t len, int echo)
115 struct sigaction sigs[NSIG];
116 int oksigs[NSIG];
117 struct sigaction sa;
118 FILE *tty;
119 int ret = 0;
120 int of = 0;
121 int i;
122 int c;
123 char *p;
125 struct termios t_new, t_old;
127 memset(&oksigs, 0, sizeof(oksigs));
129 memset(&sa, 0, sizeof(sa));
130 sa.sa_handler = intr;
131 sigemptyset(&sa.sa_mask);
132 sa.sa_flags = 0;
133 for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
134 if (i != SIGALRM)
135 if (sigaction(i, &sa, &sigs[i]) == 0)
136 oksigs[i] = 1;
138 if((tty = fopen("/dev/tty", "r")) != NULL)
139 rk_cloexec_file(tty);
140 else
141 tty = stdin;
143 fprintf(stderr, "%s%s", preprompt, prompt);
144 fflush(stderr);
146 if(echo == 0){
147 tcgetattr(fileno(tty), &t_old);
148 memcpy(&t_new, &t_old, sizeof(t_new));
149 t_new.c_lflag &= ~ECHO;
150 tcsetattr(fileno(tty), TCSANOW, &t_new);
152 intr_flag = 0;
153 p = buf;
154 while(intr_flag == 0){
155 c = getc(tty);
156 if(c == EOF){
157 if(!ferror(tty))
158 ret = 1;
159 break;
161 if(c == '\n')
162 break;
163 if(of == 0)
164 *p++ = c;
165 of = (p == buf + len);
167 if(of)
168 p--;
169 *p = 0;
171 if(echo == 0){
172 fprintf(stderr, "\n");
173 tcsetattr(fileno(tty), TCSANOW, &t_old);
176 if(tty != stdin)
177 fclose(tty);
179 for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
180 if (oksigs[i])
181 sigaction(i, &sigs[i], NULL);
183 if(ret)
184 return -3;
185 if(intr_flag)
186 return -2;
187 if(of)
188 return -1;
189 return 0;
192 #endif /* HAVE_CONIO_H */
195 UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, int verify)
197 int ret;
199 ret = read_string("", prompt, buf, length, 0);
200 if (ret)
201 return ret;
203 if (verify) {
204 char *buf2;
205 buf2 = malloc(length);
206 if (buf2 == NULL)
207 return 1;
209 ret = read_string("Verify password - ", prompt, buf2, length, 0);
210 if (ret) {
211 free(buf2);
212 return ret;
214 if (strcmp(buf2, buf) != 0)
215 ret = 1;
216 free(buf2);
218 return ret;