As the C interface provided by the ISO_C_BINDING module has matured, it is becoming relatively easy to incorporate C-based libraries into Fortran programs. The following is a collection of interfaces to common C POSIX system functions. These have recently only been tested with GNU Fortran 7.4.0 but should be portable to other modern Fortran compilers.
Many were written early in the availability of the ISO_C_BINDING and could be simplified or improved upon (I used what worked at the time); please feel free to do so. This is intended to be a seed for others to build on.
There is a cursory example program included in the source for each of the procedures.
Using the GNU compiler as an example and the three following files a demonstration of the M_system(3f) module can be tested using
gfortran -c M_system.f90 gcc -c C-M_system.c gfortran -I. demo.f90 C-M_system.o M_system.o -o demo ./demo
This is a subset of the procedures available in the GPF (General Purpose Fortran) collection, which includes sample codes and man(1) pages for these routines and others.
M_system(3f) - Fortran interface to C system interface procedures (mostly POSIX)
**Public objects:** use m_system, only : set_environment_variable, system_unsetenv, & system_putenv, system_getenv use m_system, only : system_intenv, system_readenv, system_clearenv use M_system, only : system_getcwd, system_link, & system_mkfifo, system_remove, system_rename, & system_umask, system_unlink, & system_rmdir, system_chdir, system_mkdir, & system_stat, system_isdir, system_islnk, system_isreg, & system_isblk, system_ischr, system_isfifo, & system_realpath, & system_access, & system_utime, & system_issock, system_perm use M_system, only : system_errno, system_perror use M_system, only : system_getegid, system_geteuid, system_getgid, & system_gethostname, system_getpid, system_getppid, system_getsid, & system_getuid, system_uname use M_system, only : system_kill use M_system, only : system_rand, system_srand use M_system, only : system_cpu_time
M_system(3fm) is a collection of Fortran procedures that call C or a C wrapper using the ISO_C_BINDING interface to access system calls. System calls are a special set of functions used by programs to communicate directly with an operating system.
Generally, system calls are slower than normal function calls because when you make a call control is relinquished to the operating system to perform the system call. In addition, depending on the nature of the system call, your program may be blocked by the OS until the system call has finished, thus making the execution time of your program even longer.
system_putenv(3f): call putenv(3c)
system_getenv(3f): function call to get_environment_variable(3f)
system_unsetenv(3f): call unsetenv(3c) to remove variable from environment
set_environment_variable(3f): set environment variable by calling setenv(3c)
system_initenv(3f): initialize environment table for reading
system_readenv(3f): read next entry from environment table
system_clearenv(3f): emulate clearenv(3c) to clear environment
system_chdir(3f): call chdir(3c) to change current directory of a process
system_getcwd(3f): call getcwd(3c) to get pathname of current working directory
system_stat(3f): determine system information of file by name
system_perm(3f): create string representing file permission and type
system_access(3f): determine filename access or existence
system_isdir(3f): determine if filename is a directory
system_islnk(3f): determine if filename is a link
system_isreg(3f): determine if filename is a regular file
system_isblk(3f): determine if filename is a block device
system_ischr(3f): determine if filename is a character device
system_isfifo(3f): determine if filename is a fifo - named pipe
system_issock(3f): determine if filename is a socket
system_realpath(3f): resolve a pathname
system_chmod(3f): call chmod(3c) to set file permission mode
system_chown(3f): call chown(3c) to set file owner
system_getumask(3f): call umask(3c) to get process permission mask
system_setumask(3f): call umask(3c) to set process permission mask
system_mkdir(3f): call mkdir(3c) to create empty directory
system_mkfifo(3f): call mkfifo(3c) to create a special FIFO file
system_link(3f): call link(3c) to create a filename link
system_rename(3f): call rename(3c) to change filename
system_remove(3f): call remove(3c) to remove file
system_rmdir(3f): call rmdir(3c) to remove empty directory
system_unlink(3f): call unlink(3c) to remove a link to a file
system_utime(3f): call utime(3c) to set file access and modification times
Some vendors provide their own way to access POSIX functions and make those available as modules; for instance …
(The code was very long and seemed to be causing the wiki software to hang, due to syntax highlighting I think so I replaced it with a link to the GitHub repository. —Jason Blevins)