[Robelle] [SmugBook] [Index] [Prev] [Next]

Fopen Routine in MPE and UNIX

Both MPE and UNIX have routines named FOPEN and FCLOSE which open and close files. The MPE versions are more complex, since the MPE file system is more complex (it has record types and sizes, user labels, etc.). On UNIX a file is just a stream of bytes. In fact, MPE has superseded Fopen with the even more complex Hpfopen routine.

Foptions Means File-Type-Options

The foptions value is a 16-bit unsigned integer made up of 1, 2 and 3-bit fields. The 1-bit fields have value 1 to enable the option. Most fields default to the 0 value. Remember that MPE numbers bit fields as (startbit:bitcount) and leftmost bit is 0:

(1:1)  (2:3) (5:1) (6:1) (7:1) (8:2) (10:3)  (13:1)  (14:2)
-------------------------------------------------------------
Byte   File  No    Tape  CCTL  Record  Def.  Ascii/ Domain
stream type? :File label       format  desig Binary
-------------------------------------------------------------
New!!  0=std                   0=fix   0=file        0=new
       1=ksam                  1=var   1=$stdlist    1=old
       2=rio                   2=undef 2=$newpass    2=temp
       3=ksam/xl               3=spool 3=$oldpass    3=either
       4=cir                           4=$stdin
       5=nm spoofle                    5=$stdinx
       6=msg                           6=$null
-------------------------------------------------------------

Aoptions Means Access-Type Options

The aoptions value is a 16-bit unsigned integer made up of 1 to 4-bit fields. The 1-bit fields have value 1 to enable the option. Most fields default to the 0 value. Remember that MPE numbers bit fields as (startbit:bitcount) and leftmost bit is 0:

(2:1) (3:1) (4:1)  (5:2) (7:1) (8:2) (10:1)  (11:1) (12:4)
-------------------------------------------------------------
Glob  File  Nowait Multi Nobuf Excl  Locking Multi  Access
open  Copy  I/O    Access      acc           record type
-------------------------------------------------------------
                   0=none      0=default          0=read
                   1=intra-job 1=exclusive        1=write
                   2=inter-job 2=excl+read        2=write/save
                               3=share            3=append
                                                  4=inout
                                                  5=update
                                                  6=execute
-------------------------------------------------------------

HPFOPEN Gives You Mapped Access and More

When HP needed new parameters and options for opening files, they added a new intrinsic to MPE: HPFOPEN. It accepts a variable number of pairs of parameters, the first of which is an Itemnum and the second is the Item_Value.

    HPFOPEN (filenum,     {32-bit integer by reference}
             status,      {32-bit integer by reference}
             itemnum,     {32-bit integer by value}
             itemval,     {type varies, by reference}
             ...          {up to 41 pairs}
             );

A few important things to remember: the status is actually decoded as two 16-bit values, Itemnum 2 the file name must have an opening "quote" that also appears at the end, you can open a new file and save it at the same time, and you can open a file with mapped access.

FCLOSE Intrinsic on MPE

FCLOSE does at least five distinct functions:
 FCLOSE(filenum      {16-bit integer value }

       ,disposition  {0=as is, 1=save, 2= temp}
                     {3=temp, no rewind, 4=delete}
                     {5=convert perm to temp NEW!!}
                     {.(11:2)=1 truncates, 2=trims}
       ,securitycode {1=tight, creator only}
       );

FCONTROL versus fcntl

The MPE FCONTROL intrinsic and the UNIX fcntl function both provide control over open files, but are otherwise dissimilar. The terminal control functions of FCONTROL, such as control code 12 to turn Echo on, are not part of fcntl; see stty, termio, and ioctl instead. The file-lock functions of fctnl are provided by the FLOCK and FUNLOCK intrinsics of MPE, if at all. The undocumented control codes 31 and 30 of Fcontrol (Vplus Block mode On and Off) are incorporated into the blmode functions of HP-UX.

On UNIX, "fopen" is MUCH Simpler

There are only two parameters to fopen on UNIX: the filename and the access type ("r", "w", "a", "rb", "wb", "ab"..)
    if ((destfp = fopen(dest_file,"r")) == NULL)
       fprintf(stderr, "error opening %s file\n", dest_file);

The access types are:

"r" : open for reading
"w" : erase or create new empty
"a" : append or create for writing
"rb" : open binary file for reading
"wb" : erase binary file or create empty
"ab" : open binary file for appending
"r+" : open for reading and writing
"w+" : erase, open for reading/writing
"a+" : retain contents, read/write
"r+b" or "rb+" : open binary file for read/write
"w+b" or "wb+" : erase binary file, read/write
"a+b" or "ab+" : retain contents of binary file, read/write

Closing a File on UNIX

The fclose() routine takes only one parameter:
    int fclose(FILE *stream)
fclose() writes any buffered data for the named stream and then closes it. This is automatic for all open files when you call exit(2).


[Robelle] [SmugBook] [Index] [Mpetips] [Prev] [Next]