system function to determine OS 
Author Message
 system function to determine OS

I am looking for a function call to determine what
OS platform a fortran program is runninng on that will
work from MacOS X, Windows, and linux, and not throw
an error on the other platforms.

Something like call system('something') but with the
ability to capture the result in a variable. As
(ignore variable types, etc.):

      os=ostype()
      if(os.eq.mac) ...
      if(os.eq.windows) ...
      if(os.eq.linux) ...

Thanks!



Sun, 11 Sep 2011 23:45:58 GMT  
 system function to determine OS
Since this information is known at compile time, I think the best
solution is to define a parameter with the value in, and stick it in a
module.  Then just uncomment the right one at compile time.

"Clever" ways to do this at run-time are liable to come unstuck when
Apple/MS/Linux change the rules without telling you.

        INTEGER,PARAMETER :: MAC=1,WINDOWS=2,LINUX=3
         INTEGER,PARAMETER :: OS=MAC
!!        INTEGER,PARAMETER :: OS=WINDOWS
!!        INTEGER,PARAMETER :: OS=LINUX

Quote:

> I am looking for a function call to determine what
> OS platform a fortran program is runninng on that will
> work from MacOS X, Windows, and linux, and not throw
> an error on the other platforms.

> Something like call system('something') but with the
> ability to capture the result in a variable. As
> (ignore variable types, etc.):

>      os=ostype()
>      if(os.eq.mac) ...
>      if(os.eq.windows) ...
>      if(os.eq.linux) ...

> Thanks!

--
John Appleyard
Polyhedron Software
Programs for Programmers - QA, Compilers, Graphics, Consultancy
********* Visit our Web site on http://www.polyhedron.co.uk/ *********


Mon, 12 Sep 2011 02:50:51 GMT  
 system function to determine OS

Quote:

> Since this information is known at compile time, I think the best
> solution is to define a parameter with the value in, and stick it in a
> module.  Then just uncomment the right one at compile time.

> "Clever" ways to do this at run-time are liable to come unstuck when
> Apple/MS/Linux change the rules without telling you.

>     INTEGER,PARAMETER :: MAC=1,WINDOWS=2,LINUX=3
>         INTEGER,PARAMETER :: OS=MAC
> !!        INTEGER,PARAMETER :: OS=WINDOWS
> !!        INTEGER,PARAMETER :: OS=LINUX

Interesting. If doing this in C, I would use preprocessor macros and
compile my program with cc -DUSING_MAC, cc -DUSING_LINUX, or cc
-DUSING_WINDOWS.

My program would contain blocks like

#ifdef USING_MAC
   --- do mac stuff ...
#endif

#ifdef USING_LINUX
   --- do linux stuff ...
#endif

#ifdef USING_WINDOWS
   --- do windows stuff ...
#endif

Is there a Fortran analog to this ?



Mon, 12 Sep 2011 03:35:15 GMT  
 system function to determine OS

Quote:

> Since this information is known at compile time, I think the best
> solution is to define a parameter with the value in, and stick it in a
> module.  Then just uncomment the right one at compile time.
> "Clever" ways to do this at run-time are liable to come unstuck when
> Apple/MS/Linux change the rules without telling you.
>        INTEGER,PARAMETER :: MAC=1,WINDOWS=2,LINUX=3
>         INTEGER,PARAMETER :: OS=MAC
> !!        INTEGER,PARAMETER :: OS=WINDOWS
> !!        INTEGER,PARAMETER :: OS=LINUX

I am not sure that it is completely known at compile
time, but it might be about as close as you can get.
(Consider wine for running windows executables
on Linux, for example.)

On most systems there are C preprocessor symbols
that indicate at compile time the system that
is in use.  (Or that the compiler believes is in use.)

Those could also be set on the compiler command
line in a makefile.

-- glen



Mon, 12 Sep 2011 03:49:01 GMT  
 system function to determine OS
Quote:

> Since this information is known at compile time, I think the best
> solution is to define a parameter with the value in, and stick it in a
> module.  Then just uncomment the right one at compile time.

> "Clever" ways to do this at run-time are liable to come unstuck when
> Apple/MS/Linux change the rules without telling you.

