Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!dimacs.rutgers.edu!mips!wrdis01!news.cs.indiana.edu!sdd.hp.com!think.com!snorkelwacker.mit.edu!paperboy!meissner From: meissner@osf.org (Michael Meissner) Newsgroups: comp.unix.programmer Subject: Re: Can I get back stdout after redirection? Message-ID: Date: 3 May 91 20:08:57 GMT References: <48186@ut-emx.uucp> <1991May02.125231.20488@ghost.unimi.it> Sender: news@OSF.ORG Distribution: usa Organization: Open Software Foundation Lines: 49 In-reply-to: matteo@ghost.unimi.it's message of 2 May 91 12:52:31 GMT In article <1991May02.125231.20488@ghost.unimi.it> matteo@ghost.unimi.it (Matteo Gelosa) writes: | > Since the shell has already closed stdout's file descriptor, is it not | > possible to reopen it? (Also, how did stdout get opened in the first | > place? The shell inherited it from its parent, didn't it?) | | There is a special file named "/dev/tty", that always refers | to your current tty. Simply you have to reopen it if you want | to send your output back to your tty. | A lot of programs that have to communicate only with a tty | device do it to avoid redirection. | Try with this... However, /dev/tty is not the same thing as stdout (though for many users it winds up at the same location). If you need to restore your original stdout after redirection, the only way is not to close your last handle on it. I would recomend: orig_stdout = dup (fileno (stdout)); if (orig_stdout < 0) { perror ("dup"); abort (); } if (freopen ("newfile", "a", stdout) != stdout) { perror ("newfile"); abort (); } /* whatever */ fclose (stdout); if (fdopen (orig_stdout, "w") == 0) { perror ("fdopen"); abort (); } for the adventuresome, you can play games with dup behind stdio's back, but it's not something I recomend..... -- Michael Meissner email: meissner@osf.org phone: 617-621-8861 Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142 Considering the flames and intolerance, shouldn't USENET be spelled ABUSENET?