fix constraint violation in ftw
[musl] / src / legacy / getpass.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <termios.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6
7 char *getpass(const char *prompt)
8 {
9         int fd;
10         struct termios s, t;
11         ssize_t l;
12         static char password[128];
13
14         if ((fd = open("/dev/tty", O_RDONLY|O_NOCTTY)) < 0) fd = 0;
15
16         tcgetattr(fd, &t);
17         s = t;
18         t.c_lflag &= ~(ECHO|ISIG);
19         t.c_lflag |= ICANON;
20         t.c_iflag &= ~(INLCR|IGNCR);
21         t.c_iflag |= ICRNL;
22         tcsetattr(fd, TCSAFLUSH, &t);
23         tcdrain(fd);
24
25         fputs(prompt, stderr);
26         fflush(stderr);
27
28         l = read(fd, password, sizeof password);
29         if (l >= 0) {
30                 if (l > 0 && password[l-1] == '\n') l--;
31                 password[l] = 0;
32         }
33
34         tcsetattr(fd, TCSAFLUSH, &s);
35
36         if (fd > 2) close(fd);
37
38         return password;
39 }