1 // RUN: %clangxx -O0 -g %s -lutil -o %t && %run %t | FileCheck %s
2 
3 // REQUIRES: stable-runtime
4 // XFAIL: android && asan
5 
6 #include <assert.h>
7 #include <stdio.h>
8 #include <string.h>
9 #if __linux__
10 #include <pty.h>
11 #elif defined(__FreeBSD__)
12 #include <libutil.h>
13 #include <pwd.h>
14 #include <sys/ioctl.h>
15 #include <sys/termios.h>
16 #include <sys/types.h>
17 #else
18 #include <util.h>
19 #endif
20 #include <unistd.h>
21 
22 int
23 main (int argc, char** argv)
24 {
25     int master;
26     int pid = forkpty(&master, NULL, NULL, NULL);
27 
28     if(pid == -1) {
29       fprintf(stderr, "forkpty failed\n");
30       return 1;
31     } else if (pid > 0) {
32       char buf[1024];
33       int res = read(master, buf, sizeof(buf));
34       write(1, buf, res);
35       write(master, "password\n", 9);
36       while ((res = read(master, buf, sizeof(buf))) > 0) write(1, buf, res);
37     } else {
38       char *s = getpass("prompt");
39       assert(strcmp(s, "password") == 0);
40       write(1, "done\n", 5);
41     }
42     return 0;
43 }
44 
45 // CHECK: prompt
46 // CHECK: done
47