C Client Program Passing File to ALGOL Library

The following is an ALGOL library called FOO/FILE/A:

BEGIN
   INTEGER PROCEDURE STATIONNAME (F, MALLOC, COPYTOPTR);
      FILE F;
      INTEGER PROCEDURE MALLOC (SIZE);
         VALUE SIZE;
         INTEGER SIZE;
         FORMAL;
      PROCEDURE COPYTOPTR (LEN, BUF, OFF, PTR);
         VALUE LEN, OFF, PTR;
         INTEGER LEN, OFF, PTR; EBCDIC ARRAY BUF [*];
         FORMAL;
   BEGIN
      EBCDIC ARRAY T [0:300];
      POINTER P;
      INTEGER PTR, LEN;
      REPLACE P:T BY F(1).STATIONNAME;
      IF F.ATTERR THEN BEGIN
         REPLACE P:T BY “<attribute error>.”;
      END IF;
      REPLACE P-1 BY 48”00”;
      LEN:= OFFSET (P);
      PTR:= MALLOC (LEN);
      COPYTOPTR (LEN, T, 0, PTR);
      STATIONNAME:= PTR;
   END;
   EXPORT STATIONNAME;
   FREEZE (TEMPORARY);
END.

FOO/FILE/A exports a single procedure called STATIONNAME. The STATIONNAME procedure accepts three parameters: a remote file called F, the procedure MALLOC, and the procedure COPYTOPTR. The STATIONNAME procedure reads the STATIONNAME attribute of the remote file into array T. The STATIONNAME procedure uses the MALLOC function to allocate space in the C client program heap. The STATIONNAME procedure then uses the COPYTOPTR function to copy the contents of array T into the C client program heap. The STATIONNAME procedure return value stores the length of the STATIONNAME file attribute value.

The following is the file FOO/FILE/H, which is a header file used by the C client program to declare the imported procedure STATIONNAME:

extern “ALGOL” char *STATIONNAME (__file_t, __malloc_t,
                                  __copy_to_ptr_t);

Of the items in this header file, __file_t corresponds to parameter F in the ALGOL library; __malloc_t corresponds to MALLOC; and __copy_to_ptr_t corresponds to COPYTOPTR.

The following is the C client program, FOO/FILE/C:

#include <stdio.h>
#include “foo.file.h” (bytitle=“OBJECT/FOO/FILE/A”, intname=“LIB”)

main () {
   printf (“this printf implicitly opens stdout.\n”);
   printf (“title=\”%s\”\n”, STATIONNAME (stdout->_file_no));
}

The first printf statement in the C client program implicitly opens the file stdout as a remote file. The second printf statement implicitly invokes the STATIONNAME procedure and displays the STATIONNAME file attribute value stored in the C client program heap.

Note that the STATIONNAME invocation does not mention the __malloc_t and __copy_to_ptr_t parameters, because these are automatically passed by the C compiler. However, the STATIONNAME invocation explicitly passes __file_t a parameter of type int, which the C compiler changes into a parameter of type file, as required by the ALGOL library. The int value that is passed should always be extracted from a FILE* pointer, as shown by the ->_file_no clause in this example.