comp.lang.ada
 help / color / mirror / Atom feed
* How to detect OS type and version?
@ 2005-10-13  2:35 Roger Blum
  2005-10-13  6:05 ` Martin Dowie
                   ` (3 more replies)
  0 siblings, 4 replies; 37+ messages in thread
From: Roger Blum @ 2005-10-13  2:35 UTC (permalink / raw)


Is there a way within Ada (GNAT 3.15p) to tell whether the application is 
running on Linux or Windows (and the version of the OS)?

Regards,
Roger 





^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  2:35 How to detect OS type and version? Roger Blum
@ 2005-10-13  6:05 ` Martin Dowie
  2005-10-13  9:50   ` Stefan Bellon
                     ` (3 more replies)
  2005-10-13 12:06 ` Rob Norris
                   ` (2 subsequent siblings)
  3 siblings, 4 replies; 37+ messages in thread
From: Martin Dowie @ 2005-10-13  6:05 UTC (permalink / raw)


Roger Blum wrote:
> Is there a way within Ada (GNAT 3.15p) to tell whether the application is 
> running on Linux or Windows (and the version of the OS)?

There is no single standard why... yet!

I think that you will be able to do this with "package 
Ada.Environment_Variables" come Ada200Y, e.g.

if Ada.Environment_Variables.Exists ("OS")
    and Ada.Environment_Variables.Value ("OS") = "Windows_NT" then
    ...
elsif Ada.Environment_Variables.Exists ("OSTYPE")
       and Ada.Environment_Variables.Value ("OSTYPE") = "linux-gnu" then
    ...
else
    -- Unknown OS
    ...
end if;

I have a (partially tested) version of this package. If you (or anyone 
else) would like it, please email me directly and I'll see what I can 
do! :-)

Cheers

-- Martin



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  6:05 ` Martin Dowie
@ 2005-10-13  9:50   ` Stefan Bellon
  2005-10-13 10:39     ` Martin Dowie
  2005-10-13 18:12     ` Jeffrey R. Carter
  2005-10-13 18:11   ` Jeffrey R. Carter
                     ` (2 subsequent siblings)
  3 siblings, 2 replies; 37+ messages in thread
From: Stefan Bellon @ 2005-10-13  9:50 UTC (permalink / raw)


Martin Dowie wrote:

> I think that you will be able to do this with "package 
> Ada.Environment_Variables" come Ada200Y, e.g.
> 
> if Ada.Environment_Variables.Exists ("OS")
>     and Ada.Environment_Variables.Value ("OS") = "Windows_NT" then
>     ...
> elsif Ada.Environment_Variables.Exists ("OSTYPE")
>        and Ada.Environment_Variables.Value ("OSTYPE") = "linux-gnu"
> then ...
> else
>     -- Unknown OS
>     ...
> end if;

This approach has one problem: Those environment variables can be set
by the user, so that the user can fake the setting to select wrong OS
dependent code.

What we are doing is this: We configure our project for the target
operating system and during that configuration, a small file with
content similar to the following is generated:

separate (Bauhaus.OS)
function Get_OS return String is
begin
   return "GNU/Linux";
end Get_OS;

This way, we have the OS type compiled in our executables and can
check wherever necessary without the fear that the user may change it.
It would be nice if this was available via some language defined
package, I agree.

-- 
Dipl.-Inf. Stefan Bellon
Bauhaus Software Technologies | TTI GmbH TGZ Softwareanalysen c/o ISTE
Tel.: +49 711 78 16 221       | Universitätsstraße 38
Fax.: +49 711 78 16 380       | 70569 Stuttgart



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  9:50   ` Stefan Bellon
@ 2005-10-13 10:39     ` Martin Dowie
  2005-10-13 18:12     ` Jeffrey R. Carter
  1 sibling, 0 replies; 37+ messages in thread
From: Martin Dowie @ 2005-10-13 10:39 UTC (permalink / raw)


Stefan Bellon wrote:
> This approach has one problem: Those environment variables can be set
> by the user, so that the user can fake the setting to select wrong OS
> dependent code.

Absolutely! Though I'm not sure exactly that would acheive as presumably
they would have to fake everything else that followed! :-)

I think this is what "System.Name" was probably orginally intended to
provide but it never quite worked out that way...





^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 12:06 ` Rob Norris
@ 2005-10-13 11:59   ` Martin Dowie
  2005-10-14  0:21     ` Randy Brukardt
  2005-10-14  9:52     ` Rob Norris
  0 siblings, 2 replies; 37+ messages in thread
From: Martin Dowie @ 2005-10-13 11:59 UTC (permalink / raw)


That way doesn't give the version of the OS.





^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  2:35 How to detect OS type and version? Roger Blum
  2005-10-13  6:05 ` Martin Dowie
@ 2005-10-13 12:06 ` Rob Norris
  2005-10-13 11:59   ` Martin Dowie
  2005-10-13 20:25 ` Bernd Specht
  2005-10-14  6:30 ` Roger Blum
  3 siblings, 1 reply; 37+ messages in thread
From: Rob Norris @ 2005-10-13 12:06 UTC (permalink / raw)


On Thu, 13 Oct 2005 02:35:13 GMT, "Roger Blum"
<rogerblum@hawaii.rr.com> wrote:

>Is there a way within Ada (GNAT 3.15p) to tell whether the application is 
>running on Linux or Windows (and the version of the OS)?
>
>Regards,
>Roger 
>

One lazy dirty method is to inspect the path or directory separators.

Not very exact, but may be useful eg: 

type os_type is (Unix, Windows, Unknown);

function What_OS return os_type is
  if gnat.os_lib.directory_separator = '/' then
    return Unix;
  elsif gnat.os_lib.directory_separator = '\' then
    return Windows;
  else
    return Unknown;
  end if;
end What_OS;



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  6:05 ` Martin Dowie
  2005-10-13  9:50   ` Stefan Bellon
@ 2005-10-13 18:11   ` Jeffrey R. Carter
  2005-10-13 19:44   ` Simon Wright
  2005-10-13 21:04   ` Michael Bode
  3 siblings, 0 replies; 37+ messages in thread
