1 /* Copyright (C) 1992-1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, see <http://www.gnu.org/licenses/>. */
18 /* Modified to use with samba by Jeremy Allison, 8th July 1995. */
21 #include "system/filesys.h"
22 #include "system/wait.h"
23 #include "system/terminal.h"
24 #include "system/passwd.h"
27 * Define additional missing types
29 #ifndef HAVE_SIG_ATOMIC_T_TYPE
30 typedef int sig_atomic_t;
34 #define SIGCLD SIGCHLD
39 /* SYSTEM V TERMIO HANDLING */
41 static struct termio t;
43 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
44 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
45 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
55 static int tcgetattr(int fd, struct termio *_t)
57 return ioctl(fd, TCGETA, _t);
60 static int tcsetattr(int fd, int flags, struct termio *_t)
63 ioctl(fd, TCFLSH, TCIOFLUSH);
64 return ioctl(fd, TCSETS, _t);
67 #elif !defined(TCSAFLUSH)
69 /* BSD TERMIO HANDLING */
71 static struct sgttyb t;
73 #define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
74 #define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
75 #define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
80 static int tcgetattr(int fd, struct sgttyb *_t)
82 return ioctl(fd, TIOCGETP, (char *)_t);
85 static int tcsetattr(int fd, int flags, struct sgttyb *_t)
87 return ioctl(fd, TIOCSETP, (char *)_t);
90 #else /* POSIX TERMIO HANDLING */
91 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
92 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
93 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
95 static struct termios t;
96 #endif /* SYSV_TERMIO */
98 static void catch_signal(int signum, void (*handler)(int ))
100 #ifdef HAVE_SIGACTION
101 struct sigaction act;
102 struct sigaction oldact;
104 memset(&act, 0, sizeof(act));
106 act.sa_handler = handler;
109 * We *want* SIGALRM to interrupt a system call.
111 if(signum != SIGALRM)
112 act.sa_flags = SA_RESTART;
114 sigemptyset(&act.sa_mask);
115 sigaddset(&act.sa_mask,signum);
116 sigaction(signum,&act,&oldact);
117 #else /* !HAVE_SIGACTION */
118 /* FIXME: need to handle sigvec and systems with broken signal() */
119 signal(signum, handler);
123 static sig_atomic_t gotintr;
124 static int in_fd = -1;
126 /***************************************************************
127 Signal function to tell us were ^C'ed.
128 ****************************************************************/
130 static void gotintr_sig(int signum)
134 close(in_fd); /* Safe way to force a return. */
138 char *rep_getpass(const char *prompt)
142 static char buf[256];
143 static size_t bufsize = sizeof(buf);
146 /* Catch problematic signals */
147 catch_signal(SIGINT, gotintr_sig);
149 /* Try to write to and read from the terminal if we can.
150 If we can't open the terminal, use stderr and stdin. */
152 in = fopen ("/dev/tty", "w+");
160 setvbuf(in, NULL, _IONBF, 0);
162 /* Turn echoing off if it is on now. */
164 if (tcgetattr (fileno (in), &t) == 0) {
167 echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
176 /* Write the prompt. */
180 /* Read the password. */
184 if (fgets(buf, bufsize, in) == NULL) {
190 if (buf[nread - 1] == '\n')
191 buf[nread - 1] = '\0';
194 /* Restore echoing. */
196 if (gotintr && in_fd == -1) {
197 in = fopen ("/dev/tty", "w+");
200 tcsetattr (fileno (in), TCSANOW, &t);
206 if (in && in != stdin) /* We opened the terminal; now close it. */
209 /* Catch problematic signals */
210 catch_signal(SIGINT, SIG_DFL);
213 printf("Interrupted by signal.\n");