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// REQUIRES: stable-runtime
7// XFAIL: android && asan
8
9// No libutil.
10// UNSUPPORTED: target={{.*solaris.*}}
11
12#include <assert.h>
13#include <stdio.h>
14#include <string.h>
15#if __linux__
16# include <pty.h>
17#elif defined(__FreeBSD__)
18# include <libutil.h>
19# include <pwd.h>
20# include <sys/ioctl.h>
21# include <sys/termios.h>
22# include <sys/types.h>
23#elif defined(__sun__) && defined(__svr4__)
24# include <termios.h>
25#else
26# include <util.h>
27#endif
28#include <unistd.h>
29
30int main(int argc, char **argv) {
31 int m;
32 int pid = forkpty(amaster: &m, NULL, NULL, NULL);
33
34 if (pid == -1) {
35 fprintf(stderr, format: "forkpty failed\n");
36 return 1;
37 } else if (pid > 0) {
38 char buf[1024];
39 int res = read(fd: m, buf: buf, nbytes: sizeof(buf));
40 write(fd: 1, buf: buf, n: res);
41 write(fd: m, buf: "password\n", n: 9);
42 while ((res = read(fd: m, buf: buf, nbytes: sizeof(buf))) > 0)
43 write(fd: 1, buf: buf, n: res);
44 } else {
45 char *s = getpass(prompt: "prompt");
46 assert(strcmp(s, "password") == 0);
47 write(fd: 1, buf: "done\n", n: 5);
48 }
49 return 0;
50}
51
52// CHECK: prompt
53// CHECK: done
54

source code of compiler-rt/test/sanitizer_common/TestCases/Posix/getpass.cpp