From: Jeffrey R. Carter @ 2005-10-13 18:11 UTC (permalink / raw)


Martin Dowie wrote:
> 
> if Ada.Environment_Variables.Exists ("OS")
>    and Ada.Environment_Variables.Value ("OS") = "Windows_NT" then

I'd think "and then" would be appropriate here.

-- 
Jeff Carter
"I fart in your general direction."
Monty Python & the Holy Grail
05



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  9:50   ` Stefan Bellon
  2005-10-13 10:39     ` Martin Dowie
@ 2005-10-13 18:12     ` Jeffrey R. Carter
  2005-10-13 18:37       ` Stefan Bellon
  2005-10-16  0:13       ` Ray Blaak
  1 sibling, 2 replies; 37+ messages in thread
From: Jeffrey R. Carter @ 2005-10-13 18:12 UTC (permalink / raw)


Stefan Bellon wrote:

> What we are doing is this: We configure our project for the target
> operating system and during that configuration, a small file with
> content similar to the following is generated:
> 
> separate (Bauhaus.OS)
> function Get_OS return String is
> begin
>    return "GNU/Linux";
> end Get_OS;

Is there some reason you're using String rather than an enumerated type?

-- 
Jeff Carter
"I fart in your general direction."
Monty Python & the Holy Grail
05



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 18:12     ` Jeffrey R. Carter
@ 2005-10-13 18:37       ` Stefan Bellon
  2005-10-13 21:21         ` Robert A Duff
  2005-10-16  0:13       ` Ray Blaak
  1 sibling, 1 reply; 37+ messages in thread
From: Stefan Bellon @ 2005-10-13 18:37 UTC (permalink / raw)


Jeffrey R. Carter wrote:

> Stefan Bellon wrote:
> 
> > What we are doing is this: We configure our project for the target
> > operating system and during that configuration, a small file with
> > content similar to the following is generated:
> > 
> > separate (Bauhaus.OS)
> > function Get_OS return String is
> > begin
> >    return "GNU/Linux";
> > end Get_OS;
> 
> Is there some reason you're using String rather than an enumerated
> type?

Nothing more than historical reasons. In some files where OS specific
code resides, we have something like the following on package level:

   Is_MSWindows : constant Boolean := Get_OS = "Windows";

We hope that GNAT can optimize enough to propagate the Boolean constant
and only include the active configuration in a case like

   if Is_MSWindows then
      Do_It_The_MSWindows_Way;
   else
      Do_It_The_UNIX_Way;
   end if;

But yes, making it an enumeration is a neater solution (done now).

-- 
Dipl.-Inf. Stefan Bellon
Bauhaus Software Technologies | TTI GmbH TGZ Softwareanalysen c/o ISTE
Tel.: +49 711 78 16 221       | Universitätsstraße 38
Fax.: +49 711 78 16 380       | 70569 Stuttgart



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  6:05 ` Martin Dowie
  2005-10-13  9:50   ` Stefan Bellon
  2005-10-13 18:11   ` Jeffrey R. Carter
@ 2005-10-13 19:44   ` Simon Wright
  2005-10-13 21:04   ` Michael Bode
  3 siblings, 0 replies; 37+ messages in thread
From: Simon Wright @ 2005-10-13 19:44 UTC (permalink / raw)


Martin Dowie <martin.dowie@btopenworld.com> writes:

> Roger Blum wrote:
>> Is there a way within Ada (GNAT 3.15p) to tell whether the
>> application is running on Linux or Windows (and the version of the
>> OS)?
>
> There is no single standard why... yet!
>
> I think that you will be able to do this with "package
> Ada.Environment_Variables" come Ada200Y, e.g.

We would like to distinguish the PowerPC VxWorks target from the Intel
Windows development environment!

At the moment we just test the endianness, not great but it works for
us.

One solution, similar to what we use in another context, is as
suggested by Stefan. We used a visible constant enumeration, eg

  type environment_kind is (host, target);
  environment : constant environment_kind := host;

and compiler path selection (via GNAT project file).

That way, GNAT (certainly at -O2) will generate no object code for the
'wrong' branch. OK, you get to recompile the world, but that was going
to happen anyway!



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  2:35 How to detect OS type and version? Roger Blum
  2005-10-13  6:05 ` Martin Dowie
  2005-10-13 12:06 ` Rob Norris