>     INTEGER,PARAMETER :: MAC=1,WINDOWS=2,LINUX=3
>         INTEGER,PARAMETER :: OS=MAC
> !!        INTEGER,PARAMETER :: OS=WINDOWS
> !!        INTEGER,PARAMETER :: OS=LINUX


>> I am looking for a function call to determine what
>> OS platform a fortran program is runninng on that will
>> work from MacOS X, Windows, and linux, and not throw
>> an error on the other platforms.

>> Something like call system('something') but with the
>> ability to capture the result in a variable. As
>> (ignore variable types, etc.):

>>      os=ostype()
>>      if(os.eq.mac) ...
>>      if(os.eq.windows) ...
>>      if(os.eq.linux) ...

>> Thanks!

Another option would be to create a separate module for each
machine/OS and USE that module to get the OS name.  This
has the advantage that you won't forget to change the
parameter definition when you compile on different machines.
The drawback is that you have several nearly identical
modules with the same name on each system.  And there is no
fail safe for running the program on a system that isn't
expected.

Dick Hendrickson



Mon, 12 Sep 2011 03:53:13 GMT  
 system function to determine OS
Thanks all. I'll work around it. I just thought there
might be such a call.


Mon, 12 Sep 2011 06:53:37 GMT  
 system function to determine OS

Quote:

> I am looking for a function call to determine what
> OS platform a fortran program is runninng on that will
> work from MacOS X, Windows, and linux, and not throw
> an error on the other platforms.

Here is my workaround. It works for gfortran on Mac Leopard,
and Windows XP anyway. Good enough for now.

       character*40 os,ostype
       os=ostype()
       write(6,901)os(1:len(trim(os)))
  901  format('OS=',a)
       end
       character*40 function ostype()
c an attempt to determine the OS the Fortran is running on
       logical flag
       ostype=''
c Windows
       inquire(file='c:/windows',exist=flag)
       if (flag) ostype='Windows'//''
c linux/unix
       inquire(file='/etc',exist=flag)
       if (flag) ostype='linux/unix'//''
c Mac (must come after linux/unix test)
       inquire(file='/Applications',exist=flag)
       if (flag) ostype='Mac'//''
       return
       end



Mon, 12 Sep 2011 08:10:41 GMT  
 system function to determine OS

Quote:


> > I am looking for a function call to determine what
> > OS platform a fortran program is runninng on that will
> > work from MacOS X, Windows, and linux, and not throw
> > an error on the other platforms.

> Here is my workaround. It works for gfortran on Mac Leopard,
> and Windows XP anyway. Good enough for now.

  [code elided]

That's awfully fragile. Perhaps it sometimes works on gfortran; that
isn't handy for me to test at the moment, but it assumes an awful lot of
things.

First, it assumes behavior of inquire on directories as opposed to
files. That's not covered in the standard and I think I recall cases of
people not getting the results they expected, depending on the
particular compiler, and perhaps even the compiler version.

Second, it appears to be assuming that a Unix system can't have a
directory named /Applications. That's not a particularly robust
assumption. Even if we ignore the fact that OS X is a unix system and
almost always has an /Applications directory, it is also quite possible
for any other unix system to have such a directory - perhaps not very
typical, but certainly possible.

Likewise, it appears to be assuming that a Windows XP system can't have
either an /etc directory or an /Applications directory. Many of my
WIndows XP systems have had an etc directory, and they aren't
particularly unusual in that. I've seen other software that assumed such
a directory. It is common for Fortran compilers to accept "/" as an
alternare for "\" in Windows pathnames.

