Debian Codegraph @

compiler-rt/test/sanitizer_common/TestCases/Posix/getpass.cpp

raw
1 // RUN: %clangxx -O0 -g %s -lutil -o %t
2
3 // Ignore leaks as this is not the point of test, but HWASAN repors one here.
4 // RUN: %env_tool_opts=detect_leaks=0 %run %t | FileCheck %s
5
6 // XFAIL: android && asan
7
8 // No libutil.
9 // UNSUPPORTED: target={{.*solaris.*}}
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <string.h>
14 #if __linux__
15 # include <pty.h>
16 #elif defined(__FreeBSD__)
17 # include <libutil.h>
18 # include <pwd.h>
19 # include <sys/ioctl.h>
20 # include <sys/termios.h>
21 # include <sys/types.h>
22 #elif defined(__sun__) && defined(__svr4__)
23 # include <termios.h>
24 #else
25 # include <util.h>
26 #endif
27 #include <unistd.h>
28
29 int main(int argc, char **argv) {
30 int m;
31 int pid = forkpty(&m, NULL, NULL, NULL);
32
33 if (pid == -1) {
34 fprintf(stderr, "forkpty failed\n");
35 return 1;
36 } else if (pid > 0) {
37 char buf[1024];
38 int res = read(m, buf, sizeof(buf));
39 write(1, buf, res);
40 write(m, "password\n", 9);
41 while ((res = read(m, buf, sizeof(buf))) > 0)
42 write(1, buf, res);
43 } else {
44 char *s = getpass("prompt");
45 assert(strcmp(s, "password") == 0);
46 write(1, "done\n", 5);
47 }
48 return 0;
49 }
50
51 // CHECK: prompt
52 // CHECK: done