@ 2005-10-13 20:25 ` Bernd Specht
  2005-10-13 20:36   ` Michael Bode
  2005-10-14  6:30 ` Roger Blum
  3 siblings, 1 reply; 37+ messages in thread
From: Bernd Specht @ 2005-10-13 20:25 UTC (permalink / raw)


"Roger Blum" <rogerblum@hawaii.rr.com> wrote in
news:BJj3f.711$QM5.65@tornado.socal.rr.com: 

> Is there a way within Ada (GNAT 3.15p) to tell whether the application
> is running on Linux or Windows (and the version of the OS)?
> 
> Regards,
> Roger 
> 
> 

Determination of the os is relative simple, the os version of a linux also. 
Windows is a bit more complicated.

Use "Text_IO" and try "Open" the file "/proc/sys/kernel/osrelease". On a 
Windows system, you will get an exception (Name_Error ?), on a Linux system 
you can read the file which contains the kernel version. You can use 
"/proc/sys/kernel/version" to get the build (release?) date.

If you cannot determine a linux system as described above, then try to open 
a file that is typically for windows (e.g. "c:\winnt\system32\command.com"). 
If you get _no_ exception, then you are on a windows system. To find the 
version, look for the file "prodspec.ini" (in c:\winnt\system32 and in c:
\system32). You can find the Window type (2000, XP) and the build number. 
But - not on all Windows systems :-( (Win98 do not have this file).

Hope this helps.


Regards,
Bernd

PS: Don't forget to close the files ;-)







^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 20:25 ` Bernd Specht
@ 2005-10-13 20:36   ` Michael Bode
  2005-10-13 20:41     ` Bernd Specht
  0 siblings, 1 reply; 37+ messages in thread
From: Michael Bode @ 2005-10-13 20:36 UTC (permalink / raw)


Bernd.Specht@gmx.com (Bernd Specht) writes:

