Find the answer to your Linux question:
Results 1 to 7 of 7
I understand I can use freopen to redirect stderror to a file; I can get my program working, however I do not want to be doing that. I am running ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Posts
    7

    redirect stderr

    I understand I can use freopen to redirect stderror to a file; I can get my program working, however I do not want to be doing that.

    I am running on Ubuntu. doing some googling I understand os.popen4 require me to install libs in my system. I cannot be doing that as this program eventually run in an embeded system.

    I found pstream (PStreams - POSIX Process Control in C++), but I am unable to use it according to their examples.

    string str;
    ipstream host("hostname");
    getline(host, str); <=== always error as getline expects (char**, size_t*, FILE*), changed the syntax and I am still getting error as host isn't FILE *

    did a quick Ubuntu and pstream search and I found I need to install the libs as well.

    I am using popen to run some command, and wish to capture the stderr without directing it to a file. Is there a way to cleanly and easily do it?
    eg:
    FILE *fp = popen(command, "r");

    while (fgets(buf, 511, fp))
    {
    ...
    }

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    if your just trying to send or redirect stderr why don't you try dup2(int old, int new)

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Posts
    7
    dup2 duplicate a file descriptor, how will that help in redirecting stderr?
    have an example or link to help? I am not getting the use at this time.

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    stderr file descriptor is 2....I hope this helps

    stdin = 0
    stdout = 1
    stderr = 2

  5. #5
    Just Joined!
    Join Date
    Sep 2008
    Posts
    7
    Thanx, I read up on this thread on dup but somehow I am not able to get it working. The stderr is not redirected. what could be the issue here.
    Yahoo! Groups


    FILE *fp = popen(command, "r");
    int d = fileno(fp);
    dup2(d,fileno(stdout));
    dup2(d,fileno(stderr);

    while (fgets(buf, 511, fp))
    {
    ....
    }

  6. #6
    Just Joined!
    Join Date
    Sep 2008
    Posts
    7
    I can get popen( "command1 2>&1", "r" ) to redirect stderr
    however if there is a string of commands
    eg:
    "command1;command2;command3" I will need to perform a insert of " 2>&1" according.

    Is there a easier to get dup2, freopen etc so that I don't have to do " 2>&1"?

  7. #7
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I never used popen before but if your using pipes and redirection this is a good example of redirection....just compile this program then run it:

    ./program ps wc

    and it will redirect the bash command ps into wc

    Code:
    #include <unistd.h>
    #include <stdlib.h>
    
    enum PIPES {READ, WRITE};
    
    int main(int argc, char**argv)
    {
    	if (argv[2])
    	{
    		int hpipe[2];
    		pipe(hpipe);
    
    		if (fork())
    		{
    			close(hpipe[WRITE]);
    			dup2(hpipe[READ], 0);//stdin = 0
    			close(hpipe[READ]);
    			execlp(argv[2],argv[2],NULL);
    		}
    		else
    		{
    			close(hpipe[READ]);
    			dup2(hpipe[WRITE], 1);//stdout = 1
    			close(hpipe[WRITE]);
    			execlp(argv[1],argv[1],NULL);
    		}
    	}
    	exit(EXIT_SUCCESS);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...