如何在不关闭管道的情况下通过管道发送文件结束? [英] How to send end-of-file through a pipe without closing the pipe?

查看:92
本文介绍了如何在不关闭管道的情况下通过管道发送文件结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开运行bash的终端并输入以下内容时:

When I open a terminal running bash and type the following:

cat\nfoo\n^Decho bar\n

(输入\ n且^ D为control-d)我得到:

(where \n is enter and ^D is control-d) I get:

foo
bar

即,^ D导致cat退出,但我仍然可以输入更多内容.
我该如何通过C中的(unix)管道发送相同的输入(特别是文件结尾)?

Ie, ^D causes cat to exit, but I can still type more.
How would I send the same input (specifically the end-of-file) through a (unix) pipe in C?

推荐答案

xterm不会通过pipe(7)与shell和实用程序一起执行其输入.而是使用Unix PTY框架(有关某些信息,请参见pty(7)openpty(3)forkpty(3)posix_openpt(3)pts(4)联机帮助页). PTY框架允许任何进程充当终端主机"(例如,telnetd(8)sshd(8)xterm(1)等),并且任何进程都可以连接到终端从机以提供交互式环境,就像坐在控制台.

xterm does not perform its input with the shell and utilities via pipe(7)s. Instead, it uses the Unix PTY framework (see pty(7), openpty(3), forkpty(3), posix_openpt(3), pts(4) manpages for some information). The PTY framework allows any process to serve as a terminal "master" (e.g., telnetd(8), sshd(8), xterm(1), etc.) and any process can connect to the terminal slave to provide an interactive environment just like sitting at the console.

Unix环境中的高级编程,第二版 rads.stackoverflow.com/amzn/click/com/0201433079"rel =" noreferrer"rel =" nofollow noreferrer>史蒂文斯和拉格一个优秀的小型pty程序,它允许以类似于expect(1)的方式来驱动交互式"程序,但是使用C而不是tcl.

The Advanced Programming in the Unix Environment, 2nd edition book by Stevens and Rago has an excellent chapter on using pseudo-terminal devices to control slave programs -- including an excellent little pty program that allows driving "interactive" programs in a manner similar to expect(1), but in C rather than tcl.

这篇关于如何在不关闭管道的情况下通过管道发送文件结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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