> If you cannot determine a linux system as described above, then try to open 
> a file that is typically for windows (e.g. "c:\winnt\system32\command.com"). 
> If you get _no_ exception, then you are on a windows system. To find the 
> version, look for the file "prodspec.ini" (in c:\winnt\system32 and in c:
> \system32). You can find the Window type (2000, XP) and the build number. 
> But - not on all Windows systems :-( (Win98 do not have this file).

It has not to be "c:\winnt". The correct path is "%Systemroot%" and
for that you must read the environment variable. 

-- 
Michael Bode



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 20:36   ` Michael Bode
@ 2005-10-13 20:41     ` Bernd Specht
  2005-10-13 20:58       ` Stefan Bellon
  0 siblings, 1 reply; 37+ messages in thread
From: Bernd Specht @ 2005-10-13 20:41 UTC (permalink / raw)


Michael Bode <m.g.bode@web.de> wrote in news:87u0flf93a.fsf@code-hal.de:

> Bernd.Specht@gmx.com (Bernd Specht) writes:
> 
>> If you cannot determine a linux system as described above, then try to
>> open a file that is typically for windows (e.g.
>> "c:\winnt\system32\command.com"). If you get _no_ exception, then you
>> are on a windows system. To find the version, look for the file
>> "prodspec.ini" (in c:\winnt\system32 and in c: \system32). You can
>> find the Window type (2000, XP) and the build number. But - not on all
>> Windows systems :-( (Win98 do not have this file). 
> 
> It has not to be "c:\winnt". The correct path is "%Systemroot%" and
> for that you must read the environment variable. 
> 

But as he doesn't know the os, he cannot read an environment variable. 
Personally I've never seen a Windows system on a different drive (older 
versions of Windows required C: which often gave problems with OS/2 on same 
drive). Beside this you can try C:, D: ...



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 20:41     ` Bernd Specht
@ 2005-10-13 20:58       ` Stefan Bellon
  2005-10-14  0:27         ` Randy Brukardt
  0 siblings, 1 reply; 37+ messages in thread
From: Stefan Bellon @ 2005-10-13 20:58 UTC (permalink / raw)


Bernd Specht wrote:

> Personally I've never seen a Windows system on a different drive
> (older versions of Windows required C: which often gave problems with
> OS/2 on same drive). Beside this you can try C:, D: ...

And I have never seen a Windows installed inside a directory called
"winnt".

-- 
Dipl.-Inf. Stefan Bellon
Bauhaus Software Technologies | TTI GmbH TGZ Softwareanalysen c/o ISTE
Tel.: +49 711 78 16 221       | Universitätsstraße 38
Fax.: +49 711 78 16 380       | 70569 Stuttgart



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  6:05 ` Martin Dowie
                     ` (2 preceding siblings ...)
  2005-10-13 19:44   ` Simon Wright
@ 2005-10-13 21:04   ` Michael Bode
  2005-10-14  1:33     ` Steve
  3 siblings, 1 reply; 37+ messages in thread
From: Michael Bode @ 2005-10-13 21:04 UTC (permalink / raw)


Martin Dowie <martin.dowie@btopenworld.com> writes:

> Roger Blum wrote:
>> Is there a way within Ada (GNAT 3.15p) to tell whether the
>> application is running on Linux or Windows (and the version of the
>> OS)?
>
> There is no single standard why... yet!
>
> I think that you will be able to do this with "package
> Ada.Environment_Variables" come Ada200Y, e.g.
>
> if Ada.Environment_Variables.Exists ("OS")
>     and Ada.Environment_Variables.Value ("OS") = "Windows_NT" then
>     ...
> elsif Ada.Environment_Variables.Exists ("OSTYPE")

He could use GNAT.Os_Lib.Getenv ("OSTYPE")

-- 
Michael Bode



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 18:37       ` Stefan Bellon
@ 2005-10-13 21:21         ` Robert A Duff
  2005-10-13 21:25           ` Stefan Bellon
  0 siblings, 1 reply; 37+ messages in thread
From: Robert A Duff @ 2005-10-13 21:21 UTC (permalink / raw)


Stefan Bellon <bellon@bauhaus-tec.com> writes:

> Nothing more than historical reasons. In some files where OS specific
> code resides, we have something like the following on package level:
> 
>    Is_MSWindows : constant Boolean := Get_OS = "Windows";
> 
> We hope that GNAT can optimize enough to propagate the Boolean constant
> and only include the active configuration in a case like
> 
>    if Is_MSWindows then
>       Do_It_The_MSWindows_Way;
>    else
>       Do_It_The_UNIX_Way;
>    end if;

Compilers will probably not be able to optimize this unless Get_OS is
inlined.  I believe GNAT refuses to inline functions that return String.

> But yes, making it an enumeration is a neater solution (done now).

... and add pragma Inline to the function.

By the way, a case statement would be better than an if statement,
because then you get the benefit of the full coverage rules.  "Not
windows" might imply "unix" today, but what if you add a third
possible value?

- Bob



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 21:21         ` Robert A Duff
@ 2005-10-13 21:25           ` Stefan Bellon
  2005-10-13 21:52             ` Robert A Duff
  0 siblings, 1 reply; 37+ messages in thread
From: Stefan Bellon @ 2005-10-13 21:25 UTC (permalink / raw)


Robert A Duff wrote:

> I believe GNAT refuses to inline functions that return String.

Oh, I didn't know that. Thanks for the hint.

-- 
Dipl.-Inf. Stefan Bellon
Bauhaus Software Technologies | TTI GmbH TGZ Softwareanalysen c/o ISTE
Tel.: +49 711 78 16 221       | Universitätsstraße 38
Fax.: +49 711 78 16 380       | 70569 Stuttgart



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 21:25           ` Stefan Bellon
@ 2005-10-13 21:52             ` Robert A Duff
  0 siblings, 0 replies; 37+ messages in thread
From: Robert A Duff @ 2005-10-13 21:52 UTC (permalink / raw)


Stefan Bellon <bellon@bauhaus-tec.com> writes:

> Robert A Duff wrote:
> 
> > I believe GNAT refuses to inline functions that return String.
> 
> Oh, I didn't know that. Thanks for the hint.

Well, don't believe me -- look at the generated code to make sure.
And/or, turn on the option that warns about ignored pragmas Inline.

- Bob



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 11:59   ` Martin Dowie
@ 2005-10-14  0:21     ` Randy Brukardt
  2005-10-14  8:02       ` Martin Dowie
  2005-10-14  9:52     ` Rob Norris
  1 sibling, 1 reply; 37+ messages in thread
From: Randy Brukardt @ 2005-10-14  0:21 UTC (permalink / raw)


"Martin Dowie" <martin.dowie@baesystems.com> wrote in message
news:434e4a25_1@glkas0286.greenlnk.net...
> That way doesn't give the version of the OS.

But once you know that you're running on Windows, you can arrange to call
the Win32 Get_Version function to learn all of the details. (You'd have to
do that with a dynamic call to avoid statically linking part of the Windows
runtime.) I presume something similar is available in the Linux API.

                          Randy.






^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 20:58       ` Stefan Bellon
@ 2005-10-14  0:27         ` Randy Brukardt
  2005-10-15 19:48           ` Bernd Specht
  0 siblings, 1 reply; 37+ messages in thread
From: Randy Brukardt @ 2005-10-14  0:27 UTC (permalink / raw)


"Stefan Bellon" <bellon@bauhaus-tec.com> wrote in message
news:20051013225800.4e50bfa0@pscube.informatik.uni-stuttgart.de...
Bernd Specht wrote:

>>But as he doesn't know the os, he cannot read an environment variable.

Why not? Use Ada.Environment_Variables (Ada 200Y) or the
implementation-defined version that comes with your compiler. (Pretty much
every Ada compiler I know of has one.)

>> Personally I've never seen a Windows system on a different drive
>> (older versions of Windows required C: which often gave problems with
>> OS/2 on same drive). Beside this you can try C:, D: ...

>And I have never seen a Windows installed inside a directory called
>"winnt".

That was the standard for Windows NT and at least some Windows 2000 systems.
All of the systems here are that way. (OTOH, none of them are on drive C:)

As I said before, once you figure out that you are running on Windows (some
version), then dynamically load the GetVersionEx API function and get all of
the version details.

                            Randy.








^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 21:04   ` Michael Bode
@ 2005-10-14  1:33     ` Steve
  0 siblings, 0 replies; 37+ messages in thread
From: Steve @ 2005-10-14  1:33 UTC (permalink / raw)


"Michael Bode" <m.g.bode@web.de> wrote in message 
news:87mzldf7rm.fsf@code-hal.de...
> Martin Dowie <martin.dowie@btopenworld.com> writes:
>
>> Roger Blum wrote:
>>> Is there a way within Ada (GNAT 3.15p) to tell whether the
>>> application is running on Linux or Windows (and the version of the
>>> OS)?
>>
>> There is no single standard why... yet!
>>
>> I think that you will be able to do this with "package
>> Ada.Environment_Variables" come Ada200Y, e.g.
>>
>> if Ada.Environment_Variables.Exists ("OS")
>>     and Ada.Environment_Variables.Value ("OS") = "Windows_NT" then
>>     ...
>> elsif Ada.Environment_Variables.Exists ("OSTYPE")
>
> He could use GNAT.Os_Lib.Getenv ("OSTYPE")
>
> -- 
> Michael Bode

In the implementation of GNAT.OS_Lib there is an interesting definition:

   On_Windows : constant Boolean := Directory_Separator = '\';

Apparently that's how GNAT finds the OS.

Once you have the OS, you can use OS specific code to find the version.

Steve
(The Duck) 





^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13  2:35 How to detect OS type and version? Roger Blum
                   ` (2 preceding siblings ...)
  2005-10-13 20:25 ` Bernd Specht
@ 2005-10-14  6:30 ` Roger Blum
  3 siblings, 0 replies; 37+ messages in thread
From: Roger Blum @ 2005-10-14  6:30 UTC (permalink / raw)


Thanks a lot for all your information and ideas. I think, with that I will 
find an acceptable solution!

Regards,
Roger

"Roger Blum" <rogerblum@hawaii.rr.com> schrieb im Newsbeitrag 
news:BJj3f.711$QM5.65@tornado.socal.rr.com...
> Is there a way within Ada (GNAT 3.15p) to tell whether the application is 
> running on Linux or Windows (and the version of the OS)?
>
> Regards,
> Roger
> 





^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-14  0:21     ` Randy Brukardt
@ 2005-10-14  8:02       ` Martin Dowie
  2005-10-14 23:27         ` Randy Brukardt
  0 siblings, 1 reply; 37+ messages in thread
From: Martin Dowie @ 2005-10-14  8:02 UTC (permalink / raw)


Randy Brukardt wrote:
> "Martin Dowie" <martin.dowie@baesystems.com> wrote in message
> news:434e4a25_1@glkas0286.greenlnk.net...
>> That way doesn't give the version of the OS.
>
> But once you know that you're running on Windows, you can arrange to
> call the Win32 Get_Version function to learn all of the details.
> (You'd have to do that with a dynamic call to avoid statically
> linking part of the Windows runtime.) I presume something similar is
> available in the Linux API.

Yep - and (GMGPL) Claw can provide this! :-)

