最简单的方式来获得在Linux下C一PTY ++ [英] Simplest way to get a PTY in Linux C++

查看:1239
本文介绍了最简单的方式来获得在Linux下C一PTY ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有。
我编程的东西,需要猛砸的接口。起初,我以为我可以只使用的popen或QProcess中。 (我使用QT C ++)它们工作得很好,但我不能让他们在一个tty,你需要,如果你打算使用任何类似sudo的,这需要一个tty / PTY接受的密码。运行猛砸

all. 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.

我发现有些事情就像forkpty(),openpty()等在GNU标准C库,但无法弄清楚如何使用它们或发现任何很好的例子,甚至阅读其相应的联机帮助页之后。从字面上看,我的程序的所有这部分需要做的是能够读取一个tty可以运行/ bin / bash中/写。难道真的那么简单,还是有更多的它比满足眼睛?

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! Here's the header file I've come up with!

推荐答案

考虑选择运行 / bin / sh的-s ,意思是从标准输入读取命令。这消除了需要模拟一个终端。

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

这篇关于最简单的方式来获得在Linux下C一PTY ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