OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / unistd / getpass.c
1 /* Copyright (C) 1992,93,94,95,96,97,98,99 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 not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <termios.h>
22 #include <unistd.h>
23 #include <string.h>
24
25 libc_hidden_proto(strlen)
26 libc_hidden_proto(tcsetattr)
27 libc_hidden_proto(tcgetattr)
28 libc_hidden_proto(setvbuf)
29 libc_hidden_proto(fopen)
30 libc_hidden_proto(fclose)
31 libc_hidden_proto(fileno)
32 libc_hidden_proto(fflush)
33 libc_hidden_proto(fgets)
34 libc_hidden_proto(fputs)
35 libc_hidden_proto(fputc)
36 libc_hidden_proto(putc)
37 libc_hidden_proto(__fputc_unlocked)
38
39 /* It is desirable to use this bit on systems that have it.
40    The only bit of terminal state we want to twiddle is echoing, which is
41    done in software; there is no need to change the state of the terminal
42    hardware.  */
43
44 #ifndef TCSASOFT
45 #define TCSASOFT 0
46 #endif
47 #define PWD_BUFFER_SIZE 256
48
49 char *
50 getpass (prompt)
51      const char *prompt;
52 {
53   FILE *in, *out;
54   struct termios s, t;
55   int tty_changed;
56   static char buf[PWD_BUFFER_SIZE];
57   int nread;
58
59   /* Try to write to and read from the terminal if we can.
60      If we can't open the terminal, use stderr and stdin.  */
61
62   in = fopen ("/dev/tty", "r+");
63   if (in == NULL)
64     {
65       in = stdin;
66       out = stderr;
67     }
68   else
69     out = in;
70
71   /* Turn echoing off if it is on now.  */
72
73   if (tcgetattr (fileno (in), &t) == 0)
74     {
75       /* Save the old one. */
76       s = t;
77       /* Tricky, tricky. */
78       t.c_lflag &= ~(ECHO|ISIG);
79       tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
80       if (in != stdin) {
81         /* Disable buffering for read/write FILE to prevent problems with
82          * fseek and buffering for read/write auto-transitioning. */
83         setvbuf(in, NULL, _IONBF, 0);
84       }
85     }
86   else
87     tty_changed = 0;
88
89   /* Write the prompt.  */
90   fputs(prompt, out);
91   fflush(out);
92
93   /* Read the password.  */
94   fgets (buf, PWD_BUFFER_SIZE-1, in);
95   if (buf != NULL)
96     {
97       nread = strlen(buf);
98       if (nread < 0)
99         buf[0] = '\0';
100       else if (buf[nread - 1] == '\n')
101         {
102           /* Remove the newline.  */
103           buf[nread - 1] = '\0';
104           if (tty_changed)
105             /* Write the newline that was not echoed.  */
106             putc('\n', out);
107         }
108     }
109
110   /* Restore the original setting.  */
111   if (tty_changed) {
112     (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
113   }
114
115   if (in != stdin)
116     /* We opened the terminal; now close it.  */
117     fclose (in);
118
119   return buf;
120 }