• 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
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.

You could write to a file.  Or to stdout, or to a socket...
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?
I thought that's what pen123 said it was doing.
Powerful Yet Easy-to-Use Network Monitoring

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

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.

Below is a example without fork

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

int x_sys(char * c_crc)            
        int retstat;                                
        if(retstat >= 0)                            
        return retstat;                                  

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
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"...

Alex Piko
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.
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"
hmm, think ozo answered right (1st comment + above comment:)
reject mine
Alex Piko
well, you could always write to a file in /var/temp & let the caller repeatedly check for its existance, then read it.
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
* Cobol special register contains the
* returned value from function myprog.
* myprog must return an int (possible a long)

or do you want to do

Where CMD-SPEC is defined (as far as I remember) as
   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.
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
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
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

From novice to tech pro — start learning today.