From owner-freebsd-questions@FreeBSD.ORG Thu Apr 16 20:22:14 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E3EB010657A3 for ; Thu, 16 Apr 2009 20:22:14 +0000 (UTC) (envelope-from mel.flynn+fbsd.questions@mailing.thruhere.net) Received: from mailhub.rachie.is-a-geek.net (rachie.is-a-geek.net [66.230.99.27]) by mx1.freebsd.org (Postfix) with ESMTP id 3CF948FC19 for ; Thu, 16 Apr 2009 20:22:14 +0000 (UTC) (envelope-from mel.flynn+fbsd.questions@mailing.thruhere.net) Received: from sarevok.dnr.servegame.org (gate.lan.rachie.is-a-geek.net [192.168.2.10]) by mailhub.rachie.is-a-geek.net (Postfix) with ESMTP id 5836C7E821; Thu, 16 Apr 2009 12:22:12 -0800 (AKDT) From: Mel Flynn To: freebsd-questions@freebsd.org Date: Thu, 16 Apr 2009 22:22:06 +0200 User-Agent: KMail/1.11.0 (FreeBSD/8.0-CURRENT; KDE/4.2.0; i386; ; ) References: <20090414181300.GA3813@sushi.pseudo.local> <200904142107.26678.mel.flynn+fbsd.questions@mailing.thruhere.net> <20090416190352.GA5366@sushi.pseudo.local> In-Reply-To: <20090416190352.GA5366@sushi.pseudo.local> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200904162222.06680.mel.flynn+fbsd.questions@mailing.thruhere.net> Cc: Tobias Rehbein Subject: Re: [OT] C programming question: reopen stdin X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Apr 2009 20:22:15 -0000 On Thursday 16 April 2009 21:03:52 Tobias Rehbein wrote: > Am Tue, Apr 14, 2009 at 09:07:26PM +0200 schrieb Mel Flynn: > > On Tuesday 14 April 2009 20:13:00 Tobias Rehbein wrote: > > > I'm having a little trouble solving a specific problem in C. I want to > > > write a filter which reads data from stdin. After reading the user > > > should be able to interact with the program via stdin. > > > > Just open(2) /dev/tty. If tty is invalid, then you don't have to expect a > > user either. > > Thanks for this hint. I tried to implement an example. Good someone take a > look at it and tell me if I did it right. Well, at least it works... > > The code is here: > > http://gist.github.com/95320 It is really much simpler, see below for code: % cat tty.c | ./tty 1: #include 1: #include Hello! 2: Hello! quit (Don't focus on the 80char linebuf, I just know for this example I don't need more). /dev/tty is the "controlling terminal input". Stdin is the standard input, which is either the receiving end of a pipe/redirection or the controlling terminal in it's absence. > To avoid further spamming of the freebsd-questions mailing list: Could > someone point me to a good place to ask C programming questions? O'Reilly has a few good titles (Practical C programming is a good primer, followed by Algorithms with C), other then that, comp.lang.c newsgroup. -- Mel #include #include int main(int argc, char **argv) { FILE *tty; char linebuf[80]; while( fgets(linebuf, sizeof(linebuf), stdin) ) printf("1: %s", linebuf); if( (tty = fopen("/dev/tty", "r")) == NULL ) errx(0, "Running non-interactively. See ya!"); while( fgets(linebuf, sizeof(linebuf), tty) ) { if( strcmp(linebuf, "quit\n") == 0 ) break; printf("2: %s", linebuf); } return 0; }