LinuxQuestions.org
Visit Jeremy's Blog.
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 01-24-2008, 09:31 PM   #1
tmc3011
LQ Newbie
 
Registered: Jan 2008
Posts: 1

Rep: Reputation: 0
How to get stderr output when using a ::system() call


Hi,

I am trying to write a C++ application that wraps around the linux mount command.
I need to call
::system("mount /dev/sdc1 /mnt/test");
unfortunately this just returns 0 for success and nothing usefull on failure, all of the error messages are sent to stderr and I can't figure out how to redirect them.

none of the following worked
::system("mount /dev/sdc1 /mnt/test >& out.txt");
::system("mount /dev/sdc1 /mnt/test 2>&1 out.txt");
::system("mount /dev/sdc1 /mnt/test 2>&1 > out.txt");
::system("mount /dev/sdc1 /mnt/test | 2>&1 tee -a out.txt");
or
freopen("myfile.txt","w",stderr);
::system("mount /dev/sdc1 /mnt/test");
fclose();

Any suggestions?

I also did try using the mount(2) function, but after spending some time looking at the mount and nfsmount implementation in util-linux I realized that it was not going to be trivial and decided to use the mount(8) command. If anyone knows a way to implement using the nfs_mount_data funtion that would help also.

Thanks
Tamara
 
Old 01-24-2008, 09:58 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
i know you can change file descriptors to point to others, which i think would be what your looking for. hopefully youve done some pipe programming and other UNIX system programming, if so you should be able to figure it out without too much effort.

check man dup2. what this system call does is change one file descriptor (for example stderr) to point to another one. so what you can do is create your own file descriptor (or pipe i think) and set the output of stderr to go to your new fd (or pipe, i forget sorry!). once you have access to the file descriptor integer value, you can read it just like a normal file.

hope thats what your looking for
 
Old 01-25-2008, 04:17 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
system("mount /dev/sdc1 /mnt/test > out.txt 2>&1");
order is important...

command 2>&1 1> outfile

means:send 2 to 1 (tty) then send 1 to outfile (2 still points at tty)

command 1> outfile 2>&1

means: send 1 to outfile then send 2 to 1 (outfile)


geddit?

Last edited by bigearsbilly; 01-25-2008 at 04:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 can i make centos 4.5's system call using vDSO(call *%gs:0x10) instead of int80 tclwp Red Hat 3 08-06-2007 12:07 AM
Calling a system call from a system call? Loc_8 Programming 0 09-16-2006 04:21 PM
perl: send output of system() call to variable drkstr Programming 4 07-23-2006 10:47 PM
Using stderr output for decision making MeelaPo Programming 6 10-20-2004 08:21 PM
captumring output from STDERR rajatgarg Programming 2 01-30-2004 10:35 AM

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

All times are GMT -5. The time now is 05:40 PM.

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