Recognize platforms in Fortran

Sometimes it is useful for a Fortran 90 program to know on which platform it runs. The main reasons why I wrote the module PLATFORM are to know where the home directory is and where to store the configure files (e.g. namelist-files) on the currently used platform without maintaining different platform-dependent source files.

The module PLATFORM detects:

  • DOS-based Windows platforms, as 95/98(SE)/ME
  • NT-based Windows platforms, as NT/2000/XP/Vista/7
  • Unix(-like) platforms, as Linux, MacOS X, etc.

Cygwin is recognized as the concerned Windows platform.

User Functions

  • Get_Platform returns the abbreviated name of the platform: ux=Unix/Linux/Mac OS X, nt=Windows NT/2000/XP/Vista/7, 95=Windows 95/98(SE)/ME.
  • Get_Home_Dir returns the path of the platform’s home directory. On Unix-systems it is stored in the environment variable HOME. On NT-based systems in USERPROFILE. On DOS-based Windows versions the current directory is used.
  • Get_Config_Dir returns the path where config-files are stored in general. On Unix-systems it is the HOME directory, on NT-based Windows systems the path is saved in the environment variable APPDATA, on DOS-based Windows there is not a typical directory for this purpose. I assigned the current directory for this Windows platforms.

Sample program

PROGRAM test_platform
USE Platform
PRINT*, 'Platform: ', Get_Platform()
PRINT*, 'Home:     ', TRIM(Get_Home_Dir())
PRINT*, 'Cfg-Path: ', TRIM(Get_Config_Dir())
END PROGRAM test_platform

Output on my Linux machine:

Platform: ux
Home:     /home/hi
Cfg-Path: /home/hi

Output on my Windows Vista box:

Platform: nt
Home:     C:\Users\hi
Cfg-Path: C:\Users\hi\AppData\Roaming

Requirements and Limitations

Fortran 90 compiler or higher, e.g. G95, GFortran or Intel® Fortran Compiler

  • Fortran 2000 feature GET_ENVIRONMENT_VARIABLE()
  • Conditional Compilation feature
  • GETCWD extension (intrinsic function of many current Fortran 95 compiler)

Path length is limited to 512 characters.

Licence

The user is allowed to use the software for any purpose, to distribute it, to modify it, and to distribute modified versions of the software.

See LICENCE.txt in the distribution for details.

Download

platform-f90-1.0.zip

source code distribution – module code, sample program code and documentation

Leave a Reply

Your email address will not be published. Required fields are marked *