Quick question for you Randy - was the original intent that "System.Name"
provide this?

Cheers

-- Martin





^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 11:59   ` Martin Dowie
  2005-10-14  0:21     ` Randy Brukardt
@ 2005-10-14  9:52     ` Rob Norris
  1 sibling, 0 replies; 37+ messages in thread
From: Rob Norris @ 2005-10-14  9:52 UTC (permalink / raw)


On Thu, 13 Oct 2005 12:59:16 +0100, "Martin Dowie"
<martin.dowie@baesystems.com> wrote:

>That way doesn't give the version of the OS.
>

I tried to imply that with "Not very exact, but may be useful"...

Once you know the system is Unix like (including VMS??) they should
have the 'uname -a' shell command which could be called by some Ada
function that wraps around C pragma system call in stdlib.h.

For Windows systems the command is 'ver'.

Something like:


with Interfaces.C.Strings;
with Ada.Text_IO;

--
--
function C_System (Command : in Interfaces.C.Strings.chars_ptr) 
	return Integer;
pragma Import (C, C_System, "system");

--
--
function Shell_Command (Command : in String) return Integer is
 C_Command : Interfaces.C.Strings.chars_ptr :=
	Interfaces.C.Strings.New_String (Command);
begin
 return C_System (C_Command);
end Shell_Command;


type os_type is (Unix, Windows, Unknown);

--
--
function What_OS return os_type is
begin
  if gnat.os_lib.directory_separator = '/' then
    if shell_Command ("uname -a") = -1 then
	Ada.Text_IO.Put_Line ("OS version inspection failed");
    end if;	
    return Unix;
  elsif gnat.os_lib.directory_separator = '\' then
    if shell_Command ("ver") = -1 then
	Ada.Text_IO.Put_Line ("OS version inspection failed");
    end if;	
    return Windows;
  else
    return Unknown;
  end if;
end What_OS;


procedure main is
  Ada.Text_IO.OS_Type'Image (what_os);
end main;




^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-14  8:02       ` Martin Dowie
@ 2005-10-14 23:27         ` Randy Brukardt
  2005-10-15 12:28           ` Larry Kilgallen
  0 siblings, 1 reply; 37+ messages in thread
From: Randy Brukardt @ 2005-10-14 23:27 UTC (permalink / raw)


"Martin Dowie" <martin.dowie@baesystems.com> wrote in message
news:434f643e$1_1@glkas0286.greenlnk.net...
...
> Quick question for you Randy - was the original intent that "System.Name"
> provide this?

I think so. But it's an enumeration type, and adding values everytime a new
version of an OS becomes available is annoying, so there never was much
support for this.

Ada 83 compounded the problem by implying that users could simply use a
pragma System_Name to change the target. The net effect was that nobody
provided more than one target in the type System.Name.

For Janus/Ada, we used to provide a number of literals, but somewhere along
the line that got dropped, perhaps for the reason above. The name of the
literal is a bit helpful ("Win32" or "Unix"), but hardly portable in any
way.

                      Randy.






