LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-20-2008, 08:05 AM   #1
eddchr
LQ Newbie
 
Registered: Oct 2008
Posts: 9

Rep: Reputation: 0
Unhappy Propagate exit value, xterm


Hi,

We have a little scenario regarding how exit/return values gets propagated when executing a command via, for example, xterm.

Example program:
Code:
int main(){ return 123;}
What we need is to receive the return value above (123) when running the following command:
Code:
xterm -e ./example_program
The actual value we receive is the return value from xterm, which in the most cases are zero, and not 123..

Any ideas?
 
Old 10-20-2008, 08:51 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
As w workaround you could save the exit code of your program to a temp file:
Code:
bash:/tmp$ cat eksit.c
int main()
{
    return 67;
}
bash:/tmp$ gcc -o eksit eksit.c
bash:/tmp$ xterm -e '/tmp/eksit ; echo $? >/tmp/exitcode'
bash:/tmp$ cat /tmp/exitcode 
67
 
Old 10-20-2008, 03:34 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
This might be a start, if you really need it to be inlined.
Code:
//outer.c


#include <unistd.h>
#include <sys/types.h>


int main(int argc, char *argv[])
{
    int pipes[2] = { -1, -1 };
    pipe(pipes);

    pid_t new_process = fork();


    if (new_process < 0) return 1;


    if (new_process == 0)
    {
    dup2(pipes[1], 3);

    if (pipes[0] != 3) close(pipes[0]);
    if (pipes[1] != 3) close(pipes[1]);

    execvp(argv[1], argv + 1);
    }


    else
    {
    close(pipes[1]);

    waitpid(new_process, NULL, 0x00);

    int outcome = 0;
    read(pipes[0], &outcome, sizeof(outcome));

    return outcome;
    }


    return 1;
}
Code:
//inner.c


#include <unistd.h>
#include <sys/wait.h>


int main(int argc, char *argv[])
{
    int outcome = WEXITSTATUS( system(argv[1]) );
    write(3, &outcome, sizeof(outcome));
    return 0;
}
Code:
bash> gcc outer.c -o outer
bash> gcc inner.c -o inner
bash> ./outer xterm -e "./inner 'ls nothere'" || echo "'nothere' isn't here"
'nothere' isn't here
bash> ./outer xterm -e "./inner 'ls outer'" && echo "'outer' is here"
'outer' is here
It doesn't have the normal error checks, but it's a general idea of how you might do it.
ta0kira
 
  


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
xterm - howto lauch an xterm into a specific directory jobano Linux - Software 11 01-30-2023 04:45 AM
Warning: at kernel/exit.c:814 do exit() (Tainted: P ) <- nVidia video problem? Hitboxx Fedora 2 10-06-2007 09:47 PM
LXer: Sun Microsystems - doing all it can to propagate its immense software wealth LXer Syndicated Linux News 0 12-11-2006 12:21 PM
xterm alt becomes compose? Only xterm affected. -0- Linux - Software 0 09-12-2006 11:59 PM
how is black xterm background in IceWM when issue a xterm command? BRAHmS Linux - Software 1 02-14-2005 03:51 AM

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

All times are GMT -5. The time now is 03:53 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