• C

Calling a "C" program from other language

In a unix environment, calling a c program from other languages like "cobol" or "fortran" should return a value back to the calling program. Is it possible to set a shell variable in the c program and access it once the program is finished... or Is by any means the c program can set a value which can be accessed by the calling program
pen123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
You could write to a file.  Or to stdout, or to a socket...
0
graham_kCommented:
sure, but why do you want to set a shell variable? Why not just use the exit() function in you c program & have the caller check it, just like in Unix?
0
ozoCommented:
I thought that's what pen123 said it was doing.
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

AlexPikoCommented:
[UNIX]
(async)
fork a child, call your programm with exec....
You will receive a signal SIGCHILD on the child's exit,
Get the return code with the MACRO WEXITSTATUS.

(sync)
Below is a example without fork

#include <stdio.h>                                  
#include <stdlib.h>                                
#include <sys/wait.h>                              

int x_sys(char * c_crc)            
{                                                  
        int retstat;                                
        retstat=system(c_cmd);                      
        if(retstat >= 0)                            
               return=WEXITSTATUS(retstat);    
        return retstat;                                  
}                                                  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlexPikoCommented:
Maybe i havnt understand your Q exactly...

you can use popen() to create a process. popen returns a filehandle to read/write between the caller and the "child"...

regards
Alex Piko
0
graham_kCommented:
hi, ozo, I think that I still don't understand the question.

are we talking about calling one program for another & waiting for the 'child' to terminate - not spawning where both run independently ?

If so, the system() command, while not Ansi compliant, exists on unix, dos & windoze. It's the need for a system variable that phazes me.
0
ozoCommented:
reading it again, maybe system/exit (which are ANSI standard, although how the host environment executes the string is implementation-defined) were what pen123 was asking about.
But then, the question asks about what the called c program can do, and doesn't say how the calling fortran or cobol program is invoking it...
The literal answer might be "No, shell environment variables of the parent are in a different process space, you can only pass environment variables to a child"
0
AlexPikoCommented:
hmm, think ozo answered right (1st comment + above comment:)
reject mine
regards
Alex Piko
0
graham_kCommented:
well, you could always write to a file in /var/temp & let the caller repeatedly check for its existance, then read it.
0
pagerbakCommented:
What do you mean, when you say call. Do you want to call a c function, as you would call a cobol subprogram? or do you want to execute a program, that could be run from the command line?

I.e. do you want to do
 CALL 'MYPROG' USING ...
* Cobol special register contains the
* returned value from function myprog.
* myprog must return an int (possible a long)
 IF RETURN-CODE = 0 THEN
  ...
 ELSE
  ...
 END-IF

or do you want to do
 CALL 'SYSTEM' USING CMD-SPEC

Where CMD-SPEC is defined (as far as I remember) as
 01 CMD-SPEC.
   05  filler pic x(6) value 'MYPROG'.
* CMD-SPEC must act like a c-string, so
* it must be terminated by a
   05  filler pic x value low-value.

I have done both with Micro Focus Cobol.
0
mwimerCommented:
1. In unix you return a "value back" from system()
but it is only a integer value.
2. There is no way for a parent process to
lock at any evenironment variables set in the
child.  It can't be done, never.
3. The parent can set an environment variable
that the child can see but only when using
fork and exec*(man exec).  Because the environment of the parent is passed on to the
child.  
4. you can set the environment with setenv()
and can get the environment with getenv().
This uses char*s.

That asnwers your questions. But doesn't solve
your problem; that is solves by  AlexPiko's popen
comment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.