174989affSNico Weber // RUN: %clangxx -O0 -g %s -lutil -o %t && %run %t | FileCheck %s
274989affSNico Weber 
374989affSNico Weber // REQUIRES: stable-runtime
474989affSNico Weber // XFAIL: android && asan
503d593ddSRainer Orth // No libutil.
603d593ddSRainer Orth // UNSUPPORTED: solaris
774989affSNico Weber 
874989affSNico Weber #include <assert.h>
974989affSNico Weber #include <stdio.h>
1074989affSNico Weber #include <string.h>
1174989affSNico Weber #if __linux__
1274989affSNico Weber #include <pty.h>
1374989affSNico Weber #elif defined(__FreeBSD__)
1474989affSNico Weber #include <libutil.h>
1574989affSNico Weber #include <pwd.h>
1674989affSNico Weber #include <sys/ioctl.h>
1774989affSNico Weber #include <sys/termios.h>
1874989affSNico Weber #include <sys/types.h>
1903d593ddSRainer Orth #elif defined(__sun__) && defined(__svr4__)
2003d593ddSRainer Orth #include <termios.h>
2174989affSNico Weber #else
2274989affSNico Weber #include <util.h>
2374989affSNico Weber #endif
2474989affSNico Weber #include <unistd.h>
2574989affSNico Weber 
2674989affSNico Weber int
main(int argc,char ** argv)2774989affSNico Weber main (int argc, char** argv)
2874989affSNico Weber {
29*0b3d3122SVitaly Buka     int m;
30*0b3d3122SVitaly Buka     int pid = forkpty(&m, NULL, NULL, NULL);
3174989affSNico Weber 
3274989affSNico Weber     if(pid == -1) {
3374989affSNico Weber       fprintf(stderr, "forkpty failed\n");
3474989affSNico Weber       return 1;
3574989affSNico Weber     } else if (pid > 0) {
3674989affSNico Weber       char buf[1024];
37*0b3d3122SVitaly Buka       int res = read(m, buf, sizeof(buf));
3874989affSNico Weber       write(1, buf, res);
39*0b3d3122SVitaly Buka       write(m, "password\n", 9);
40*0b3d3122SVitaly Buka       while ((res = read(m, buf, sizeof(buf))) > 0) write(1, buf, res);
4174989affSNico Weber     } else {
4274989affSNico Weber       char *s = getpass("prompt");
4374989affSNico Weber       assert(strcmp(s, "password") == 0);
4474989affSNico Weber       write(1, "done\n", 5);
4574989affSNico Weber     }
4674989affSNico Weber     return 0;
4774989affSNico Weber }
4874989affSNico Weber 
4974989affSNico Weber // CHECK: prompt
5074989affSNico Weber // CHECK: done
51