^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-14 23:27         ` Randy Brukardt
@ 2005-10-15 12:28           ` Larry Kilgallen
  2005-10-15 14:12             ` Martin Dowie
  0 siblings, 1 reply; 37+ messages in thread
From: Larry Kilgallen @ 2005-10-15 12:28 UTC (permalink / raw)


In article <Td-dnezkj9VKoM3eRVn-uA@megapath.net>, "Randy Brukardt" <randy@rrsoftware.com> writes:

> Ada 83 compounded the problem by implying that users could simply use a
> pragma System_Name to change the target.

If it describes the target, certainly that must be under the
control of the user, since compilation might be in a different
environment from execution.



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-15 12:28           ` Larry Kilgallen
@ 2005-10-15 14:12             ` Martin Dowie
  0 siblings, 0 replies; 37+ messages in thread
From: Martin Dowie @ 2005-10-15 14:12 UTC (permalink / raw)


Larry Kilgallen wrote:
> In article <Td-dnezkj9VKoM3eRVn-uA@megapath.net>, "Randy Brukardt" <randy@rrsoftware.com> writes:
> 
> 
>>Ada 83 compounded the problem by implying that users could simply use a
>>pragma System_Name to change the target.
> 
> 
> If it describes the target, certainly that must be under the
> control of the user, since compilation might be in a different
> environment from execution.

If you are cross compiling then the version of package System for the 
target would have a System.Name to reflect the target.

I'm not sure why this is a problem for any 'closed' compiler, as the 
vendor must know the exact target (OS + processor + switches).

I can understand there is a problem for 'open' compilers, as anyone can 
build for any OS+processor+switches and make the system name anything - 
even erroneous!

Cheers

-- Martin



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-14  0:27         ` Randy Brukardt
@ 2005-10-15 19:48           ` Bernd Specht
  0 siblings, 0 replies; 37+ messages in thread
From: Bernd Specht @ 2005-10-15 19:48 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> wrote in
news:keWdnbiXWJM4Z9PeRVn-rg@megapath.net: 

> "Stefan Bellon" <bellon@bauhaus-tec.com> wrote in message
> news:20051013225800.4e50bfa0@pscube.informatik.uni-stuttgart.de...
> Bernd Specht wrote:
> As I said before, once you figure out that you are running on Windows
> (some version), then dynamically load the GetVersionEx API function and
> get all of the version details.
> 
>                             Randy.

Is loading os-specific api function possible in a system independent way? 
How, I would like to know.

Bernd



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-13 18:12     ` Jeffrey R. Carter
  2005-10-13 18:37       ` Stefan Bellon
@ 2005-10-16  0:13       ` Ray Blaak
  2005-10-16  0:29         ` Robert A Duff
  1 sibling, 1 reply; 37+ messages in thread
From: Ray Blaak @ 2005-10-16  0:13 UTC (permalink / raw)


"Jeffrey R. Carter" <spam@spam.com> writes:

> Stefan Bellon wrote:
> >    return "GNU/Linux";
> 
> Is there some reason you're using String rather than an enumerated type?

I submit that an enumerated type is the wrong thing to do. Operating systems
are not fixed, can have many sub-variants. (WinOS, Win2000, WinNT, etc.).

Simply adding a new OS to some central definition should not impact all
existing code. In practical terms people should have had an "unknown OS"
handler anyway.

It's akin to having some app that processes first names as enumerated values
instead of strings. It's a mistake because the set of values are open ended
and are not known in advance, and can change depending on where you execute.

-- 
Cheers,                                        The Rhythm is around me,
                                               The Rhythm has control.
Ray Blaak                                      The Rhythm is inside me,
rAYblaaK@STRIPCAPStelus.net                    The Rhythm has my soul.



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-16  0:13       ` Ray Blaak
@ 2005-10-16  0:29         ` Robert A Duff
  2005-10-16  4:48           ` Ray Blaak
  0 siblings, 1 reply; 37+ messages in thread
From: Robert A Duff @ 2005-10-16  0:29 UTC (permalink / raw)


Ray Blaak <rAYblaaK@STRIPCAPStelus.net> writes:

> "Jeffrey R. Carter" <spam@spam.com> writes:
> 
> > Stefan Bellon wrote:
> > >    return "GNU/Linux";
> > 
> > Is there some reason you're using String rather than an enumerated type?
> 
> I submit that an enumerated type is the wrong thing to do. Operating systems
> are not fixed, can have many sub-variants. (WinOS, Win2000, WinNT, etc.).

Well, maybe, but...

> Simply adding a new OS to some central definition should not impact all
> existing code. In practical terms people should have had an "unknown OS"
> handler anyway.

What is the directory separator character on "unknown OS"?  ;-)

The advantage of using an enumeration type, and _not_ having an
Unknown_OS value, is that the compiler will tell you about all
the places you might need to change when you add a new OS.
That might or might not be what you want.

In my current project, I have two levels of distinction -- OS (Unix
vs. Windows, for example) and OS_Flavor (Solaris vs. Linux, for
example).  I've not needed to distinguish (for example) different
versions of Linux, and it's probably a good idea to avoid that fine
level of distinction if possible.

> It's akin to having some app that processes first names as enumerated values
> instead of strings. It's a mistake because the set of values are open ended
> and are not known in advance, and can change depending on where you execute.

You probably don't port to a new OS quite as often as you add names to
your database of telephone numbers or whatever.  And you probably don't
use wildly different code based on first names, which needs updating
every time you add a new first name.

- Bob



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-16  0:29         ` Robert A Duff
@ 2005-10-16  4:48           ` Ray Blaak
  2005-10-16 14:16             ` Larry Kilgallen
  2005-10-16 15:59             ` Robert A Duff
  0 siblings, 2 replies; 37+ messages in thread
From: Ray Blaak @ 2005-10-16  4:48 UTC (permalink / raw)


Robert A Duff <bobduff@shell01.TheWorld.com> writes:
> Ray Blaak <rAYblaaK@STRIPCAPStelus.net> writes:
> > Simply adding a new OS to some central definition should not impact all
> > existing code. In practical terms people should have had an "unknown OS"
> > handler anyway.
> 
> What is the directory separator character on "unknown OS"?  ;-)

I don't know, of course. The point is to have a plan for it. In the worst case
you exit with the appropriate error message "unknown OS!". In practice you
assume "Unix".

> The advantage of using an enumeration type, and _not_ having an
> Unknown_OS value, is that the compiler will tell you about all
> the places you might need to change when you add a new OS.
> That might or might not be what you want.

That is the inherent advantage of using an enumerated type, of course.

My point is that the nature of the information does not correspond to a known
fixed set.

> In my current project, I have two levels of distinction -- OS (Unix
> vs. Windows, for example) and OS_Flavor (Solaris vs. Linux, for
> example).  I've not needed to distinguish (for example) different
> versions of Linux, and it's probably a good idea to avoid that fine
> level of distinction if possible.

That you need only a few levels of distinction is a property of your current
project, not a property of the set of known OS types. That should be separated.

