r5906: Fix the usage of the internal popt (make proto should ignore it)
[Samba/gebeck_regimport.git] / source4 / lib / getsmbpass.c
blob4ffcde8dfd31c03170510d62ae1f67019a432be1
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. */
21 #include "includes.h"
22 #include "system/terminal.h"
23 #include "system/wait.h"
25 #ifdef REPLACE_GETPASS
27 #ifdef SYSV_TERMIO
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)
37 #ifndef TCSAFLUSH
38 #define TCSAFLUSH 1
39 #endif
41 #ifndef TCSANOW
42 #define TCSANOW 0
43 #endif
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)
52 if(flags & TCSAFLUSH)
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)
67 #define TCSAFLUSH 1
68 #define TCSANOW 0
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)
90 FILE *in, *out;
91 int echo_off;
92 static char buf[256];
93 static size_t bufsize = sizeof(buf);
94 size_t nread;
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+");
103 if (in == NULL)
105 in = stdin;
106 out = stderr;
108 else
109 out = in;
111 setvbuf(in, NULL, _IONBF, 0);
113 /* Turn echoing off if it is on now. */
115 if (tcgetattr (fileno (in), &t) == 0)
117 if (ECHO_IS_ON(t))
119 TURN_ECHO_OFF(t);
120 echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
121 TURN_ECHO_ON(t);
123 else
124 echo_off = 0;
126 else
127 echo_off = 0;
129 /* Write the prompt. */
130 fputs (prompt, out);
131 fflush (out);
133 /* Read the password. */
134 buf[0] = 0;
135 fgets(buf, bufsize, in);
136 nread = strlen(buf);
137 if (buf[nread - 1] == '\n')
138 buf[nread - 1] = '\0';
140 /* Restore echoing. */
141 if (echo_off)
142 (void) tcsetattr (fileno (in), TCSANOW, &t);
144 if (in != stdin)
145 /* We opened the terminal; now close it. */
146 fclose (in);
148 /* Catch problematic signals */
149 CatchSignal(SIGINT, SIGNAL_CAST SIG_DFL);
151 printf("\n");
152 return buf;
155 #else
156 void getsmbpasswd_dummy(void);
157 void getsmbpasswd_dummy(void) {;}
158 #endif