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 Library General Public License as
6 published by the Free Software Foundation; either version 2 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 Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 /* Modified to use with samba by Jeremy Allison, 8th July 1995. */
22 #include "system/terminal.h"
23 #include "system/wait.h"
25 #ifdef REPLACE_GETPASS
29 /* SYSTEM V TERMIO HANDLING */
31 static struct termio t
;
33 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
34 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
35 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
45 static int tcgetattr(int fd
, struct termio
*_t
)
47 return ioctl(fd
, TCGETA
, _t
);
50 static int tcsetattr(int fd
, int flags
, struct termio
*_t
)
53 ioctl(fd
, TCFLSH
, TCIOFLUSH
);
54 return ioctl(fd
, TCSETS
, _t
);
57 #elif !defined(TCSAFLUSH)
59 /* BSD TERMIO HANDLING */
61 static struct sgttyb t
;
63 #define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
64 #define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
65 #define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
70 static int tcgetattr(int fd
, struct sgttyb
*_t
)
72 return ioctl(fd
, TIOCGETP
, (char *)_t
);
75 static int tcsetattr(int fd
, int flags
, struct sgttyb
*_t
)
77 return ioctl(fd
, TIOCSETP
, (char *)_t
);
80 #else /* POSIX TERMIO HANDLING */
81 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
82 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
83 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
85 static struct termios t
;
86 #endif /* SYSV_TERMIO */
88 char *getsmbpass(const char *prompt
)
93 static size_t bufsize
= sizeof(buf
);
96 /* Catch problematic signals */
97 CatchSignal(SIGINT
, SIGNAL_CAST SIG_IGN
);
99 /* Try to write to and read from the terminal if we can.
100 If we can't open the terminal, use stderr and stdin. */
102 in
= fopen ("/dev/tty", "w+");
111 setvbuf(in
, NULL
, _IONBF
, 0);
113 /* Turn echoing off if it is on now. */
115 if (tcgetattr (fileno (in
), &t
) == 0)
120 echo_off
= tcsetattr (fileno (in
), TCSAFLUSH
, &t
) == 0;
129 /* Write the prompt. */
133 /* Read the password. */
135 fgets(buf
, bufsize
, in
);
137 if (buf
[nread
- 1] == '\n')
138 buf
[nread
- 1] = '\0';
140 /* Restore echoing. */
142 (void) tcsetattr (fileno (in
), TCSANOW
, &t
);
145 /* We opened the terminal; now close it. */
148 /* Catch problematic signals */
149 CatchSignal(SIGINT
, SIGNAL_CAST SIG_DFL
);
156 void getsmbpasswd_dummy(void);
157 void getsmbpasswd_dummy(void) {;}