What I would do in your case is to make my own project-specific enumerated
type and decide the appropriate current literal value at startup based on the
the string-based values returned from querying the OS.

If the reported OS value cannot be correlated to one of your assumed OS types,
then you must decide what to do, which is likely to either assume it's a
default known one (e.g. some sort of "generic" Unix) or else bail out.

> > It's akin to having some app that processes first names as enumerated values
> > instead of strings. It's a mistake because the set of values are open ended
> > and are not known in advance, and can change depending on where you execute.
> 
> You probably don't port to a new OS quite as often as you add names to
> your database of telephone numbers or whatever.  And you probably don't
> use wildly different code based on first names, which needs updating
> every time you add a new first name.

That is essentially true in practice, I admit.

I guess what I care about is that the built in OS query that would live
somewhere under the System or Ada hierarchy would not return an enumerated
type, but rather a string.

That is, the enumeration should not be built into the language, since it is
not at all a stable enough set of values for the long term. Imagine being able
to code to an updated set only every 10 years, which seems to be Ada's upgrade
cycle.

-- 
Cheers,                                        The Rhythm is around me,
                                               The Rhythm has control.
Ray Blaak                                      The Rhythm is inside me,
rAYblaaK@STRIPCAPStelus.net                    The Rhythm has my soul.



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-16  4:48           ` Ray Blaak
@ 2005-10-16 14:16             ` Larry Kilgallen
  2005-10-16 15:55               ` Robert A Duff
  2005-10-16 20:06               ` Pascal Obry
  2005-10-16 15:59             ` Robert A Duff
  1 sibling, 2 replies; 37+ messages in thread
From: Larry Kilgallen @ 2005-10-16 14:16 UTC (permalink / raw)


In article <ufyr2dqf1.fsf@STRIPCAPStelus.net>, Ray Blaak <rAYblaaK@STRIPCAPStelus.net> writes:
> Robert A Duff <bobduff@shell01.TheWorld.com> writes:
>> Ray Blaak <rAYblaaK@STRIPCAPStelus.net> writes:
>> > Simply adding a new OS to some central definition should not impact all
>> > existing code. In practical terms people should have had an "unknown OS"
>> > handler anyway.
>> 
>> What is the directory separator character on "unknown OS"?  ;-)
> 
> I don't know, of course. The point is to have a plan for it. In the worst
> case you exit with the appropriate error message "unknown OS!". In practice
> you assume "Unix".

I got to work on some Ada code that used this "directory separator"
approach.  Perhaps it is ok if your only choices are Windows or Unix,
but on VMS the characters ":[", "." and "]" are all used, except when
that list is ":<", "." and ">".  Needless to say, the "directory separator"
approach was totally inadequate.



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-16 14:16             ` Larry Kilgallen
@ 2005-10-16 15:55               ` Robert A Duff
  2005-10-16 20:06               ` Pascal Obry
  1 sibling, 0 replies; 37+ messages in thread
From: Robert A Duff @ 2005-10-16 15:55 UTC (permalink / raw)


Kilgallen@SpamCop.net (Larry Kilgallen) writes:

> In article <ufyr2dqf1.fsf@STRIPCAPStelus.net>, Ray Blaak <rAYblaaK@STRIPCAPStelus.net> writes:
> > Robert A Duff <bobduff@shell01.TheWorld.com> writes:
> >> Ray Blaak <rAYblaaK@STRIPCAPStelus.net> writes:
> >> > Simply adding a new OS to some central definition should not impact all
> >> > existing code. In practical terms people should have had an "unknown OS"
> >> > handler anyway.
> >> 
> >> What is the directory separator character on "unknown OS"?  ;-)
> > 
> > I don't know, of course. The point is to have a plan for it. In the worst
> > case you exit with the appropriate error message "unknown OS!". In practice
> > you assume "Unix".
> 
> I got to work on some Ada code that used this "directory separator"
> approach.  Perhaps it is ok if your only choices are Windows or Unix,
> but on VMS the characters ":[", "." and "]" are all used, except when
> that list is ":<", "." and ">".  Needless to say, the "directory separator"
> approach was totally inadequate.

Yes, I agree.  What you want is a set of operations for pulling apart
pathnames and putting them together in various ways.  Even that won't
work on some systems, but I suspect it's good enough in practise.

Ada 2005 has something along those lines, but I haven't looked at
it carefully.

