r5906: Fix the usage of the internal popt (make proto should ignore it)
[abartlet/samba.git/.git] / source4 / lib / cmdline / getsmbpass.c
1 /* Copyright (C) 1992-1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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.  */
18
19 /* Modified to use with samba by Jeremy Allison, 8th July 1995. */
20
21 #include "includes.h"
22 #include "system/terminal.h"
23 #include "system/wait.h"
24
25 #ifdef REPLACE_GETPASS
26
27 #ifdef SYSV_TERMIO 
28
29 /* SYSTEM V TERMIO HANDLING */
30
31 static struct termio t;
32
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)
36
37 #ifndef TCSAFLUSH
38 #define TCSAFLUSH 1
39 #endif
40
41 #ifndef TCSANOW
42 #define TCSANOW 0
43 #endif
44
45 static int tcgetattr(int fd, struct termio *_t)
46 {
47         return ioctl(fd, TCGETA, _t);
48 }
49
50 static int tcsetattr(int fd, int flags, struct termio *_t)
51 {
52         if(flags & TCSAFLUSH)
53                 ioctl(fd, TCFLSH, TCIOFLUSH);
54         return ioctl(fd, TCSETS, _t);
55 }
56
57 #elif !defined(TCSAFLUSH)
58
59 /* BSD TERMIO HANDLING */
60
61 static struct sgttyb t;  
62
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)
66
67 #define TCSAFLUSH 1
68 #define TCSANOW 0
69
70 static int tcgetattr(int fd, struct sgttyb *_t)
71 {
72         return ioctl(fd, TIOCGETP, (char *)_t);
73 }
74
75 static int tcsetattr(int fd, int flags, struct sgttyb *_t)
76 {
77         return ioctl(fd, TIOCSETP, (char *)_t);
78 }
79
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)
84
85 static struct termios t;
86 #endif /* SYSV_TERMIO */
87
88 char *getsmbpass(const char *prompt)
89 {
90   FILE *in, *out;
91   int echo_off;
92   static char buf[256];
93   static size_t bufsize = sizeof(buf);
94   size_t nread;
95
96   /* Catch problematic signals */
97   CatchSignal(SIGINT, SIGNAL_CAST SIG_IGN);
98
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.  */
101
102   in = fopen ("/dev/tty", "w+");
103   if (in == NULL)
104     {
105       in = stdin;
106       out = stderr;
107     }
108   else
109     out = in;
110
111   setvbuf(in, NULL, _IONBF, 0);
112
113   /* Turn echoing off if it is on now.  */
114
115   if (tcgetattr (fileno (in), &t) == 0)
116     {
117           if (ECHO_IS_ON(t))
118         {
119                 TURN_ECHO_OFF(t);
120                 echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
121                 TURN_ECHO_ON(t);
122         }
123       else
124         echo_off = 0;
125     }
126   else
127     echo_off = 0;
128
129   /* Write the prompt.  */
130   fputs (prompt, out);
131   fflush (out);
132
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';
139
140   /* Restore echoing.  */
141   if (echo_off)
142     (void) tcsetattr (fileno (in), TCSANOW, &t);
143
144   if (in != stdin)
145     /* We opened the terminal; now close it.  */
146     fclose (in);
147
148   /* Catch problematic signals */
149   CatchSignal(SIGINT, SIGNAL_CAST SIG_DFL);
150
151   printf("\n");
152   return buf;
153 }
154
155 #else
156  void getsmbpasswd_dummy(void);
157  void getsmbpasswd_dummy(void) {;}
158 #endif