~ubuntu-branches/ubuntu/trusty/musl/trusty-proposed

« back to all changes in this revision

Viewing changes to src/legacy/getpass.c

  • Committer: Package Import Robot
  • Author(s): Kevin Bortis
  • Date: 2013-09-20 20:54:14 UTC
  • Revision ID: package-import@ubuntu.com-20130920205414-5b61trtmma18w58o
Tags: upstream-0.9.13
ImportĀ upstreamĀ versionĀ 0.9.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}