Last active
March 1, 2021 22:14
-
-
Save drikosev/c5b650e8cf2e0ec68d9ba193c6ace47f to your computer and use it in GitHub Desktop.
Hardcode or find at Runtime OS Name and File Separator in Fortran (by m_sys.java).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module m_sys | |
implicit none | |
public get_os_name | |
public get_user_home | |
public get_arg_prefix | |
public get_file_sep | |
character(len=20), save :: os_name = ' ' | |
character(len=255), save :: homedir = ' ' | |
character(len=1), save :: file_sep = ' ' | |
character(len=1), save :: arg_prefix = ' ' | |
private os_name, homedir, file_sep, arg_prefix; | |
private find_os_name | |
private find_user_home | |
contains | |
function get_os_name() result (os) | |
character(len=20) :: os | |
if ( len(trim(os_name)) == 0 ) then | |
os_name = find_os_name() | |
end if | |
os = os_name | |
end function get_os_name | |
function get_user_home() result (str) | |
character(len=255) :: str | |
if ( len(trim(homedir)) == 0 ) then | |
homedir = find_user_home(get_os_name(),file_sep,arg_prefix) | |
end if | |
str = homedir | |
end function get_user_home | |
function get_arg_prefix() result (str) | |
character(len=1) :: str | |
if ( len(trim(arg_prefix)) == 0 ) then | |
homedir = find_user_home(get_os_name(),file_sep,arg_prefix) | |
end if | |
str = arg_prefix | |
end function get_arg_prefix | |
function get_file_sep() result (str) | |
character(len=1) :: str | |
if ( len(trim(file_sep)) == 0 ) then | |
homedir = find_user_home(get_os_name(),file_sep,arg_prefix) | |
end if | |
str = file_sep | |
end function get_file_sep | |
function find_os_name() result(os_name) | |
character(len=20) :: os_name | |
character :: sep | |
logical :: file_exists | |
CALL get_environment_variable('OS', os_name) | |
if (len(trim(os_name)) == 0) then | |
CALL get_environment_variable('OSTYPE', os_name) | |
if (len(trim(os_name)) == 0) then | |
inquire (file='/usr/bin/sw_vers', exist=file_exists) | |
if (file_exists) then | |
os_name = 'Darwin' | |
return | |
end if | |
inquire (file='/proc/version', exist=file_exists) | |
if (file_exists) then | |
os_name = 'Linux' | |
return | |
end if | |
end if | |
if (len(trim(os_name)) == 0) then | |
os_name = 'nix' | |
end if | |
else if (os_name(1:3) == 'Win') then | |
os_name = 'Windows' | |
CALL get_environment_variable('PWD', sep) | |
if (sep == '/') then | |
os_name = 'Linux' | |
end if | |
end if | |
end function find_os_name | |
function find_user_home(os_name,file_sep,arg_prefix) result(homedir) | |
character(len=20) :: os_name | |
character(len=255) :: homedir | |
character(len=1) :: file_sep | |
character(len=1) :: arg_prefix | |
character(len=20) :: user | |
character(len=8) :: home = '/home/' | |
if (os_name(1:3) == 'Win') then | |
if (len(trim(file_sep)) == 0) then | |
file_sep = '\' | |
end if | |
if (len(trim(arg_prefix)) == 0) then | |
arg_prefix = '/' | |
end if | |
CALL get_environment_variable('USERPROFILE', homedir) | |
homedir = trim(homedir) // trim(file_sep) | |
else | |
if (len(trim(file_sep)) == 0) then | |
file_sep = '/' | |
end if | |
if (len(trim(arg_prefix)) == 0) then | |
arg_prefix = '-' | |
end if | |
if (os_name(1:3) == 'Mac' .or. os_name(1:6) == 'Darwin') then | |
home = '/Users/' | |
endif | |
CALL get_environment_variable('HOME', homedir) | |
if ( index(homedir,'root') > 0 ) then | |
CALL get_environment_variable('LOGNAME', user) | |
if ( trim(user) == 'root') then | |
CALL get_environment_variable('SUDO_USER', user) | |
if ( trim(user) == 'root' .or. len(trim(user)) == 0 ) then | |
CALL get_environment_variable('HOME', homedir) | |
homedir = trim(homedir) // trim(file_sep) | |
else | |
homedir = trim(home) // trim(user) // trim(file_sep) | |
endif | |
else | |
homedir = trim(home) // trim(user) // trim(file_sep) | |
endif | |
else | |
homedir = trim(homedir) // trim(file_sep) | |
endif | |
end if | |
end | |
end module m_sys | |
program test | |
use m_sys | |
print *, ' OS name: ', get_os_name() | |
print *, ' Home: ', get_user_home() | |
print *, ' File sep: ', get_file_sep() | |
print *, 'Arg Prefix: ', get_arg_prefix() | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A Java Class that sets some system variables for Fortran programs. | |
gcj --main=m_sys m_sys.java -o m_sys && ./m_sys -rt -main > x.f90 && gfc x.f90 && ./a.out | |
OR | |
javac m_sys.java && java m_sys -rt -main > x.f90 && gfc x.f90 && ./a.out | |
OR | |
javac m_sys.java && java m_sys > x.f90 && gfc x.f90 && ./a.out | |
Arguments | |
1. -rt | |
Find OS Name & File Separator at Runtime. Otherwise, hardcode them now. | |
2. -main | |
Add a program unit, just for testing. | |
In example, to create a module + main Fortran in Darwin with a hardcoded OS Name, type: | |
javac m_sys.java && java m_sys -main > m_sysDarwin.f90 && gfc m_sysDarwin.f90 && ./a.out | |
In example, to create a module + main Fortran in Linux with a hardcoded OS Name, type: | |
javac m_sys.java && java m_sys -main > m_sysLinux.f90 && gfc m_sysLinux.f90 && ./a.out | |
*/ | |
class m_sys { | |
public m_sys(){} | |
public static void main(String argv[]){ | |
String os_name = System.getProperty("os.name"); | |
String os_file_sep = System.getProperty("file.separator"); | |
String os_prefix = os_file_sep.equals("/") ? "-" : "/"; | |
String os_home = System.getProperty("user.home"); | |
boolean dynamic = false; | |
boolean test = false; | |
for( int i=0; argv != null && i < argv.length; i++) | |
if ( argv[i] != null) { | |
if ((argv[i].equalsIgnoreCase("-rt") || argv[i].equalsIgnoreCase("/rt"))) { | |
dynamic = true; | |
} | |
if ((argv[i].equalsIgnoreCase("-main") || argv[i].equalsIgnoreCase("/main"))) { | |
test = true; | |
} | |
} | |
if ( os_name != null) { | |
if ( os_name.indexOf("Windows")>=0 || os_name.indexOf("windows")>=0) | |
os_name = new String("Windows"); | |
if ( dynamic) { | |
os_prefix = new String(" = ' '"); | |
os_file_sep = new String(" = ' '"); | |
os_home = new String(" = ' '"); | |
os_name = new String(" = ' '"); | |
} | |
else { | |
os_home = new String(" = ' '"); | |
os_name = new String(" = '" + os_name + "'"); | |
os_file_sep = new String(" = '" + os_file_sep + "'"); | |
os_prefix = new String(" = '" + os_prefix + "'"); | |
} | |
} | |
System.out.println("module m_sys "); | |
System.out.println(" implicit none "); | |
System.out.println(" "); | |
System.out.println(" public get_os_name "); | |
System.out.println(" public get_user_home "); | |
System.out.println(" public get_arg_prefix "); | |
System.out.println(" public get_file_sep "); | |
System.out.println(" "); | |
System.out.println(" character(len=20), save :: os_name " + os_name ); | |
System.out.println(" character(len=255), save :: homedir " + os_home ); | |
System.out.println(" character(len=1), save :: file_sep " + os_file_sep ); | |
System.out.println(" character(len=1), save :: arg_prefix " + os_prefix ); | |
System.out.println(" "); | |
System.out.println(" private os_name, homedir, file_sep, arg_prefix; "); | |
System.out.println(" private find_os_name "); | |
System.out.println(" private find_user_home "); | |
System.out.println(" "); | |
System.out.println(" contains "); | |
System.out.println(" "); | |
System.out.println(" function get_os_name() result (os) "); | |
System.out.println(" character(len=20) :: os "); | |
System.out.println(" if ( len(trim(os_name)) == 0 ) then "); | |
System.out.println(" os_name = find_os_name() "); | |
System.out.println(" end if "); | |
System.out.println(" os = os_name "); | |
System.out.println(" end function get_os_name "); | |
System.out.println(" "); | |
System.out.println(" function get_user_home() result (str) "); | |
System.out.println(" character(len=255) :: str "); | |
System.out.println(" if ( len(trim(homedir)) == 0 ) then "); | |
System.out.println(" homedir = find_user_home(get_os_name(),file_sep,arg_prefix) "); | |
System.out.println(" end if "); | |
System.out.println(" str = homedir "); | |
System.out.println(" end function get_user_home "); | |
System.out.println(" "); | |
System.out.println(" function get_arg_prefix() result (str) "); | |
System.out.println(" character(len=1) :: str "); | |
System.out.println(" if ( len(trim(arg_prefix)) == 0 ) then "); | |
System.out.println(" homedir = find_user_home(get_os_name(),file_sep,arg_prefix) "); | |
System.out.println(" end if "); | |
System.out.println(" str = arg_prefix "); | |
System.out.println(" end function get_arg_prefix "); | |
System.out.println(" "); | |
System.out.println(" function get_file_sep() result (str) "); | |
System.out.println(" character(len=1) :: str "); | |
System.out.println(" if ( len(trim(file_sep)) == 0 ) then "); | |
System.out.println(" homedir = find_user_home(get_os_name(),file_sep,arg_prefix) "); | |
System.out.println(" end if "); | |
System.out.println(" str = file_sep "); | |
System.out.println(" end function get_file_sep "); | |
System.out.println(" "); | |
System.out.println(" function find_os_name() result(os_name) "); | |
System.out.println(" character(len=20) :: os_name "); | |
System.out.println(" character :: sep "); | |
System.out.println(" logical :: file_exists "); | |
System.out.println(" "); | |
System.out.println(" CALL get_environment_variable('OS', os_name) "); | |
System.out.println(" if (len(trim(os_name)) == 0) then "); | |
System.out.println(" CALL get_environment_variable('OSTYPE', os_name) "); | |
System.out.println(" if (len(trim(os_name)) == 0) then "); | |
System.out.println(" inquire (file='/usr/bin/sw_vers', exist=file_exists) "); | |
System.out.println(" if (file_exists) then "); | |
System.out.println(" os_name = 'Darwin' "); | |
System.out.println(" return "); | |
System.out.println(" end if "); | |
System.out.println(" inquire (file='/proc/version', exist=file_exists) "); | |
System.out.println(" if (file_exists) then "); | |
System.out.println(" os_name = 'Linux' "); | |
System.out.println(" return "); | |
System.out.println(" end if "); | |
System.out.println(" end if "); | |
System.out.println(" if (len(trim(os_name)) == 0) then "); | |
System.out.println(" os_name = 'nix' "); | |
System.out.println(" end if "); | |
System.out.println(" else if (os_name(1:3) == 'Win') then "); | |
System.out.println(" os_name = 'Windows' "); | |
System.out.println(" CALL get_environment_variable('PWD', sep) "); | |
System.out.println(" if (sep == '/') then "); | |
System.out.println(" os_name = 'Linux' "); | |
System.out.println(" end if "); | |
System.out.println(" end if "); | |
System.out.println(" "); | |
System.out.println(" end function find_os_name "); | |
System.out.println(" "); | |
System.out.println(" function find_user_home(os_name,file_sep,arg_prefix) result(homedir) "); | |
System.out.println(" "); | |
System.out.println(" character(len=20) :: os_name "); | |
System.out.println(" character(len=255) :: homedir "); | |
System.out.println(" character(len=1) :: file_sep "); | |
System.out.println(" character(len=1) :: arg_prefix "); | |
System.out.println(" character(len=20) :: user "); | |
System.out.println(" "); | |
System.out.println(" character(len=8) :: home = '/home/' "); | |
System.out.println(" "); | |
System.out.println(" if (os_name(1:3) == 'Win') then "); | |
System.out.println(" if (len(trim(file_sep)) == 0) then "); | |
System.out.println(" file_sep = '\\' "); | |
System.out.println(" end if "); | |
System.out.println(" if (len(trim(arg_prefix)) == 0) then "); | |
System.out.println(" arg_prefix = '/' "); | |
System.out.println(" end if "); | |
System.out.println(" "); | |
System.out.println(" CALL get_environment_variable('USERPROFILE', homedir) "); | |
System.out.println(" homedir = trim(homedir) // trim(file_sep) "); | |
System.out.println(" else "); | |
System.out.println(" if (len(trim(file_sep)) == 0) then "); | |
System.out.println(" file_sep = '/' "); | |
System.out.println(" end if "); | |
System.out.println(" if (len(trim(arg_prefix)) == 0) then "); | |
System.out.println(" arg_prefix = '-' "); | |
System.out.println(" end if "); | |
System.out.println(" if (os_name(1:3) == 'Mac' .or. os_name(1:6) == 'Darwin') then "); | |
System.out.println(" home = '/Users/' "); | |
System.out.println(" endif "); | |
System.out.println(" CALL get_environment_variable('HOME', homedir) "); | |
System.out.println(" if ( index(homedir,'root') > 0 ) then "); | |
System.out.println(" CALL get_environment_variable('LOGNAME', user) "); | |
System.out.println(" if ( trim(user) == 'root') then "); | |
System.out.println(" CALL get_environment_variable('SUDO_USER', user) "); | |
System.out.println(" if ( trim(user) == 'root' .or. len(trim(user)) == 0 ) then "); | |
System.out.println(" CALL get_environment_variable('HOME', homedir) "); | |
System.out.println(" homedir = trim(homedir) // trim(file_sep) "); | |
System.out.println(" else "); | |
System.out.println(" homedir = trim(home) // trim(user) // trim(file_sep) "); | |
System.out.println(" endif "); | |
System.out.println(" else "); | |
System.out.println(" homedir = trim(home) // trim(user) // trim(file_sep) "); | |
System.out.println(" endif "); | |
System.out.println(" else "); | |
System.out.println(" homedir = trim(homedir) // trim(file_sep) "); | |
System.out.println(" endif "); | |
System.out.println(" end if "); | |
System.out.println(" "); | |
System.out.println(" end "); | |
System.out.println(" "); | |
System.out.println(" end module m_sys "); | |
System.out.println(" "); | |
if (test){ | |
System.out.println(" program test "); | |
System.out.println(" use m_sys "); | |
System.out.println(" "); | |
System.out.println(" print *, ' OS name: ', get_os_name() "); | |
System.out.println(" print *, ' Home: ', get_user_home() "); | |
System.out.println(" print *, ' File sep: ', get_file_sep() "); | |
System.out.println(" print *, 'Arg Prefix: ', get_arg_prefix() "); | |
System.out.println(" "); | |
System.out.println(" end "); | |
} | |
System.exit(0); | |
} | |
}//class m_sys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module m_sys | |
implicit none | |
public get_os_name | |
public get_user_home | |
public get_arg_prefix | |
public get_file_sep | |
character(len=20), save :: os_name = 'Linux' | |
character(len=255), save :: homedir = ' ' | |
character(len=1), save :: file_sep = '/' | |
character(len=1), save :: arg_prefix = '-' | |
private os_name, homedir, file_sep, arg_prefix; | |
private find_os_name | |
private find_user_home | |
contains | |
function get_os_name() result (os) | |
character(len=20) :: os | |
if ( len(trim(os_name)) == 0 ) then | |
os_name = find_os_name() | |
end if | |
os = os_name | |
end function get_os_name | |
function get_user_home() result (str) | |
character(len=255) :: str | |
if ( len(trim(homedir)) == 0 ) then | |
homedir = find_user_home(get_os_name(),file_sep,arg_prefix) | |
end if | |
str = homedir | |
end function get_user_home | |
function get_arg_prefix() result (str) | |
character(len=1) :: str | |
if ( len(trim(arg_prefix)) == 0 ) then | |
homedir = find_user_home(get_os_name(),file_sep,arg_prefix) | |
end if | |
str = arg_prefix | |
end function get_arg_prefix | |
function get_file_sep() result (str) | |
character(len=1) :: str | |
if ( len(trim(file_sep)) == 0 ) then | |
homedir = find_user_home(get_os_name(),file_sep,arg_prefix) | |
end if | |
str = file_sep | |
end function get_file_sep | |
function find_os_name() result(os_name) | |
character(len=20) :: os_name | |
character :: sep | |
logical :: file_exists | |
CALL get_environment_variable('OS', os_name) | |
if (len(trim(os_name)) == 0) then | |
CALL get_environment_variable('OSTYPE', os_name) | |
if (len(trim(os_name)) == 0) then | |
inquire (file='/usr/bin/sw_vers', exist=file_exists) | |
if (file_exists) then | |
os_name = 'Darwin' | |
return | |
end if | |
inquire (file='/proc/version', exist=file_exists) | |
if (file_exists) then | |
os_name = 'Linux' | |
return | |
end if | |
end if | |
if (len(trim(os_name)) == 0) then | |
os_name = 'nix' | |
end if | |
else if (os_name(1:3) == 'Win') then | |
os_name = 'Windows' | |
CALL get_environment_variable('PWD', sep) | |
if (sep == '/') then | |
os_name = 'Linux' | |
end if | |
end if | |
end function find_os_name | |
function find_user_home(os_name,file_sep,arg_prefix) result(homedir) | |
character(len=20) :: os_name | |
character(len=255) :: homedir | |
character(len=1) :: file_sep | |
character(len=1) :: arg_prefix | |
character(len=20) :: user | |
character(len=8) :: home = '/home/' | |
if (os_name(1:3) == 'Win') then | |
if (len(trim(file_sep)) == 0) then | |
file_sep = '\' | |
end if | |
if (len(trim(arg_prefix)) == 0) then | |
arg_prefix = '/' | |
end if | |
CALL get_environment_variable('USERPROFILE', homedir) | |
homedir = trim(homedir) // trim(file_sep) | |
else | |
if (len(trim(file_sep)) == 0) then | |
file_sep = '/' | |
end if | |
if (len(trim(arg_prefix)) == 0) then | |
arg_prefix = '-' | |
end if | |
if (os_name(1:3) == 'Mac' .or. os_name(1:6) == 'Darwin') then | |
home = '/Users/' | |
endif | |
CALL get_environment_variable('HOME', homedir) | |
if ( index(homedir,'root') > 0 ) then | |
CALL get_environment_variable('LOGNAME', user) | |
if ( trim(user) == 'root') then | |
CALL get_environment_variable('SUDO_USER', user) | |
if ( trim(user) == 'root' .or. len(trim(user)) == 0 ) then | |
CALL get_environment_variable('HOME', homedir) | |
homedir = trim(homedir) // trim(file_sep) | |
else | |
homedir = trim(home) // trim(user) // trim(file_sep) | |
endif | |
else | |
homedir = trim(home) // trim(user) // trim(file_sep) | |
endif | |
else | |
homedir = trim(homedir) // trim(file_sep) | |
endif | |
end if | |
end | |
end module m_sys | |
program test | |
use m_sys | |
print *, ' OS name: ', get_os_name() | |
print *, ' Home: ', get_user_home() | |
print *, ' File sep: ', get_file_sep() | |
print *, 'Arg Prefix: ', get_arg_prefix() | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a helper C program that scans most of the environment variables | |
available to a process (hope so). The results may differ from terminal | |
commands, or if one can run the executable with sudo (or/and via ssh). | |
The environment variable 'SHELL' seems to be a good criterion when one | |
wants to distinguish between a Windows and a nix System. In Mac OS X | |
and Linux is always available, even if one runs sudo, even via ssh. It | |
is also available in Cygwin and not available outside it (Win terminal). | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#if defined(_WIN32) | |
#include <direct.h> | |
#else | |
#include <unistd.h> | |
#endif | |
char buffer[256]; | |
char *g_file=buffer; | |
char *getEnv(char *w) | |
{ | |
char* envC; | |
envC = getenv (w); | |
if (envC!=NULL) { | |
strncpy(buffer, envC,255); | |
} | |
else | |
strncpy(buffer, "\0",255); | |
g_file = buffer; | |
return g_file; | |
}// | |
int main(const int argc, const char **argv, const char *envp[]){ | |
int i; | |
if (argc>0) | |
printf("arg: %s \n", argv[0]); | |
if (argc>1) | |
printf("arg: %s \n", argv[1]); | |
printf(" -------------------------------------- \n"); | |
if (envp) { | |
for (i=0; envp[i]; i++) { | |
printf("envp[%d]=%s \n", i, envp[i]); | |
} | |
} | |
printf(" -------------------------------------- \n"); | |
printf("getenv(SHELL)=%s \n", getEnv("SHELL")); | |
printf("getenv(PWD)=%s \n", getEnv("PWD")); | |
printf("getenv(MAIL)=%s \n", getEnv("MAIL")); | |
printf("getenv(HOME)=%s \n", getEnv("HOME")); | |
printf("getenv(LOGNAME)=%s \n", getEnv("LOGNAME")); | |
printf("getenv(USER)=%s \n", getEnv("USER")); | |
printf("getenv(CD)=%s \n", getEnv("CD")); | |
g_file = getcwd(buffer, 255); | |
if ( g_file ) | |
printf("CWD=%s \n", g_file); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment