Operating System - HP-UX
1748032 Members
4702 Online
108757 Solutions
New Discussion юеВ

Pascal code to use a C function

 
SOLVED
Go to solution
Stephen Badgett
Regular Advisor

Pascal code to use a C function

I am writting a pascal program that I would like to use use some of the "C" functions like "open(),read(),write() and close()" instead of the pascal "open,read,write ..."
I know that I can write a "C" stub and call it as a function but what I would like to do is make the call to the "C" "open()" directly from the pascal program instead of writting a stub like "openit.c"

Is there a way to do this?
Not as is, is now
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: Pascal code to use a C function

If you think about this for a moment you will realize that this is pretty much Looney Tunes. Let's take for example the 'read' function or more properly in Pascal, the read procedure. To Pascal this is a hi-level operation with a great deal of abstraction. You simply say "read x, y;" and voila x and y are read -- regardless of whether x and y are real, integer, boolean, ... because the compiler actually translates the high level "read x, y" into lower-level i/o operations appropriate to the data type. In C, open is a low-level system call and it is the sole responsibility of the programmer to format the data, pass a pointer to the data, number of bytes to read, and the file descriptor --- itself a much lower-level construct than Pascal's FILE.

If it ain't broke, I can fix that.
Mike Stroyan
Honored Contributor
Solution

Re: Pascal code to use a C function

There are HP-specific mechanisms for making calls from pascal to code written in other languages. You will probably need to use an alias from a different name to avoid conflicts
with pascal keywords. See
"HP Pascal/HP-UX Programmer's Guide" and
"HP Pascal/HP-UX Reference Manual" at
http://www.docs.hp.com./hpux/dev/index.html#Pascal
Stephen Badgett
Regular Advisor

Re: Pascal code to use a C function

Thank you A. Clay and Mike,

Look like I will have to write "c" wrappers for these calls.

I have attached the pascal file if you would like to look at what I am doing.

Steve
Not as is, is now
A. Clay Stephenson
Acclaimed Contributor

Re: Pascal code to use a C function

Well, you obviously didn't like my first responce when I basically told you that you were asking the Pascal compiler to distinguish between 'read' and 'read' so you probably won't like this answer either. Why not do this in C? You seem to running around the world to do very trivial things. Also, unless you specifically need the low-level i/o that read and write provide, you can use use fopen() (or popen) to create files which are generally easier to use than fdes i/o.

When I first came into the UNIX world I was very proficient in Pascal and found that more and more I was calling external C routines. I finally gave up and did everything in C. If you have a great deal of Pascal code, there is a Gnu utility that will translate your Pascal to reasonably good C.
If it ain't broke, I can fix that.
Stephen Badgett
Regular Advisor

Re: Pascal code to use a C function

It was not that I did not like your response I was asking what my boss was needing for this Pascal issue we confronted. I also did not have a lot of time to explore if there was a solution for this issue. I did know that the external c program works fine and this is currently my solution but in short I had to know if there is any way to do this without using c directly (extern). I like c and use it extensively in our coding requirements but not in our current application code I am fixing. This current application is quite huge and rewriting it in c will be the future of this application and since c does so well in what it does I know that is the direction we will end up. What Gnu utility you were talking about? So I apologize in the points giving, I was looking for a Pascal solution and I know c enough to write most everything in c and have used EXTERNAL enough think the same about rewriting the application in c if I had time but I was looking for something I guess was not there. So, I guess I have to apologize in all I do since I am taken wrong most of the time. The little knowledge I know about Pascal I find it quite weak in some areas. Do have a nice day, I am (:

Steve
Not as is, is now
A. Clay Stephenson
Acclaimed Contributor

Re: Pascal code to use a C function

Here's the Pascal to C translator.

http://hpux.connect.org.uk/hppd/hpux/Languages/p2c-1.20/
If it ain't broke, I can fix that.
Stephen Badgett
Regular Advisor

Re: Pascal code to use a C function

forget it! - I have found my own solution
Not as is, is now