[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Calling a "C" program from other language

Posted on 1999-11-15
11
Medium Priority
?
245 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:pen123
  • 3
  • 3
  • 3
  • +2
11 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 2210326
You could write to a file.  Or to stdout, or to a socket...
0
 
LVL 6

Expert Comment

by:graham_k
ID: 2210484
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
 
LVL 85

Expert Comment

by:ozo
ID: 2210516
I thought that's what pen123 said it was doing.
0
Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 
LVL 1

Accepted Solution

by:
AlexPiko earned 140 total points
ID: 2210612
[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
 
LVL 1

Expert Comment

by:AlexPiko
ID: 2210655
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
 
LVL 6

Expert Comment

by:graham_k
ID: 2210745
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
 
LVL 85

Expert Comment

by:ozo
ID: 2210803
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
 
LVL 1

Expert Comment

by:AlexPiko
ID: 2210884
hmm, think ozo answered right (1st comment + above comment:)
reject mine
regards
Alex Piko
0
 
LVL 6

Expert Comment

by:graham_k
ID: 2210943
well, you could always write to a file in /var/temp & let the caller repeatedly check for its existance, then read it.
0
 
LVL 1

Expert Comment

by:pagerbak
ID: 2212581
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
 
LVL 1

Expert Comment

by:mwimer
ID: 2273550
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

Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question