Fortran Library Routines |
1 |
![]() |
getpid() returns INTEGER*4 and would require an INTEGER*4 getpid declaration to ensure proper handling of the result. (Without explicit typing, a REAL result would be assumed by default because the function name starts with g.) As a reminder, explicit type statements appear in the function summaries for these routines.Be aware that
IMPLICIT statements and the -r8, -i2, -dbl and -xtypemap compiler options also alter the data typing of arguments and return values. A mismatch between the expected and actual data types in calls to these library routines could cause unexpected behavior. Options -r8 and -dbl promote the data type of INTEGER functions to INTEGER*8, REAL functions to REAL*8, and DOUBLE functions to REAL*16. To protect against these problems, function names and variables appearing in library calls should be explicitly typed with their expected sizes, as in:
integer*4 seed, getuid real*4 ran ... seed = 70198 val = getuid() + ran(seed) ... |
Explicit typing in the example protects the library calls from any data type promotion when the
-r8 and -dbl compiler options are used. Without explicit typing, these options could produce unexpected results. See the Fortran User's Guide for details on these options.
abort: Terminate and Write Memory to Core File
The subroutine is called by:
call abort |
abort flushes the I/O buffers and then aborts the process, possibly producing a core file in the current directory. See limit(1) about limiting or supressing core dumps.
access: Check File for Permissions or Existence
The function is called by:
|
INTEGER*4 access status = access ( name, mode ) | |||
|
name |
character |
Input |
File name |
|
mode |
character |
Input |
Permissions |
|
Return value |
INTEGER*4 |
Output |
status=0: OK |
access determines if you can access the file name with the permissions specified by mode.
access returns zero if the access specified by mode would be successful.
|
r w x blank |
Test for read permission Test for write permission Test for execute permission Test for existence of the file |
Example 1: Test for read/write permission:
INTEGER*4 access, status status = access ( 'taccess.data', 'rw' ) if ( status .eq. 0 ) write(*,*) "ok" if ( status .ne. 0 ) write(*,*) 'cannot read/write', status |
Example 2: Test for existence:
INTEGER*4 access, status status = access ( 'taccess.data', ' ' ) ! blank mode if ( status .eq. 0 ) write(*,*) "file exists" if ( status .ne. 0 ) write(*,*) 'no such file', status |
See also gerror(3F) to interpret error codes.
alarm: Execute a Subroutine after a Specified Time
The function is called by:
Example: alarm--wait 9 seconds then call sbrtn:
See also: alarm(3C), sleep(3F), and signal(3F).
The alternate external versions for MIL-STD-1753 are:
See also "mvbits: Move a Bit Field," on page 65, and the chapter on Intrinsic Functions in the Fortran 77 4.2 Reference.
x = and( word1, word2 )
x = or( word1, word2 )
x = xor( word1, word2 )
x = not( word )
x = rshift( word, nbits )
x = lshift( word, nbits )
Usage: and, or, xor, not, rshift, lshift
word, word1, word2, nbits are integer input arguments. These are generic functions expanded inline by the compiler. The data type returned is that of the first argument.
call bic( bitnum, word )
call bis( bitnum, word )
call setbit( bitnum, word, state )
x = bit( bitnum, word )
Usage: bic, bis, bit, setbit
bitnum, state, and word are
LOGICAL bit
INTEGER*4 input arguments. Function bit() returns a logical value.
chdir: Change Default Directory
The function is called by:
INTEGER*4 chdir n = chdir( dirname ) | |||
|
dirname |
character |
Input |
Directory name |
|
Return value |
INTEGER*4 |
Output |
n=0: OK, n>0: Error code |
Example: chdir--change cwd to MyDir:
INTEGER*4 chdir, n n = chdir ( 'MyDir' ) if ( n .ne. 0 ) stop 'chdir: error' end |
See also: chdir(2), cd(1), and gerror(3F) to interpret error codes.
chmod: Change the Mode of a File
The function is called by:
Example: chmod--add write permissions to MyFile:
character*18 name, mode INTEGER*4 chmod, n name = 'MyFile' mode = '+w' n = chmod( name, mode ) if ( n .ne. 0 ) stop 'chmod: error' end |
See also: chmod(1), and gerror(3F) to interpret error codes.
date: Get Current System Date as a Character String
The subroutine is called by:
|
call date( c ) | |||
|
c |
CHARACTER*9 |
Output |
Variable, array, array element, or character substring |
The form of the returned string c is: dd-mmm-yy
dd
Day of the month, as a 2-digit integer
mmm
Month, as a 3-letter abbreviation
yy
Year, as a 2-digit integer
See also "idate: Return Current System Date" on page 30."
dtime, etime: Elapsed Execution Time
Both functions have return values of elapsed time (or -1.0 as error indicator).
The time is in seconds. The resolution is to a nanosecond under Solaris 2.x. dtime: Elapsed Time Since the Last dtime Call
For dtime, the elapsed time is:
The function is called by:
Note - Do not call dtime from within a parallelized loop.
Example: dtime(), single processor:
etime: Elapsed Time Since Start of Execution
For etime, the elapsed time is:
For aparallelized Fortran program linked with libF77_mt, if the environment variable PARALLEL is:
Note: The initial call to
etime will be inaccurate. It merely enables the system clock. Do not use the value returned by the initial call to etime.
See also times(2), f77(1), and the Fortran Programmer's Guide.
exit: Terminate a Process and Set the Status
The subroutine is called by:
|
call exit( status ) | ||
|
status |
INTEGER*4 |
Input |
... if(dx .lt. 0.) call exit( 0 ) ... end |
exit flushes and closes all the files in the process, and notifies the parent process if it is executing a wait.
fdate: Return Date and Time in an ASCII String
The subroutine or function is called by:
|
call fdate( string ) | ||
|
string |
character*24 |
Output |
CHARACTER fdate*24 string = fdate() |
If used as a function, the calling routine must define the type and length of fdate. | ||
|
Return value |
character*24 |
Output | |
Example 1: fdate as a subroutine:
character*24 string call fdate( string ) write(*,*) string end |
Wed Aug 3 15:30:23 1994 |
Example 2: fdate as a function, same output:
character*24 fdate write(*,*) fdate() end |
See also: ctime(3), time(3F), and idate(3F).
flush: Flush Output to a Logical Unit
The subroutine is called by:
|
call flush( lunit ) | |||
|
lunit |
INTEGER*4 |
Input |
Logical unit |
The flush subroutine flushes the contents of the buffer for the logical unit, lunit, to the associated file. This is most useful for logical units 0 and 6 when they are both associated with the console terminal.
fork: Create a Copy of the Current Process
The function is called by:
INTEGER*4 fork n = fork() | |||
|
Return value |
INTEGER*4 |
Output |
n>0: n=Process ID of copy n<0, n=System error code |
The fork function creates a copy of the calling process. The only distinction between the two processes is that the value returned to one of them, referred to as the parent process, will be the process ID of the copy. The copy is usually referred to as the child process. The value returned to the child process will be zero.
INTEGER*4 fork, pid pid = fork() if(pid.lt.0) stop 'fork error' if(pid.gt.0) then print *, 'I am the parent' else print *, 'I am the child' endif |
A corresponding exec routine has not been provided because there is no satisfactory way to retain open logical units across the exec routine. However, the usual function of fork/exec can be performed using system(3F). See also: fork(2), wait(3F), kill(3F), system(3F), and perror(3F).
free: Deallocate Memory Allocated by Malloc
The subroutine is called by:
|
call free ( ptr ) | ||
|
ptr |
pointer |
Input |
free deallocates a region of memory previously allocated by malloc. The region of memory is returned to the memory manager; it is no longer available to the user's program.
real x pointer ( ptr, x ) ptr = malloc ( 10000 ) call free ( ptr ) end |
See "malloc: Allocate Memory and Get Address" on page 64," for details.
fseek, ftell: Determine Position and Reposition a File
fseek and ftell are routines that permit repositioning of a file. ftell returns a file's current position as an offset of so many bytes from the beginning of the file. At some later point in the program, fseek can use this saved offset value to reposition the file to that same place for reading. fseek: Reposition a File on a Logical Unit
The function is called by:
Example: fseek()--Reposition MyFile to two bytes from the beginning
INTEGER*4 fseek, lunit/1/, offset/2/, from/0/, n open( UNIT=lunit, FILE='MyFile' ) n = fseek( lunit, offset, from ) if ( n .gt. 0 ) stop 'fseek error' end |
:
ftell: Return Current Position of File
The function is called by:
INTEGER*4 ftell n = ftell( lunit ) | |||
|
lunit |
INTEGER*4 |
Input |
Open logical unit |
|
Return value |
INTEGER*4 |
Input |
n>=0: n=Offset in bytes from start of file n<0: n=System error code |
INTEGER*4 ftell, lunit/1/, n open( UNIT=lunit, FILE='MyFile' ) ... n = ftell( lunit ) if ( n .lt. 0 ) stop 'ftell error' ... |
See also fseek(3S) and perror(3F).
getarg, iargc: Get Command-line Arguments
getarg and iargc access arguments on the command line (after expansion by the command-line preprocessor. getarg: Get the kth Command-Line Argument
The subroutine is called by:
|
call getarg( k, arg ) | |||
|
k |
INTEGER*4 |
Input |
Index of argument (0=first=command name) |
|
arg |
character*n |
Output |
kth argument |
|
n |
INTEGER*4 |
Size of arg |
Large enough to hold longest argument |
iargc: Get the Number of Command-Line Arguments
The function is called by:
|
m = iargc() | |||
|
Return value |
INTEGER*4 |
Output |
Number of arguments on the command line |
Example: iargc and getarg, get argument count and each argument:
See also execve(2) and getenv(3F).
getc, fgetc: Get Next Character
getc and fgetc get the next character. getc: Get Next Character from stdin
The function is called by:
INTEGER*4 getc status = getc( char ) | |||
|
char |
character |
Output |
Next character |
|
Return value |
INTEGER*4 |
Output |
status=0: OK status=-1: End of file status>0: System error code or f77 I/O error code |
Example: getc gets each character from the keyboard; note the Control-D (^D):
character char INTEGER*4 getc, status status = 0 do while ( status .eq. 0 ) status = getc( char ) write(*, '(i3, o4.3)') status, char end do end |
After compiling, a sample run of the above source is:
For any logical unit, do not mix normal Fortran input with getc().
fgetc: Get Next Character from Specified Logical Unit
The function is called by:
Example: fgetc gets each character from tfgetc.data; note the linefeeds (Octal 012):
character char INTEGER*4 fgetc, status open( unit=1, file='tfgetc.data' ) status = 0 do while ( status .eq. 0 ) status = fgetc( 1, char ) write(*, '(i3, o4.3)') status, char end do end |
After compiling, a sample run of the above source is:
demo% cat tfgetc.data ab yz demo% a.out 0 141 `a' read 0 142 `b' read 0 012 linefeed read 0 171 `y' read 0 172 `z' read 0 012 linefeed read -1 012 CONTROL-D read demo% |
For any logical unit, do not mix normal Fortran input with fgetc().
getcwd: Get Path of Current Working Directory
The function is called by:
INTEGER*4 getcwd, status character*64 dirname status = getcwd( dirname ) if ( status .ne. 0 ) stop 'getcwd: error' write(*,*) dirname end |
See also: chdir(3F), perror(3F), and getwd(3).
getenv: Get Value of Environment Variables
The subroutine is called by:
|
call getenv( ename, evalue ) | |||
|
ename |
character*n |
Input |
Name of the environment variable sought |
|
evalue |
character*n |
Output |
Value of the environment variable found; blanks if not successful |
The size of ename and evalue must be large enough to hold their respective character strings.
$SHELL:
character*18 evalue call getenv( 'SHELL', evalue ) write(*,*) "'", evalue, "'" end |
See also: execve(2) and environ(5).
getfd: Get File Descriptor for External Unit Number
The function is called by:
|
fildes = getfd( unitn ) | |||
|
unitn |
INTEGER*4 |
Input |
External unit number |
|
Return value |
INTEGER*4 |
Output |
File descriptor if file is connected; |
INTEGER*4 fildes, getfd, unitn/1/ open( unitn, file='tgetfd.data' ) fildes = getfd( unitn ) if ( fildes .eq. -1 ) stop 'getfd: file not connected' write(*,*) 'file descriptor = ', fildes end |
See also open(2).
getfilep: Get File Pointer for External Unit Number
The function is:
This function is used for mixing standard Fortran I/O with C I/O. Such a mix is nonportable, and is not guaranteed for subsequent releases of the operating system or Fortran. Use of this function is not recommended, and no direct interface is provided. You must create your own C routine to use the value returned by getfilep. A sample C routine is shown below.
Sample C function actually using getfilep:
|
tgetfilepC.c
|
#include <stdio.h> int c_read_ ( fd, buf, nbytes, buf_len ) FILE **fd ; char *buf ; int *nbytes, buf_len ; {
return fread( buf, 1, *nbytes, *fd ) ; } |
A sample compile-build-run is:
demo 11% cc -c tgetfilepC.c demo 12% f77 tgetfilepC.o tgetfilepF.f tgetfileF.f: MAIN: demo 13% a.out What is the digit? 3 The digit read by C is 3 demo 14% |
For more information, read the chapter on the C-Fortran interface in the Sun Fortran Programmer's Guide. See also open(2).
getlog: Get User's Login Name
The subroutine is called by:
|
call getlog( name ) | |||
|
name |
character*n |
Output |
User's login name, or all blanks if the process is running detached from a terminal. n should be large enough to hold the longest name. |
character*18 name call getlog( name ) write(*,*) "'", name, "'" end |
See also getlogin(3).
getpid: Get Process ID
The function is called by:
|
pid = getpid() | |||
|
Return value |
INTEGER*4 |
Output |
Process ID of the current process |
INTEGER*4 getpid, pid pid = getpid() write(*,*) 'process id = ', pid end |
See also getpid(2).
getuid, getgid: Get User or Group ID of Process
getuid and getgid get the user or group ID of the process, respectively. getuid: Get User ID of the Process
The function is called by:
|
uid = getuid() | |||
|
Return value |
INTEGER*4 |
Output |
User ID of the process |
getgid: Get Group ID of the Process
The function is called by:
|
gid = getgid() | |||
|
Return value |
INTEGER*4 |
Output |
Group ID of the process |
Example: getuid() and getpid():
INTEGER*4 getuid, getgid, gid, uid uid = getuid() gid = getgid() write(*,*) uid, gid end |
See also: getuid(2).
hostnm: Get Name of Current Host
The function is called by:
|
status = hostnm( name ) | |||
|
name |
character*n |
Output |
Name of current host system. n must be large enough to hold the host name. |
|
Return value |
INTEGER*4 |
Output |
status=0: OK status>0: Error |
INTEGER*4 hostnm, status character*8 name status = hostnm( name ) write(*,*) 'host name = "', name, '"' end |
See also gethostname(2).
idate: Return Current System Date
idate has two versions:
The standard version puts the current system date into one integer array: day, month, and year.
The subroutine is called by:
|
call idate( iarray ) Standard Version | |||
|
iarray |
INTEGER*4 |
Output |
array(3). Note the order: day, month, year. |
Example: idate (standard version):
demo% cat tidate.f INTEGER*4 iarray(3) call idate( iarray ) write(*, "(' The date is: ',3i5)" ) iarray
end demo% f77 -silent tidate.f demo% a.out The date is: 10 8 1994 demo% |
The VMS version puts the current system date into three integer variables: month, day, and year
|
call idate( m, d, y ) VMS Version | |||
|
m |
INTEGER*4 |
Output |
Month (1 - 12) |
|
d |
INTEGER*4 |
Output |
Day (1 - 7) |
|
y |
INTEGER*4 |
Output |
Year (1 - 99) |
deom% cat titime.f INTEGER*4 m, d, y call idate ( m, d, y ) write (*, "(' The date is: ',3i5)" ) m, d, y
end demo% f77 -silent tidateV.f -lV77 demo% a.out The date is: 8 10 94 |
ieee_flags,ieee_handler,sigfpe: IEEE Arithmetic
These subprograms provide modes and status required to fully exploit ANSI/IEEE Std 754-1985 arithmetic in a Fortran program. They correspond closely to the functions ieee_flags(3M), ieee_handler(3M), and sigfpe(3).
See the Sun Numerical Computation Guide for details on how these functions can be used strategically.
ieee_flags(action,mode,in,out):
For ieee_handler( action,in,out) :
INTEGER*4 ieeer character*1 mode, out, in ieeer = ieee_flags( 'set', 'direction', 'tozero', out ) |
Example 2: Clear rounding direction to default (round toward nearest):
character*1 out, in ieeer = ieee_flags('clear','direction', in, out )
|
Example 3: Clear all accrued exception-occurred bits:
character*18 out ieeer = ieee_flags( 'clear', 'exception', 'all', out ) |
Example 4: Detect overflow exception as follows:
character*18 out ieeer = ieee_flags( 'get', 'exception', 'overflow', out ) if (out .eq. 'overflow' ) stop 'overflow' |
The above code sets out to overflow and ieeer to 25 (this value is platform dependent). Similar coding detects exceptions, such as invalid or inexact.
See the Numerical Computation Guide. See also: floatingpoint(3), signal(3), sigfpe(3), f77_floatingpoint(3F), ieee_flags(3M), and ieee_handler(3M).
f77_floatingpoint.h: Fortran IEEE Definitions
The file f77_floatingpoint.h defines constants and types used to implement standard floating-point according to ANSI/IEEE Std 754-1985.
#include <f77/f77_floatingpoint.h> |
Use of this
include file requires preprocessing prior to Fortran compilation.The source file referencing this include file will automatically be preprocessed if the name has a .F or .F90 extension.
|
fp_direction_type |
The type of the IEEE rounding direction mode. The order of enumeration varies according to hardware. |
|
fp_class_type |
A list of the classes of IEEE floating-point values and symbols. |
Refer to the Numerical Computation Guide. See also ieee_environment(3M) and f77_ieee_environment(3F).
index,rindex,lnblnk: Index or Length of Substring
|
index(a1,a2) |
Index of first occurrence of string a2 in string a1 |
|
rindex(a1,a2) |
Index of last occurrence of string a2 in string a1 |
|
lnblnk(a1) |
Index of last nonblank in string a1 |
index has the following forms:
index: First Occurrence of String a2 in String a1
The index is an intrinsic function called by:
|
n = index( a1, a2 ) | |||
|
a1 |
character |
Input |
Main string |
|
a2 |
character |
Input |
Substring |
|
Return value |
INTEGER*4 |
Output |
n>0: Index of first occurrence of a2 in a1 n=0: a2 does not occur in a1. |
rindex: Last Occurrence of String a2 in String a1
The function is called by:
|
n = rindex( a1, a2 ) | |||
|
a1 |
character |
Input |
Main string |
|
a2 |
character |
Input |
Substring |
|
Return value |
INTEGER*4 |
Output |
n>0: Index of last occurrence of a2 in a1 n=0: a2 does not occur in a1 |
lnblnk: Last Nonblank in String a1
The function is called by:
|
n = lnblnk( a1 ) | |||
|
a1 |
character |
Input |
String |
|
Return value |
INTEGER*4 |
Output |
n>0: Index of last nonblank in a1 n=0: a1 is all nonblank |
Example: index(), rindex(), lnblnk():
inmax: Return Maximum Positive Integer
The function is called by:
|
m = inmax() | |||
|
Return value |
INTEGER*4 |
Output |
The maximum positive integer |
INTEGER*4 inmax, m m = inmax() write(*,*) m end demo% f77 -silent tinmax.f demo% a.out 2147483647 demo% |
See also libm_single(3f) and libm_double(3f).
ioinit: Initialize I/O: Carriage Control, File Names, ...
The IOINIT routine establishes properties of file I/O for files opened after the call to IOINIT. The file I/O properties that IOINIT controls are as follows:
INTEGER*2 IEOF, ICTL, IBZR COMMON /_ _IOIFLG/ IEOF, ICTL, IBZR ! Not in user name space |
In releases prior to SC 3.0.1, the labeled common block was named IOIFLG. We changed this name to _ _IOIFLG to prevent conflicts with any user-defined common blocks.
Source Code
Some user needs are not satisfied with a generic version of IOINIT, so we provide the source code. It is written in Fortran 77. The location is:
This search and connection is provided only for NN between 0 and 19, inclusive. For NN \> 19, nothing is done; see "Source Code" on page 42.
vrbose (IOINIT Activity)
If the argument vrbose is .TRUE., then ioinit reports on its own activity.
call ioinit( .true., .false., .false., 'FORT', .false.) |
You can assign file name in at least two ways.
demo$ FORT01=mydata demo$ FORT12=myresults demo$ export FORT01 FORT12 demo$ myprogram |
demo% setenv FORT01 mydata demo% setenv FORT12 myresults demo% myprogram |
With either shell, the ioinit call in the above example gives these results:
You can set environment variables as follows, using either sh or csh:
demo$ FORT01=tioinit.data demo$ FORT12=tioinit.au demo$ export FORT01 FORT12 demo$ |
demo% a.out demo% cat tioinit.au abc 123 PDQ 789 demo% |
demo% a.out demo% cat tioinit.au abc 123 PDQ 789 demo% |
itime: Current System Time
itime puts the current system time into an integer array: hour, minute, and second.
|
call itime( iarray ) | |||
|
iarray |
INTEGER*4 |
Output |
array(3): hour, minute, second |
demo% cat titime.f INTEGER*4 iarray(3) call itime( iarray ) write (*, "(' The time is: ',3i5)" ) iarray
end demo% f77 -silent titime.f demo% a.out The time is: 15 42 35 |
See also time(3f), ctime(3F), and fdate(3F).
kill: Send a Signal to a Process
The function is called by:
Example (fragment): Send a message using kill():
INTEGER*4 kill, pid, signum * ... status = kill( pid, signum ) if ( status .ne. 0 ) stop 'kill: error' write(*,*) 'Sent signal ', signum, ' to process ', pid end |
The function sends signal signum, and integer signal number, to the process pid. Valid signal numbers are listed in the C include file /usr/include/sys/signal.h
libm_double: libm Double-Precision Functions
These subprograms are double-precision libm functions and subroutines. Intrinsic Functions
The following Fortran intrinsic functions return double-precision values if they have double-precision arguments. You need not put them in a type statement. If the function needed is available as an intrinsic function, it is simpler to use an intrinsic than a non-intrinsic function.
Non-Intrinsic Functions
In general, these functions do not correspond to standard Fortran generic intrinsic functions--data types are determined by the usual data typing rules.
See also: intro(3M) and the Numerical Computation Guide.
libm_quadruple: libm Quad-Precision Functions
These subprograms are quadruple-precision (REAL*16) libm functions and subroutines (SPARC and PowerPC only). Intrinsic Functions
The following Fortran intrinsic functions return quadruple-precision values if they have quadruple-precision arguments. You need not put them in a type statement. If the function needed is available as an intrinsic function, it is simpler to use an intrinsic than a non-intrinsic function.
Non-Intrinsic Functions
In general, these do not correspond to standard generic intrinsic functions; data types are determined by the usual data typing rules.
If you need to use any other quadruple-precision libm function, you can call it using $PRAGMA C(fcn) before the call. For details, see the chapter on the C-Fortran interface in the Sun Fortran Programmer's Guide.
libm_single: libm Single-Precision Functions
These subprograms are single-precision libm functions and subroutines. Intrinsic Functions
The following Fortran intrinsic functions return single-precision values if they have single-precision arguments. If the function needed is available as an intrinsic function, it may be simpler to use it than a non-intrinsic function.
Non-Intrinsic Functions
In general, the functions below provide access to single-precision libm functions that do not correspond to standard Fortran generic intrinsic functions--data types are determined by the usual data typing rules.
See also: intro(3M) and the Numerical Computation Guide.
link, symlnk: Make a Link to an Existing File
link creates a link to an existing file. symlink creates a symbolic link to an existing file.
link: Create a Link to an Existing File
Example 1: link: Create a link named data1 to the file, tlink.db.data.1:
symlnk: Create a Symbolic Link to an Existing File
Example 2: symlnk: Create a symbolic link named data1 to the file, tlink.db.data.1:
See also: link(2), symlink(2), perror(3F), and unlink(3F).
loc: Return the Address of an Object
The function is called by:
|
k = loc( arg ) | |||
|
arg |
Any type |
Input |
Variable, array, or structure name |
|
Return value |
INTEGER*4 |
Output |
Address of arg |
INTEGER*4 k, loc real arg / 9.0 / k = loc( arg ) write(*,*) k end |
long, short: Integer Object Conversion
long and short handle integer object conversions. long: Convert a Short Integer to a Long Integer
The function is called by:
|
call ExpecLong( long(int2) ) | ||
|
int2 |
INTEGER*2 |
Input |
|
Return value |
INTEGER*4 |
Output |
short: Convert a Long Integer to a Short Integer
The function is:
|
call ExpecShort( short(int4) ) | ||
|
int4 |
INTEGER*4 |
Input |
|
Return value |
INTEGER*2 |
Output |
Example (fragment): long() and short():
integer*4 int4/8/, long integer*2 int2/8/, short call ExpecLong( long(int2) ) call ExpecShort( short(int4) ) ... end |
long is useful if constants are used in calls to library routines and the code is compiled with the -i2 option.
longjmp, isetjmp: Return to Location Set by isetjmp
isetjmp sets a location for longjmp; longjmp returns to that location. isetjmp: Set the Location for longjmp
The function is called by:
|
ival = isetjmp( env ) | |||
|
env |
INTEGER*4 |
Output |
env is a 12-element integer array |
|
Return value |
INTEGER*4 |
Output |
ival = 0 if isetjmp is called explicitly ival |
Note: On PowerPC, env must be
INTEGER*4 env(64). longjmp: Return to the location set by isetjmp
The subroutine is called by:
Note: On PowerPC, env must be
INTEGER*4 env(64). Description
The isetjmp and longjmp routines are used to deal with errors and interrupts encountered in a low-level routine of a program.
longjmp must be at least 12 elements long (64 on PowerPC).
|
k = malloc( n ) | |||
|
n |
INTEGER*4 |
Input |
Number of bytes of memory |
|
Return value |
INTEGER*4 |
Output |
k>0: k=address of the start of the block of memory allocated k=0: Error |
The function malloc allocates an area of memory and returns the address of the start of that area. The region of memory is not initialized in any way-- don't program assuming it is preset to zero!
pointer ( p1, X ) real*4 X ... p1 = malloc( 1000 ) if ( p1 .eq. 0 ) stop 'malloc: cannot allocate' do 11 i=1,1000/4 11 X(i) = 0. ... end |
In the above example, we acquire 1,000 bytes of memory, pointed to by
p1, and initialize it to zero.
mvbits: Move a Bit Field
Example: mvbits:
32
32
|
perror |
Print a message to Fortran logical unit 0, stderr. |
|
gerror |
Get a system error message (of the last detected system error) |
|
ierrno |
Get the error number of the last detected system error. |
These routines perform the following functions:
perror: Print Message to Logical Unit 0, stderr
The subroutine is called by:
|
call perror( string ) | |||
|
string |
character*n |
Input |
The message. It is written preceding the standard error message for the last detected system error. |
call perror( "file is for formatted I/O" ) |
gerror: Get Message for Last Detected System Error
The subroutine or function is called by:
|
call gerror( string ) | |||
|
string |
character*n |
Output |
Message for the last detected system error |
Example 2: gerror() as a subroutine:
character string*30 ... call gerror ( string ) write(*,*) string |
Example 3: gerror() as a function; string not used:
character gerror*30, z*30 ... z = gerror( ) write(*,*) z |
ierrno: Get Number for Last Detected System Error
The function is called by:
|
n = ierrno() | |||
|
Return value |
INTEGER*4 |
Output |
Number of last detected system error |
This number is updated only when an error actually occurs. Most routines and I/O statements that might generate such errors return an error code after the call; that value is a more reliable indicator of what caused the error condition.
INTEGER*4 ierrno, n ... n = ierrno() write(*,*) n |
See also intro(2) and perror(3).
fputc writes to a logical unit.
These functions write a character to the file associated with a Fortran logical unit bypassing normal Fortran I/O.
For any one unit, do not mix normal Fortran output with output by these functions.
|
status = putc( char ) | |||
|
char |
character |
Input |
The character to write to the unit |
|
Return value |
INTEGER*4 |
Output |
status=0: OK status>0: System error code |
character char, s*10 / 'OK by putc' / INTEGER*4 putc, status do i = 1, 10 char = s(i:i) status = putc( char ) end do status = putc( '\n' ) end demo% f77 -silent tputc.f demo% a.out OK by putc demo% |
fputc: Write to Specified Logical Unit
The function is called by:
See also putc(3S), intro(2), and perror(3F).
qsort: Sort the Elements of a One-dimensional Array
The subroutine is called by:
The compar(arg1,arg2) arguments are elements of array, returning:
|
Negative |
If arg1 is considered to precede arg2 |
|
Zero |
If arg1 is equivalent to arg2 |
|
Positive |
If arg1 is considered to follow arg2 |
ran: Generate a Random Number between 0 and 1
Repeated calls to ran generate a sequence of random numbers with a uniform distribution.
r = ran( i ) | |||
|
i |
INTEGER*4 |
Input |
Variable or array element |
|
r |
REAL |
Output |
Variable or array element |
SEED = 6909 * SEED + 1 (MOD 2**32) |
drand returns double precision values in the range 0.0 through 1.0.
irand returns positive integers in the range 0 through 2147483647.
These functions use random(3) to generate sequences of random numbers. The three functions share the same 256 byte state array. The only advantage of these functions is that they are widely available on UNIX systems. For better random number generators, compare lcrans, addrans, and shufrans; also see the Numerical Computation Guide.
integer*4 v(5), iflag/0/ do i = 1, 5 v(i) = irand( iflag ) end do write(*,*) v end demo% f77 -silent trand.f demo% a.out 2078917053 143302914 1027100827 1953210302 755253631 demo% |
See also random(3).
rename: Rename a File
The function is called by:
If the file specified by to exists, then both from and to must be the same type of file, and must reside on the same file system. If to exists, it is removed first.
:
See also rename(2) and perror(3F).
t = secnds( t0 )
t0
REAL
Input
Constant, variable, or array element
Return Value
REAL
Output
Number of seconds since midnight, minus t0
secnds: Get System Time in Seconds, Minus Argument
Example: secnds:
character*18 string / 'ls > MyOwnFile.names' / INTEGER*4 status, sh status = sh( string ) if ( status .ne. 0 ) stop 'sh: error' ... end |
The function sh passes string to the sh shell as input, as if the string had been typed as a command.
See also: execve(2), wait(2), and system(3).
Note: string cannot be longer than 1,024 characters.
If proc is called, it is passed the signal number as an integer argument.
SIGFPE_DEFAULT, SIGFPE_IGNORE, and SIGFPE_ABORT. See page 36
See also kill(1), signal(3), and kill(3F), and Numerical Computation Guide.
sleep: Suspend Execution for an Interval
The subroutine is called by:
|
call sleep( itime ) | |||
|
itime |
INTEGER*4 |
Input |
Number of seconds to sleep |
The actual time can be up to 1 second less than itime due to granularity in system timekeeping.
INTEGER*4 time / 5 / write(*,*) 'Start' call sleep( time ) write(*,*) 'End' end |
See also sleep(3).
stat, lstat, fstat: Get File Status
These functions return the following information:
fstat: Get Status for File, by Logical Unit
The function
is called by:
lstat: Get Status for File, by File Name
The function is called by:
|
ierr = lstat ( name, statb ) | |||
|
name |
character*n |
Input |
File name |
|
statb |
INTEGER*4 |
Output |
Status array of file, 13 elements |
|
Return value |
INTEGER*4 |
Output |
ierr=0: OK ierr>0: Error code |
Detail of Status Array for Files
The meaning of the information returned in the INTEGER*4 array statb is as described for the structure stat under stat(2).
See also stat(2), access(3F), perror(3F), and time(3F).
system: Execute a System Command
The function is called by:
character*8 string / 'ls s*' / INTEGER*4 status, system status = system( string ) if ( status .ne. 0 ) stop 'system: error' end |
The function system passes string to your shell as input, as if the string had been typed as a command. Note: string cannot be longer than 1024 characters.
The system() function is not MT-safe. Do not call it from multithreaded or parallelized programs.
These routines have the following functions:
time: Get System Time
For time(), there are two versions, a standard version and a VMS version. If you use the f77 command-line option -lV77, then you get the VMS version for time() and for idate(); otherwise, you get the standard versions.
|
n = time() Standard Version | |||
|
Return value |
INTEGER*4 |
Output |
Time, in seconds, since 0:0:0, GMT, 1/1/70 |
The function time() returns an integer with the time since 00:00:00 GMT,
January 1, 1970, measured in seconds. This is the value of the operating system clock.
INTEGER*4 n, time n = time() write(*,*) 'Seconds since 0 1/1/70 GMT = ', n end demo% f77 -silent ttime.f demo% a.out The time is: 771967850 demo% |
|
call time( t ) VMS Version | |||
|
t |
character*8 |
Output |
Time, in the form hh:mm:ss |
Example: time(t), VMS version, ctime--convert the system time to ASCII:
character t*8 call time( t ) write(*, "(' The current time is ', A8 )") t
end demo% f77 -silent ttimeV.f -lV77 demo% a.out The current time is 08:14:13 demo% |
ctime: Convert System Time to Character
The function ctime converts a system time, stime, and returns it as a 24-character ASCII string.
The format of the ctime returned value is shown in the following example. It is described in the man page ctime(3C).
character*24 ctime, string INTEGER*4 n, time n = time() string = ctime( n ) write(*,*) 'ctime: ', string end demo% f77 -silent tctime.f demo% a.out ctime: Mon Aug 12 10:35:38 1991 demo% |
ltime: Split System Time to Month, Day,... (Local)
This routine dissects a system time into month, day, and so forth, for the local time zone.
|
call ltime( stime, tarray ) | |||
|
stime |
INTEGER*4 |
Input |
System time from time() (standard version) |
|
tarray |
INTEGER*4(9) |
Output |
System time, local, as day, month, year, ... |
For the meaning of the elements in tarray, see the next section.
integer*4 stime, tarray(9), time stime = time() call ltime( stime, tarray ) write(*,*) 'ltime: ', tarray end demo% f77 -silent tltime.f demo% a.out ltime: 25 49 10 12 7 91 1 223 1 demo% |
gmtime: Split System Time to Month, Day, ... (GMT)
This routine dissects a system time into month, day, and so on, for GMT.
|
call gmtime( stime, tarray ) | |||
|
stime |
INTEGER*4 |
Input |
System time from time() (standard version) |
|
tarray |
INTEGER*4(9) |
Output |
System time, GMT, as day, month, year, ... |
integer*4 stime, tarray(9), time stime = time() call gmtime( stime, tarray ) write(*,*) 'gmtime: ', tarray end demo% f77 -silent tgmtime.f demo% a.out gmtime: 12 44 19 18 5 94 6 168 0 demo% |
Here are the tarray() values, from ctime: index, units, and range:
See also: ctime(3C), idate(3F), and fdate(3F).
topen, tclose, tread,..., tstate: Tape I/O
Manipulate magnetic tape from a Fortran program using these functions:
On any one unit, do not mix these functions with standard Fortran I/O.
topen: Associate a Device with a Tape Logical Unit
This function does not move the tape. See perror(3f) for details.
topen ok: 0 1 /dev/rst0 |
n = tclose ( tlu )
tlu
INTEGER*4
Input
Tape logical unit, in range 0 to 7
n
INTEGER*4
Return value
n=0: OK
n<0: Error
tclose: Write EOF, Close Tape Channel, Disconnect tlu
INTEGER*4 tclose
Example: tclose()--close an opened 1/4-inch tape file:
Caution - tclose() places an EOF marker immediately after the current location of the unit pointer, and then closes the unit. So if you trewin() a unit before you tclose() it, its contents are discarded.
tclose ok: 0 1 /dev/rst0 |
twrite: Write Next Physical Record to Tape
The physical record length is the size of buffer.
twrite ok: 512 1 /dev/rst0 |
tread: Read Next Physical Record from Tape
If the tape is at EOF or EOT, then tread does a return; it does not read tapes.
tread ok: 512 1 /dev/rst0 abcd |
n = trewin ( tlu )
tlu
INTEGER*4
Input
Tape logical unit, in range 0 to 7
n
INTEGER*4
Return value
n=0: OK
n<0: Error
trewin: Rewind Tape to Beginning of First Data File
If the tape is labeled, then the label is skipped over after rewinding.
INTEGER*4 trewin
Example 2: trewin()--in a two-record file, try to read three records, rewind, read one record:
1 abcd 2 wxyz 3 wxyz trewin ok: 0 1 /dev/rst0 abcd |
tskipf: Skip Files and Records; Reset EoF Status
This function does not skip backward.
INTEGER*4 nfiles / 4 /, nrecords / 1 /, tskipf, tlu / 1 / ... n = tskipf( tlu, nfiles, nrecords ) IF ( n .LT. 0 ) STOP "tskipf: cannot skip" ... |
Compare with tstate in the next section.
tstate: Get Logical State of Tape I/O Channel
For details, see st(4s).
n = tskipf( tlu, 1, 0). |
Then you can read any valid record that follows.
The next example uses tstate() to trap EOF and get at all files.
A summary of EOF and EOT follows:
The function is called by:
isatty: Is this Unit a Terminal?
The function
|
terminal = isatty( lunit ) | |||
|
lunit |
INTEGER*4 |
Input |
Logical unit |
|
Return value |
LOGICAL |
Output |
terminal=true: It is a terminal device terminal=false: It is not a terminal device |
is called by:
Example: Determine if lunit is a tty:
character*12 name, ttynam INTEGER*4 lunit /5/ logical isatty, terminal terminal = isatty( lunit ) name = ttynam( lunit ) write(*,*) 'terminal = ', terminal, ', name = "', name, '"' end |
terminal = T, name = "/dev/ttyp1 " |
unlink: Remove a File
The function is called by:
|
n = unlink ( patnam ) | |||
|
patnam |
character*n |
Input |
File name |
|
Return value |
INTEGER*4 |
Output |
n=0: OK n>0: Error |
The function unlink removes the file specified by path name patnam. If this is the last link to the file, the contents of the file are lost.
call unlink( 'tunlink.data' ) end demo% f77 -silent tunlink.f demo% ls tunl* tunlink.f tunlink.data demo% a.out demo% ls tunl* tunlink.f demo% |
See also: unlink(2), link(3F), and perror(3F). Note: the path names cannot be longer than MAXPATHLEN as defined in <sys/param.h>.
wait: Wait for a Process to Terminate
The function is:
wait suspends the caller until a signal is received, or one of its child processes terminates. If any child has terminated since the last wait, return is immediate. If there are no children, return is immediate with an error code.
INTEGER*4 n, status, wait ... n = wait( status ) if ( n .lt. 0 ) stop 'wait: error' ... end |
See also: wait(2), signal(3F), kill(3F), and perror(3F).