* C File Descriptors in Ada95
@ 1996-07-19 0:00 Jerome D. Seibert
1996-07-21 0:00 ` Nasser Abbasi
1996-07-22 0:00 ` j. doe
0 siblings, 2 replies; 3+ messages in thread
From: Jerome D. Seibert @ 1996-07-19 0:00 UTC (permalink / raw)
I have a need to make a call to a C function from an Ada-95 procedure, and
pass an open file descriptor to the C function.
I have tried extending Ada.Text_IO and passing File.Stream to my C routine
and then dereferencing the pointer in the C code (this did not work).
I then tried performing an unchecked conversion from System.Address to
Integer'Access to convert File.Stream to an Integer access, dereference the
access pointer and passing this to my C routine as a file descriptor (int)
(this did not work either).
Any info on how I might do this would be greatly appreciated.
A simplified sample of code follows (the File parameter must already be open
for write prior to making the call to Ada.Text_IO.sendfd.sendfd):
with Ada.Unchecked_Conversion;
with System;
with Interfaces.C;
package body Ada.Text_IO.sendfd is
package IC renames Interfaces.C;
-- this is my C routine
procedure sendfd (fd : IC.int);
pragma Import (C, sendfd, "sendfd");
-- define an integer access which will reference the file descriptor
type FD_Ptr is access Integer;
function UC_To_FD_Ptr is
new Ada.Unchecked_Conversion (System.Address, FD_Ptr);
procedure sendfd (File : in out File_Type) is
fd : Integer;
begin
-- convert File.Stream to a file descriptor
fd := UC_To_FD_Ptr (File.Stream).all;
-- now call the C routine
sendfd (IC.int (fd));
end sendfd;
end Ada.Text_IO.sendfd;
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: C File Descriptors in Ada95
1996-07-19 0:00 C File Descriptors in Ada95 Jerome D. Seibert
@ 1996-07-21 0:00 ` Nasser Abbasi
1996-07-22 0:00 ` j. doe
1 sibling, 0 replies; 3+ messages in thread
From: Nasser Abbasi @ 1996-07-21 0:00 UTC (permalink / raw)
To: Jerome D. Seibert
From: seibert@monkfish.nosc.mil (Jerome D. Seibert)
I have a need to make a call to a C function from an Ada-95 procedure, and
pass an open file descriptor to the C function.
I have tried extending Ada.Text_IO and passing File.Stream to my C routine
and then dereferencing the pointer in the C code (this did not work).
I then tried performing an unchecked conversion from System.Address to
Integer'Access to convert File.Stream to an Integer access, dereference the
access pointer and passing this to my C routine as a file descriptor (int)
(this did not work either).
Any info on how I might do this would be greatly appreciated.
...snip..
You might want'a look at at fattach() :
NAME
fattach - attach a STREAMS-based file descriptor to an
object in the file system name space
SYNOPSIS
int fattach(int fildes, const char *path);
MT-LEVEL
MT-Safe
DESCRIPTION
The fattach() routine attaches a STREAMS-based file descrip-
tor to an object in the file system name space, effectively
associating a name with fildes. fildes must be a valid open
file descriptor representing a STREAMS file. path is a path
name of an existing object and the user must have appropri-
ate privileges or be the owner of the file and have write
permissions. All subsequent operations on path will operate
on the STREAMS file until the STREAMS file is detached from
the node. fildes can be attached to more than one path,
that is, a stream can have several names associated with it.
bye,
Nasser
--
Nasser Abbasi. C/C++/Ada Solaris. GeneAssist - A client/server application
for Nucleic acid and protein sequence search and analysis.
Perkin Elmer - Applied BioSystem division. email: nasser@apldbio.com
MSEE(control), MSCS, MSCE, FM (Fide Chess Master).
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: C File Descriptors in Ada95
1996-07-19 0:00 C File Descriptors in Ada95 Jerome D. Seibert
1996-07-21 0:00 ` Nasser Abbasi
@ 1996-07-22 0:00 ` j. doe
1 sibling, 0 replies; 3+ messages in thread
From: j. doe @ 1996-07-22 0:00 UTC (permalink / raw)
seibert@monkfish.nosc.mil (Jerome D. Seibert) writes:
>
> I have a need to make a call to a C function from an Ada-95 procedure, and
> pass an open file descriptor to the C function.
>
> I have tried extending Ada.Text_IO and passing File.Stream to my C routine
> and then dereferencing the pointer in the C code (this did not work).
> I then tried performing an unchecked conversion from System.Address to
> Integer'Access to convert File.Stream to an Integer access, dereference the
> access pointer and passing this to my C routine as a file descriptor (int)
> (this did not work either).
>
> Any info on how I might do this would be greatly appreciated.
>
the problem is that the component of the Text_IO type File_Type that you are
attempting to make use of (File.Stream) is a pointer to a Stream, which is
effectively meant to be a C FILEs , not a C FD. to get the FD out of the
FILEs, you need to choose one of the following two solutions, depending on
your desire for more portable code or not having to maintain an extra C
function within your library.
[1] create an analogous record to iob[] found in <stdio.h>
(which may differ from machine to machine)
--ada.text_io.whatever_your_child_is_called
--from /usr/include/stdio.h on a sparc 5 under SunOS 4.1.3
type iobuf is record
Cnt : IC.int;
Ptr : unsigned_char_ptr;
Base : unsigned_char_ptr;
Size : IC.int;
Flag : IC.short;
File : IC.unsigned_char;
end record;
for iobuf use record
Cnt at 0 range 0 .. 31;
Ptr at 4 range 0 .. 31;
Base at 8 range 0 .. 31;
Size at 12 range 0 .. 31;
Flag at 16 range 0 .. 15;
File at 18 range 0 .. 7;
end record;
type IOB_Ptr is access iobuf;
function UC_To_IOB_Ptr is
new Ada.Unchecked_Conversion (System.Address, IOB_Ptr);
function C_Function_Using_FD
(FD : in IC.int;
Cmd : in IC.int;
Size : in IC.long) return Integer;
pragma Import (C, C_Function_Using_FD, "cFunctionUsingFD");
procedure Function_Using_File
(F : File_Type ) is -- a private defined in Ada.Text_IO
C_Stat : Integer;
begin
C_Stat := C_Function_Using_FD(Integer(UC_To_IOB_Ptr(F.Stream).File));
OR
[2] you need to create another .c file/C function to extract the FD from the
FILEs, and then pragma IMPORT that and use it (i.e. more portable from
machine to machine, but also incorporates another C function) :
#include <stdio.h>
int getFDfromFILEs(FILEs F)
{
return (int)(F->_file);
}
--in ada
function Get_FD_From_Stream_Ptr( F : System.Address ) return Integer;
pragma IMPORT( C, Get_Fd_From_Stream_Ptr, "getFDfromFILEs" );
--in ada.text_io.whatever_your_child_is_called
function C_Function_Using_FD( FD : Integer ) return Integer;
pragma IMPORT( C, C_Function_Using_FD, "cFunctionUsingFD" );
procedure Function_Using_File( F : File_Type );
Status : Integer;
begin
Status := C_Function_Using_FD( FD => Get_FD_From_Stream_Ptr( F.Stream ) );
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1996-07-22 0:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-19 0:00 C File Descriptors in Ada95 Jerome D. Seibert
1996-07-21 0:00 ` Nasser Abbasi
1996-07-22 0:00 ` j. doe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox