2 * promptusr.c --- prompt user for input/output
17 typedef struct _krb5_uio
{
22 struct _krb5_uio
*next
;
25 #define KRB5_UIO_GETRESPONSE 0x0001
26 #define KRB5_UIO_ECHORESPONSE 0x0002
27 #define KRB5_UIO_FREE_PROMPT 0x0004
29 static jmp_buf pwd_jump
;
33 intr_routine(int signo
)
41 krb5_os_get_tty_uio(krb5_context context
, krb5_uio uio
)
43 volatile krb5_error_code retval
;
44 krb5_sigtype (*volatile ointrfunc
)();
46 struct termios echo_control
, save_control
;
48 char read_string
[BUFSIZ
];
52 /* get the file descriptor associated with stdin */
55 if (tcgetattr(fd
, &echo_control
) == -1)
58 save_control
= echo_control
;
59 echo_control
.c_lflag
&= ~(ECHO
|ECHONL
);
61 if (setjmp(pwd_jump
)) {
62 retval
= KRB5_LIBOS_PWDINTR
; /* we were interrupted... */
66 ointrfunc
= signal(SIGINT
, intr_routine
);
68 for (p
= uio
; p
; p
= p
->next
) {
70 fputs(p
->prompt
, stdout
);
73 if ((p
->flags
& KRB5_UIO_GETRESPONSE
) == 0)
76 if ((p
->flags
& KRB5_UIO_ECHORESPONSE
) == 0)
77 if (tcsetattr(fd
, TCSANOW
, &echo_control
) == -1)
80 if (fgets(read_string
, sizeof(read_string
), stdin
) == NULL
) {
82 retval
= KRB5_LIBOS_CANTREADPWD
;
86 /* replace newline with null */
87 if ((cp
= strchr(read_string
, '\n')))
89 else /* flush rest of input line */
92 } while (ch
!= EOF
&& ch
!= '\n');
93 read_string
[sizeof(read_string
)-1] = 0;
95 if ((p
->response
= malloc(strlen(read_string
)+1)) == NULL
) {
99 strcpy(p
->response
, read_string
);
101 if ((p
->flags
& KRB5_UIO_ECHORESPONSE
) == 0) {
102 (void) putchar('\n');
103 if (tcsetattr(fd
, TCSANOW
, &save_control
) == -1) {
112 (void) signal(SIGINT
, ointrfunc
);
114 for (p
= uio
; p
; p
= p
->next
) {
116 memset(p
->response
, 0, strlen(p
->response
));
122 memset(read_string
, 0, sizeof(read_string
));
123 tcsetattr(fd
, TCSANOW
, &save_control
);
129 krb5_free_uio(krb5_context context
, krb5_uio uio
)
133 for (p
= uio
; p
; p
= next
) {
135 if (p
->prompt
&& (p
->flags
& KRB5_UIO_FREE_PROMPT
))
144 struct _krb5_uio uio_a
= { 0, KRB5_UIO_GETRESPONSE
, "Password 1: " };
145 struct _krb5_uio uio_b
= { 0, KRB5_UIO_GETRESPONSE
|
146 KRB5_UIO_ECHORESPONSE
, "Password 2: " };
147 struct _krb5_uio uio_c
= { 0, KRB5_UIO_GETRESPONSE
, "Password 3: " };
151 main(int argc
, char **argv
)
156 krb5_os_get_tty_uio(0, &uio_a
);