Les*_*zek 14 c linux shell controls
假设我们在终端上运行了一个shell,比方说/ dev/pts/1.shell已经运行,我们无法重新启动它.
现在我们要编写一个"控制"shell的C程序,即它本身会为用户提供类似shell的接口,读取用户的输入,将其传递给/ dev/pts/1上的真实shell,它执行它,读取shell的输出并将其打印回用户.
我知道如何完成这项任务的一半:我知道如何收集用户的输入并将此输入注入"真实shell":
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#define SIZE 100
int main(int argc, char** argv)
{
if( argc>1 )
{
int tty = open( argv[1], O_WRONLY|O_NONBLOCK);
if( tty!=-1 )
{
char *buf,buffer[SIZE+1];
while(1)
{
printf("> ");
fgets( buffer, SIZE, stdin );
if( buffer[0]=='q' && buffer[1]=='u' && buffer[2]=='i' && buffer[3]=='t' ) break;
for(buf=buffer; *buf!='\0'; buf++ ) ioctl(tty, TIOCSTI, buf);
}
close(tty);
}
else printf("Failed to open terminal %s\n", argv[1]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面将把你的输入传递给在终端中运行的shell(在第一个参数中给出它的名字)并让shell执行它.但是,我现在不知道如何读取shell的输出.
有小费吗?
| 归档时间: |
|
| 查看次数: |
1685 次 |
| 最近记录: |