Oh, and it also seems to assume that Windows XP systems are always
installed on a C drive. I've had ones that weren't. (But I eventually
stopped that because too much other software also seemed to just assume
that and failed when it wasn't so.)

But these all just illustrate the wisdom of John Appleyard's advice,
where he said:

Quote:
>> "Clever" ways to do this at run-time are liable to come
>> unstuck when Apple/MS/Linux change the rules without
>> telling you.

These particular issues don't even require the rules to change. They are
problems even when sticking to the current rules. They might happen to
work on your particular current systems with your particular current
compiler, but they are very fragile. John Appleyard has previously
evidenced a pretty good background in programming for multiple systems.
I'd recommend sticking with something along the lines of his advice on
this.

--
Richard Maine
email: last name at domain . net
domain: summer-triangle



Mon, 12 Sep 2011 08:48:22 GMT  
 system function to determine OS
In Dread Ink, the Grave Hand of John Did Inscribe:

Quote:
> Thanks all. I'll work around it. I just thought there
> might be such a call.

You can simulate the set command, divert to text42.txt and read in using an
open statement:

E:\gfortran\dan>gfortran sys2.f90 -Wall -o out.exe

E:\gfortran\dan>out

E:\gfortran\dan>type text42.txt
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\dan\Application Data
CLIENTNAME=Console
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=TJA1-D5768E2D16
ComSpec=C:\WINDOWS\system32\cmd.exe
f95include=C:\Program Files\Silverfrost\FTN95\include
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Documents and Settings\dan
LIBRARY_PATH=C:\q\lib\gcc-lib\i686-pc-mingw32\4.0.4;C:\q\lib
LOGONSERVER=\\TJA1-D5768E2D16
mod_path=C:\Program Files\Silverfrost\FTN95\include
NUMBER_OF_PROCESSORS=1
OS=Windows_NT
Path=C:\Program Files\Silverfrost\FTN95;C:\q\bin;
C:\Perl\site\bin;C:\Perl\bin;C
:\MinGW\bin; C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem; E:\gfortra
n\libexec\gcc\i586-pc-mingw32\4.4.0;E:\gfortran\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 15 Model 4 Stepping 10, AuthenticAMD
PROCESSOR_LEVEL=15
PROCESSOR_REVISION=040a
ProgramFiles=C:\Program Files
PROMPT=$P$G
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\DOCUME~1\dan\LOCALS~1\Temp
TMP=C:\DOCUME~1\dan\LOCALS~1\Temp
USERDOMAIN=TJA1-D5768E2D16
USERNAME=dan
USERPROFILE=C:\Documents and Settings\dan
windir=C:\WINDOWS

E:\gfortran\dan>type sys2.f90
implicit integer(a-t)

call system ('set >text42.txt')
endprogram
! gfortran sys2.f90 -Wall -o out.exe
E:\gfortran\dan>

Cheers,
--
Frank

...it's obviously how his disease manifests itself, any kind of substance
dependency is very deep, issues of self esteem, you can just tell that he's
a really insecure and vulnerable person -- and I love him. You know,
sometimes I listen to him on the radio, and he's very judgmental, he's a
very angry person, and I just want to remind him that anytime you have a
finger pointing at someone else, there's three pointing back at you.
~~ Al Franken



Mon, 12 Sep 2011 11:55:45 GMT  
 system function to determine OS

Quote:

> > Since this information is known at compile time, I think the best
> > solution is to define a parameter with the value in, and stick it in a
> > module. ?Then just uncomment the right one at compile time.

> > "Clever" ways to do this at run-time are liable to come unstuck when
> > Apple/MS/Linux change the rules without telling you.

> > ? ? INTEGER,PARAMETER :: MAC=1,WINDOWS=2,LINUX=3
> > ? ? ? ? INTEGER,PARAMETER :: OS=MAC
> > !! ? ? ? ?INTEGER,PARAMETER :: OS=WINDOWS
> > !! ? ? ? ?INTEGER,PARAMETER :: OS=LINUX

> Interesting. If doing this in C, I would use preprocessor macros and
> compile my program with cc -DUSING_MAC, cc -DUSING_LINUX, or cc
> -DUSING_WINDOWS.

> My program would contain blocks like

> #ifdef USING_MAC
> ? ?--- do mac stuff ...
> #endif

> #ifdef USING_LINUX
> ? ?--- do linux stuff ...
> #endif

> #ifdef USING_WINDOWS
> ? ?--- do windows stuff ...
> #endif

> Is there a Fortran analog to this ?

How about encapsulating the OS specific functionality in a module and
then compiling that specific module during the build process depending
on what the target OS is?

I would rather use a makefile (yuck) than conditional compilation
(yuck**2).

-- e



Mon, 12 Sep 2011 12:45:13 GMT  
 system function to determine OS


Quote:
> Since this information is known at compile time, I think the best
> solution is to define a parameter with the value in, and stick it in a
> module.  Then just uncomment the right one at compile time.

Some information is available at compile time, other parts of the OS
might change after compilation and the related information should be
gathered at runtime.  Or you might have one executable that is
shared by many different machines, each with slightly different
versions of their OS.

The POSIX fortran routine to get OS information at runtime is
PXFUNAME.  Do a google search for the details.  This is not standard
fortran, it is part of an obsolete standard fortran interface to
POSIX system utility functions.  Although not portable to all
computers, POSIX interface routines are as close as possible to
being portable whle still outside of the language standard itself.

$.02 -Ron Shepard



Mon, 12 Sep 2011 13:49:27 GMT  
 system function to determine OS

Quote:

> #ifdef USING_MAC
>    --- do mac stuff ...
> #endif

> #ifdef USING_LINUX
>    --- do linux stuff ...
> #endif

> #ifdef USING_WINDOWS
>    --- do windows stuff ...
> #endif

> Is there a Fortran analog to this ?

Yes, but it is not part of the fortran standard.  All compilers that
I know of at present support the above cpp syntax, usually triggered
by the filename extension .F or .F90.  Because it is borrowed from
the C language, it has some quirks in fortran programs.  If your
compiler does not support this directly, then you can either use cpp
(usually with some options to make it work better for fortran) or
the perl script filepp (do a google search for details).

There is also a fortran preprocessor, with its own unique syntax,
but it does not appear to be popular enough for vendors to support
it.

$.02 -Ron Shepard



Mon, 12 Sep 2011 13:57:25 GMT  
 system function to determine OS

Quote:

> Some information is available at compile time, other parts of the OS
> might change after compilation and the related information should be
> gathered at runtime.  Or you might have one executable that is
> shared by many different machines, each with slightly different
> versions of their OS.

You might, but what do you expect in that case?

What do you expect for Wine under Linux?

There are other cross-environment systems when using
the same processor which mostly use the dynamic link
libraries and enough code to make them run.  That
would likely work between Linux and MacOS X on intel.

Running VMWare, you would expect it to report on
the virtual machine not the underlying real system.

Oh, my windows system has the windows root as k:\winnt.
(Both the drive and directory name are install options.)
A windows system might also have a /etc/ directory,
and even a /etc/passwd file.

-- glen



Mon, 12 Sep 2011 14:32:37 GMT  
 system function to determine OS


Quote:

>The POSIX fortran routine to get OS information at runtime is
>PXFUNAME.  Do a google search for the details.  This is not standard
>fortran, it is part of an obsolete standard fortran interface to
>POSIX system utility functions.  Although not portable to all
>computers, POSIX interface routines are as close as possible to
>being portable whle still outside of the language standard itself.

Don't bet on it.  POSIX is FAR less portable, even among Unices, than
most people think it is.  Firstly, some areas are so badly specified
as to be completely unusable unless you rely on system dependencies
(threading and signals being two).  And, secondly, some of the more
baroque areas are unlikely to be implemented, or may be implemented
incorrectly (which is sometimes still conforming!)

If PXFUNAME is available, it should deliver something that will
usually be different between systems.  A stronger statement may not
be possible.

Regards,
Nick Maclaren.



Mon, 12 Sep 2011 16:52:59 GMT  
 system function to determine OS
John: just use your own (second) method, but look for the existence of
the O/S directories like "WINNT", "WINDOWS" and so on with equivalents
for Linux and Mac systems.They at least should be unique and also give
you in part the O/S type as well (95,98,NT,XP,Vsita...).


Mon, 12 Sep 2011 17:13:20 GMT  
 
 [ 32 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. system to determine OS

2. os.popen() vs os.system()

3. Using os.system and os.popen from pythonwin under Windows NT

4. os.system() or os.execv() w/stdout redirect and PID tracking

5. os.system/os.popen ultrasparc

6. Blocking ctrl-c to os.popen() or os.system()

7. Determining File Versions OS X

8. Determine OS at runtime

9. Determining how many days between dates (os/2 warp v3)

10. Determining how many days between dates (os/2 warp v3

11. Determine the drive OS/2 resides on?

 

 
Powered by phpBB® Forum Software