ensure that 'available = no' works for [homes]; reported by Walter Haidinger
[Samba.git] / source / lib / getsmbpass.c
blobf0c24710e0077a734f314e30cb0e6629a4e49941
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"
23 #ifdef REPLACE_GETPASS
25 #ifdef SYSV_TERMIO
27 /* SYSTEM V TERMIO HANDLING */
29 static struct termio t;
31 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
32 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
33 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
35 #ifndef TCSAFLUSH
36 #define TCSAFLUSH 1
37 #endif
39 #ifndef TCSANOW
40 #define TCSANOW 0
41 #endif
43 static int tcgetattr(int fd, struct termio *t)
45 return ioctl(fd, TCGETA, t);
48 static int tcsetattr(int fd, int flags, struct termio *t)
50 if(flags & TCSAFLUSH)
51 ioctl(fd, TCFLSH, TCIOFLUSH);
52 return ioctl(fd, TCSETS, t);
55 #elif !defined(TCSAFLUSH)
57 /* BSD TERMIO HANDLING */
59 static struct sgttyb t;
61 #define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
62 #define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
63 #define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
65 #define TCSAFLUSH 1
66 #define TCSANOW 0
68 static int tcgetattr(int fd, struct sgttyb *t)
70 return ioctl(fd, TIOCGETP, (char *)t);
73 static int tcsetattr(int fd, int flags, struct sgttyb *t)
75 return ioctl(fd, TIOCSETP, (char *)t);
78 #else /* POSIX TERMIO HANDLING */
79 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
80 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
81 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
83 static struct termios t;
84 #endif /* SYSV_TERMIO */
86 char *getsmbpass(const char *prompt)
88 FILE *in, *out;
89 int echo_off;
90 static char buf[256];
91 static size_t bufsize = sizeof(buf);
92 size_t nread;
94 /* Catch problematic signals */
95 CatchSignal(SIGINT, SIGNAL_CAST SIG_IGN);
97 /* Try to write to and read from the terminal if we can.
98 If we can't open the terminal, use stderr and stdin. */
100 in = fopen ("/dev/tty", "w+");
101 if (in == NULL)
103 in = stdin;
104 out = stderr;
106 else
107 out = in;
109 setvbuf(in, NULL, _IONBF, 0);
111 /* Turn echoing off if it is on now. */
113 if (tcgetattr (fileno (in), &t) == 0)
115 if (ECHO_IS_ON(t))
117 TURN_ECHO_OFF(t);
118 echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
119 TURN_ECHO_ON(t);
121 else
122 echo_off = 0;
124 else
125 echo_off = 0;
127 /* Write the prompt. */
128 fputs (prompt, out);
129 fflush (out);
131 /* Read the password. */
132 buf[0] = 0;
133 fgets(buf, bufsize, in);
134 nread = strlen(buf);
135 if (buf[nread - 1] == '\n')
136 buf[nread - 1] = '\0';
138 /* Restore echoing. */
139 if (echo_off)
140 (void) tcsetattr (fileno (in), TCSANOW, &t);
142 if (in != stdin)
143 /* We opened the terminal; now close it. */
144 fclose (in);
146 /* Catch problematic signals */
147 CatchSignal(SIGINT, SIGNAL_CAST SIG_DFL);
149 printf("\n");
150 return buf;
153 #else
154 void getsmbpasswd_dummy(void);
155 void getsmbpasswd_dummy(void) {;}
156 #endif