So perhaps I should have said, "What is the algorithm for parsing file
names on `unknown OS'?"  ;-)

- Bob



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-16  4:48           ` Ray Blaak
  2005-10-16 14:16             ` Larry Kilgallen
@ 2005-10-16 15:59             ` Robert A Duff
  2005-10-17 17:19               ` Ray Blaak
  1 sibling, 1 reply; 37+ messages in thread
From: Robert A Duff @ 2005-10-16 15:59 UTC (permalink / raw)


Ray Blaak <rAYblaaK@STRIPCAPStelus.net> writes:

> What I would do in your case is to make my own project-specific enumerated
> type and decide the appropriate current literal value at startup based on the
> the string-based values returned from querying the OS.

In my case, we have an enumeration type, and we compile in the value.
(That is, we have a package spec with three versions, and the build
scripts pick the right one.)  There's no need to query anything at run
time (in my project).

> I guess what I care about is that the built in OS query that would live
> somewhere under the System or Ada hierarchy would not return an enumerated
> type, but rather a string.
> 
> That is, the enumeration should not be built into the language, since
> it is not at all a stable enough set of values for the long
> term. Imagine being able to code to an updated set only every 10
> years, which seems to be Ada's upgrade cycle.

I agree that an enum type would not work here, as a language feature.
But neither would a string or anything else -- there is no solution
at the language level.  This issue has to be solved by each project,
I think.

- Bob



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-16 14:16             ` Larry Kilgallen
  2005-10-16 15:55               ` Robert A Duff
@ 2005-10-16 20:06               ` Pascal Obry
  2005-10-18 16:58                 ` Ray Blaak
  1 sibling, 1 reply; 37+ messages in thread
From: Pascal Obry @ 2005-10-16 20:06 UTC (permalink / raw)
  To: Larry Kilgallen

Larry Kilgallen a �crit :
> I got to work on some Ada code that used this "directory separator"
> approach.  Perhaps it is ok if your only choices are Windows or Unix,
> but on VMS the characters ":[", "." and "]" are all used, except when
> that list is ":<", "." and ">".  Needless to say, the "directory separator"
> approach was totally inadequate.

Agreed, all this is a configuration management problem. It is far better
in my view to have some kind of script that configure the software
before compilation. This script will check the OS for example and use
the proper bodies for some of the OS specific packages. This is exactly
was the ./configure & make does on most software. I think it is wrong to
try doing that with the language itself. As pointed out there it can
work in some very simple case but it won't generally.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-16 15:59             ` Robert A Duff
@ 2005-10-17 17:19               ` Ray Blaak
  0 siblings, 0 replies; 37+ messages in thread
From: Ray Blaak @ 2005-10-17 17:19 UTC (permalink / raw)


Robert A Duff <bobduff@shell01.TheWorld.com> writes:
> Ray Blaak <rAYblaaK@STRIPCAPStelus.net> writes:
> > That is, the enumeration should not be built into the language, since
> > it is not at all a stable enough set of values for the long
> > term. Imagine being able to code to an updated set only every 10
> > years, which seems to be Ada's upgrade cycle.
> 
> I agree that an enum type would not work here, as a language feature.
> But neither would a string or anything else -- there is no solution
> at the language level.  This issue has to be solved by each project,
> I think.

I don't see the problem with a string value. I use: 

  System.getProperty("os.name")

in Java all the time, and it works well in practice, at least to distinguish
between the OS's that I care about.

-- 
Cheers,                                        The Rhythm is around me,
                                               The Rhythm has control.
Ray Blaak                                      The Rhythm is inside me,
rAYblaaK@STRIPCAPStelus.net                    The Rhythm has my soul.



^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: How to detect OS type and version?
  2005-10-16 20:06               ` Pascal Obry
@ 2005-10-18 16:58                 ` Ray Blaak
  0 siblings, 0 replies; 37+ messages in thread
From: Ray Blaak @ 2005-10-18 16:58 UTC (permalink / raw)


Pascal Obry <pascal@obry.net> writes:
> Agreed, all this is a configuration management problem. It is far better
> in my view to have some kind of script that configure the software
> before compilation. This script will check the OS for example and use
> the proper bodies for some of the OS specific packages. This is exactly
> was the ./configure & make does on most software. I think it is wrong to
> try doing that with the language itself. As pointed out there it can
> work in some very simple case but it won't generally.

In languages like Ada that tend to need to be separately compiled for the
target OS, this is reasonable.

In languages that can portably run "anywhere", like Java in particular, some
sort of runtime query is needed, since the code is built once but executed in
multiple environments.

And then there is the case of ports of Ada to the JVM, so that Ada apps can in
fact run in multiple environments as well.

-- 
Cheers,                                        The Rhythm is around me,
                                               The Rhythm has control.
Ray Blaak                                      The Rhythm is inside me,
rAYblaaK@STRIPCAPStelus.net                    The Rhythm has my soul.



^ permalink raw reply	[flat|nested] 37+ messages in thread

end of thread, other threads:[~2005-10-18 16:58 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-13  2:35 How to detect OS type and version? Roger Blum
2005-10-13  6:05 ` Martin Dowie
2005-10-13  9:50   ` Stefan Bellon
2005-10-13 10:39     ` Martin Dowie
2005-10-13 18:12     ` Jeffrey R. Carter
2005-10-13 18:37       ` Stefan Bellon
2005-10-13 21:21         ` Robert A Duff
2005-10-13 21:25           ` Stefan Bellon
2005-10-13 21:52             ` Robert A Duff
2005-10-16  0:13       ` Ray Blaak
2005-10-16  0:29         ` Robert A Duff
2005-10-16  4:48           ` Ray Blaak
2005-10-16 14:16             ` Larry Kilgallen
2005-10-16 15:55               ` Robert A Duff
2005-10-16 20:06               ` Pascal Obry
2005-10-18 16:58                 ` Ray Blaak
2005-10-16 15:59             ` Robert A Duff
2005-10-17 17:19               ` Ray Blaak
2005-10-13 18:11   ` Jeffrey R. Carter
2005-10-13 19:44   ` Simon Wright
2005-10-13 21:04   ` Michael Bode
2005-10-14  1:33     ` Steve
2005-10-13 12:06 ` Rob Norris
2005-10-13 11:59   ` Martin Dowie
2005-10-14  0:21     ` Randy Brukardt
2005-10-14  8:02       ` Martin Dowie
2005-10-14 23:27         ` Randy Brukardt
2005-10-15 12:28           ` Larry Kilgallen
2005-10-15 14:12             ` Martin Dowie
2005-10-14  9:52     ` Rob Norris
2005-10-13 20:25 ` Bernd Specht
2005-10-13 20:36   ` Michael Bode
2005-10-13 20:41     ` Bernd Specht
2005-10-13 20:58       ` Stefan Bellon
2005-10-14  0:27         ` Randy Brukardt
2005-10-15 19:48           ` Bernd Specht
2005-10-14  6:30 ` Roger Blum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox