[OT] C programming question: reopen stdin
Hi all. 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. This means I have to reopen stdin but I don't know how to do this. I tried to solve this using dup() and dup2() but without success. You can have a look at my first experiment here: http://gist.github.com/95320 I guess the code and the contained comments show what I'm trying to achieve. Although this is off-topic on this list I hope someone with more knowledge in C may steer me into the right direction. Sorry for the noise and thanks for reading. Regards Tobias -- Tobias Rehbein PGP key: 4F2AE314 server: keys.gnupg.net fingerprint: ECDA F300 1B6E 9B87 8524 8663 E8B6 3138 4F2A E314 ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: [OT] C programming question: reopen stdin
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. -- Mel ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: [OT] C programming question: reopen stdin
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 To avoid further spamming of the freebsd-questions mailing list: Could someone point me to a good place to ask C programming questions? Regards Tobias ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: [OT] C programming question: reopen stdin
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; } ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
[tobias.rehb...@web.de: Re: [OT] C programming question: reopen stdin]
s/Good/Could/ --- Begin Message --- 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 To avoid further spamming of the freebsd-questions mailing list: Could someone point me to a good place to ask C programming questions? Regards Tobias --- End Message --- ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: [tobias.rehb...@web.de: Re: [OT] C programming question: reopen stdin]
2009/4/16 Tobias Rehbein : > s/Good/Could/ > > > -- Forwarded message -- > From: Tobias Rehbein > To: Mel Flynn > Date: Thu, 16 Apr 2009 21:03:52 +0200 > Subject: Re: [OT] C programming question: reopen stdin > 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 > > To avoid further spamming of the freebsd-questions mailing list: Could someone > point me to a good place to ask C programming questions? > > Regards > > Tobias > > ___ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org" > http://groups.google.com/group/comp.lang.c The oldest and probably most populated. -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail? ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"