[comp.lang.pascal] Sun Pascal: Pascal calls C

jasonf@cetemp.Eng.Sun.COM (Jason Freund) (08/28/90)

	I am calling a C routines from a Pascal program to (among other things)
change directories.  I am using several other C routines to do other system 
calls and only the ones that get arguments (such as cstr) passed to them fail.
What is wrong with:

------------------------system_cd.c-------------------

#include <stdio.h>

system_cd (cstr)

	char *cstr;

	{
	char cmnd[50];
	sprintf(cmnd,"cd %s",cstr);
	printf(cmnd);
	system(cmnd);

	}
---------------------------------------------------------

it's declared in the pascal part like:

procedure system_cd (cstr: string); external c;

and called like:  {s is a string}

system_cd(substr(s, 4, length(trim(s))));

Simple calls like "system_pwd() { system("pwd") }" work perfectly.

follow up to : jasonf@cetemp.Corp.sun.com

Thanks,
Jason Freund, Sun Microsystems,  jasonf@cetemp.Corp.sun.com  <== summer address
Deprtmnt of Computer Science, Univ California, Davis. freund@sakura.ucdavis.edu
Quantum Link: JasonF5,  Compu$erve: 72007,244, 690 Erie Dr, Sunnyvale, CA 94087
-------------------------------------------------------------------------------
STOLEN QUOTES -- Please give the authors credit if you know who they are!    
"To understand recursion, you need to understand recursion."
"Wow!  Virtual memory!  Now I'm gonna build me a REALLY big ram disk!"
"My other computer is a SUN3/50."  "E. Pluribus UNIX"   -- authors unkown 

jourdan@minos.inria.fr (Martin Jourdan) (08/28/90)

What do you mean by "the routines fail"?  Is it that they don't get
compiled correctly?  Or they don't receive correct arguments from
Pascal?  I used several C routines from Pascal programs and never had
any problem.

BUT... using ``system("cd xxx")'' is bound to fail (i.e., return without
any observable effect) when called either from C or from Pascal.  The
reason is that "system" spawns a shell to execute the command.  The
shell will change its **own** directory but **not the one of its parent
process** (the Pascal program).

You'd better re-read your Unix manual...

Martin Jourdan <jourdan@minos.inria.fr>, INRIA, Rocquencourt, France.
Why do we need all these %$#@%$# disclaimers?!?

jasonf@cetemp.Eng.Sun.COM (Jason Freund) (08/28/90)

	I changed the code to use "chdir()", compiled it with
"cc -c system_cd.c", compiled it with the pascal main, and it runs without
crashing at all.  But it still doesn't work.  It never actually changes
directories.  Here's the code:
----------------------------system_cd.c-------------------------------
include <stdio.h>

system_cd (cstr)

	char *cstr;

	{
	chdir(cstr);
	}
- - - - - - - - - - in calling pascal procedure:- - - - - - - - - - - - - 
procedure system_cd(cstr: string); external c;
	{is declared as procedure within procedure that calls it}
:
system_cd(substr(t1,4,length(trim(t1))));
	{trim: cuts off trailing spaces, t1 is string, ie "cd test",
	substr(t1,4,length(trim(t1))) would be "test"}

============================================================================

	Also, why doesn't my date function work?  I want the system command to 
get the date and chuck it back into the pascal calling program so that it can
be encoded:
-------------------------------system_date.c------------------------------
#include <stdio.h>

system_date (str)

	char *str;

	{
	sprintf(str, (system("/usr/bin/date")));
	}
- - - - - - - - - - in calling pascal procedure:- - - - - - - - - - - - -
procedure system_date(var s: string); external c;
:
:
system_date(s);
writeln(s);

...gets: 
*** Program execution terminated by segmentation violation 
???() 
???() 
In system_date () ... after I compile and run it.
----------------------------------------------------------------------------


Any ideas for either case?  I'm using Sun Pascal 2.0, on a 3/50 OS 4.1 machine.

Jason Freund