Reputation: 2092
My apologies if this seems duplicate but I can't find the answer I'm looking for. What I'm trying to do is create a super simple function in my C program that calls an app that I also made with C, waits for it to finish, then receives the return value from the app.
This is my source code of the app:
int main(){
return 777;
}
I compiled it and renamed it to b.out and placed it in the root folder so the program can execute it. As you can see, all the app does is return 777 to the system.
This is the program that is calling the app:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <wait.h>
#include <unistd.h>
int rcode=0;
int run(char* dir,char* mainapp,char *par1,char *par2){
int f=fork();
if (f==-1){
printf("Fork call error\n");
return -1;
}
if (f==0){ //child
char cmdline[1000];
memset(cmdline,0,1000); //command line = directory + / + appname
strcat(cmdline,dir);
strcat(cmdline,"/");
strcat(cmdline,mainapp);
//par1,par2 = parameters to program
char *args[5]={cmdline,par1,par2,NULL};
execve(cmdline,args,NULL);
return 1; //this is child here
}else{
int waitstat;
waitpid(f,&waitstat,0); //let child finish
rcode=WEXITSTATUS(waitstat); //get return code from child
}
return 0;
}
int main(){
if (run("/","b.out","","")==1){
return 0; //exit if this is child
};
printf("Child returns %d\n",rcode); //only parent prints this
return rcode;
}
When I execute everything, the return value reported is 9 (rcode=9) but it should equal 777 because my program made it equal that.
Am I supposed to replace return 777 with exit(777) or is there a better code I can use to get the return value from the child that the child sets?
Upvotes: 1
Views: 73
Reputation: 58929
The exit code is 8 bits.
Truncating 777 to 8 bits gives the value 9.
Upvotes: 2