LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2010, 08:49 PM   #1
ModMaker
LQ Newbie
 
Registered: Nov 2010
Location: Washington, USA
Distribution: Ubuntu, Backtrack
Posts: 8

Rep: Reputation: 0
Where stdout leads


I am writing a program in C and I need to determine if stdout has been redirected. I know that I can write to /dev/tty to write to the screen. I want to print to the screen always but if stdout is redirected, to print a log to that file.

So is there a way to determine if stdout has been redirected?
 
Old 12-28-2010, 10:47 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by ModMaker View Post
I want to print to the screen always but if stdout is redirected, to print a log to that file.
Perhaps a more useful way too look at it: you want to print to stdout always, but if stdout is redirected, print also to /dev/tty.
Quote:
Originally Posted by ModMaker View Post
So is there a way to determine if stdout has been redirected?
Sure. A quick and dirty way is to use isatty(STDOUT_FILENO).
 
1 members found this post helpful.
Old 12-29-2010, 02:14 AM   #3
ModMaker
LQ Newbie
 
Registered: Nov 2010
Location: Washington, USA
Distribution: Ubuntu, Backtrack
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks, it worked.
 
Old 12-29-2010, 03:57 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by ModMaker View Post
Thanks, it worked.
You're welcome. But I'll bet you're not welcoming the task of all those duplicate print statements. Try something like what's in this shell script. (Run it, you'll like it.)
Code:
rm -f 1 1.x;cat > 1.c <<EOD;gcc -Wall -Werror 1.c -o 1;./1 aaa;./1 bbb >> 1.x
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>      /* for exit() */
#include <unistd.h>      /* for STDOUT_FILENO */

FILE *dev_tty;

void printboth(char *the_format,
               ...
              )
{
  va_list argument_list;

  va_start(argument_list,the_format);
  vfprintf(stdout,the_format,argument_list);
  va_end(argument_list);

  if(isatty(STDOUT_FILENO)==0)
  {
    va_start(argument_list,the_format);
    vfprintf(dev_tty,the_format,argument_list);
    va_end(argument_list);
  }

} /* printboth() */

int main(int    argc,
         char **argv
        )
{
  dev_tty=fopen("/dev/tty","w");

  if(dev_tty==NULL)
  {
    fprintf(stderr,"could not open /dev/tty\n");

    exit(1);
  }

  printboth("running with argument %s\n",
            argv[1]
           );

  return 0;

} /* main() */
EOD
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
Editor looking for Linux leads carohuntah7 Linux - Server 3 05-11-2008 11:50 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
Firefox leads... Linuxapprentice General 3 02-02-2006 01:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration