Simplest way to get a PTY in Linux C++

2020-04-12 05:50发布

问题:

I am programming something that needs an interface to Bash. At first I thought I could just use popen or QProcess. ( I'm using QT C++ ) They work fine but I can't get them to run Bash in a tty, which you need if you are going to use anything like sudo, which requires a tty/pty to accept a password.

I found some things like forkpty(), openpty(), etc. in the GNU Standard C libraries, but could not figure out how to use them or find any good examples, even after reading their corresponding manpages. Literally, all this part of my program needs to do is be able to read/write from a tty running /bin/bash. Is it really that simple, or is there more to it than meets the eye?

If so, can anyone please provide a concise example on how to do this? Thanks in advance! EDIT: Here's the header file I've come up with!

回答1:

Consider the option of running /bin/sh -s, which means "read commands from standard input". That removes the need to emulate a terminal.



标签: c++ linux bash qt pty