6fe06f4637d1ba1c36e31a207020ad851fc6e316
[platform/upstream/glibc.git] / crypt / examples / mygetpass.c
1 #include <termios.h>
2 #include <stdio.h>
3
4 ssize_t
5 my_getpass (char **lineptr, size_t *n, FILE *stream)
6 {
7   struct termios old, new;
8   int nread;
9
10   /* Turn echoing off and fail if we can't.  */
11   if (tcgetattr (fileno (stream), &old) != 0)
12     return -1;
13   new = old;
14   new.c_lflag &= ~ECHO;
15   if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
16     return -1;
17   
18   /* Read the password.  */
19   nread = getline (lineptr, n, stream);
20
21   /* Restore terminal.  */
22   (void) tcsetattr (fileno (stream), TCSAFLUSH, &old);
23   
24   return nread;
25 }