By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,232 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,232 IT Pros & Developers. It's quick & easy.

determine operating system

P: n/a
Hi,
Is there a way to determine the name and version of the
operating system in a portable way? (for Solaris/Linux)
Thanks,

Kevin
Jul 22 '05 #1
Share this Question
Share on Google+
27 Replies


P: n/a
Kevin A wrote:

Hi,
Is there a way to determine the name and version of the
operating system in a portable way? (for Solaris/Linux)

Thanks,

Kevin


Short answer: no

Long answer: no
Jul 22 '05 #2

P: n/a
Julie <ju***@nospam.com> wrote in news:40***************@nospam.com:
Kevin A wrote:

Hi,
Is there a way to determine the name and version of the
operating system in a portable way? (for Solaris/Linux)

Thanks,

Kevin


Short answer: no

Long answer: no


Right answer: it depends

See http://predef.sourceforge.net/

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #3

P: n/a
bartek wrote:

Julie <ju***@nospam.com> wrote in news:40***************@nospam.com:
Kevin A wrote:

Hi,
Is there a way to determine the name and version of the
operating system in a portable way? (for Solaris/Linux)

Thanks,

Kevin


Short answer: no

Long answer: no


Right answer: it depends

See http://predef.sourceforge.net/

--
:: bartekd [at] o2 [dot] pl


On the contrary (and contrary to what is said in the link), those compiler
defines tell nothing of the actual operating system (or version), but at most,
tell the intended target operating system.

Right answer: no

If the OP wants a better answer, then they need to clarify as to specifically
what they are after as their post leaves a *lot* of room for
(mis)interpretation.
Jul 22 '05 #4

P: n/a
Julie <ju***@nospam.com> wrote in news:40***************@nospam.com:
bartek wrote:

Julie <ju***@nospam.com> wrote in news:40***************@nospam.com:
> Kevin A wrote:
>>
>> Hi,
>> Is there a way to determine the name and version of the
>> operating system in a portable way? (for Solaris/Linux)
>>
>> Thanks,
>>
>> Kevin
>
> Short answer: no
>
> Long answer: no


Right answer: it depends

See http://predef.sourceforge.net/

--
:: bartekd [at] o2 [dot] pl


On the contrary (and contrary to what is said in the link), those
compiler defines tell nothing of the actual operating system (or
version), but at most, tell the intended target operating system.

Right answer: no

If the OP wants a better answer, then they need to clarify as to
specifically what they are after as their post leaves a *lot* of room
for (mis)interpretation.


Therefore the good answer (from the OP's pov) is dependent on the OP's
particular requirements, i.e. what degree of portability is important for
the OP. The original message provides a clear indication that it's not
about absolute portability, in which case the strict answer 'no' is
misleading, at least. Of course, I'm not saying it isn't informative.

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #5

P: n/a
Kevin A wrote:

Hi,
Is there a way to determine the name and version of the
operating system in a portable way? (for Solaris/Linux)

Thanks,

Kevin


have you checked the man page for uname?
uname -a: returns all avaialble info for the given machine/OS.

Take a look at uname.
--
#----------------------------------------------------------#
# Penguinix Consulting #
#----------------------------------------------------------#
# Software development, QA and testing. #
# Linux support and training. #
# "Don't fear the penguin!" #
#----------------------------------------------------------#
# Registered Linux user: #309247 http://counter.li.org #
#----------------------------------------------------------#
Jul 22 '05 #6

P: n/a
Joe Cipale wrote:

Kevin A wrote:

Hi,
Is there a way to determine the name and version of the
operating system in a portable way? (for Solaris/Linux)

Thanks,

Kevin


have you checked the man page for uname?
uname -a: returns all avaialble info for the given machine/OS.

Take a look at uname.


Not portable, nor standard C++.
Jul 22 '05 #7

P: n/a
> If the OP wants a better answer, then they need to clarify as to
specifically
what they are after as their post leaves a *lot* of room for
(mis)interpretation.
We are working on a project for school (university). We work on a server
that
runs Solaris, but some of us also work on our project at home using their
Linux.

For now I have solved this problem with a (ugly) temporary solution:

QString OS::getOperatingSystem() {
return "Solaris 8";
}

But under Linux this, of course, gives the wrong result.

As you can see (from the use of QString), we use Qt. Maybe Qt has a way to
get
the name and version of the OS. But I can not find anything yet.
On the contrary (and contrary to what is said in the link), those compiler
defines tell nothing of the actual operating system (or version), but at most, tell the intended target operating system.


We compile on the same platform we run the program, so this IS a possible
solution. But if possible I would like a more elegant solution than using
macro's.
Kevin
Jul 22 '05 #8

P: n/a
Joe Cipale wrote:

Kevin A wrote:

Hi,
Is there a way to determine the name and version of the
operating system in a portable way? (for Solaris/Linux)

Thanks,

Kevin


have you checked the man page for uname?
uname -a: returns all avaialble info for the given machine/OS.

Take a look at uname.
--


Second that, but type

man 2 uname

Says it's POSIX.1. All reasonably modern Unices should be covered (SVr4
for sure), Linux too.

Certainly not C++, but while we are [OT] at it, it's about as portable
as it gets.

:-)

Denis
Jul 22 '05 #9

P: n/a
Kevin A wrote:
If the OP wants a better answer, then they need to clarify as to


specifically
what they are after as their post leaves a *lot* of room for
(mis)interpretation.

We are working on a project for school (university). We work on a server
that
runs Solaris, but some of us also work on our project at home using their
Linux.

For now I have solved this problem with a (ugly) temporary solution:

QString OS::getOperatingSystem() {
return "Solaris 8";
}

But under Linux this, of course, gives the wrong result.

As you can see (from the use of QString), we use Qt. Maybe Qt has a way to
get
the name and version of the OS. But I can not find anything yet.

On the contrary (and contrary to what is said in the link), those compiler
defines tell nothing of the actual operating system (or version), but at


most,
tell the intended target operating system.

We compile on the same platform we run the program, so this IS a possible
solution. But if possible I would like a more elegant solution than using
macro's.
Kevin


More elegant solution????

Commercial code like Qt uses a lot of macros for system dependent things.

They handle compatibility with autoconf automake, which generates the
appropriate macro definitions and makefiles for compiling in your system.

#ifdef linux
or #ifdef Linux

should just work, specially when all you have to comply is with Linux
and Solaris compatibility.

another thing you can try is using make rules.

Have your makefile include a golbal rules file

localrules.$(OSNAME)

eg

localrules.Linux
localrules.Solaris

You can use 'uname' to use the rules that apply to the system you are
compiling on, and maybe define compile time variables -DLIN_COMP_VAR.

Again, this is one of the things macros are good for. Don't be afraid
to use them....

Jorge L.
Jul 22 '05 #10

P: n/a
Julie wrote:

Joe Cipale wrote:

Kevin A wrote:

Hi,
Is there a way to determine the name and version of the
operating system in a portable way? (for Solaris/Linux)

Thanks,

Kevin


have you checked the man page for uname?
uname -a: returns all avaialble info for the given machine/OS.

Take a look at uname.


Not portable, nor standard C++.


Funny...

works the same on my BSD/Solaris/Linux machines (just a small sample)...
~sheesh~

--
#----------------------------------------------------------#
# Penguinix Consulting #
#----------------------------------------------------------#
# Software development, QA and testing. #
# Linux support and training. #
# "Don't fear the penguin!" #
#----------------------------------------------------------#
# Registered Linux user: #309247 http://counter.li.org #
#----------------------------------------------------------#
Jul 22 '05 #11

P: n/a
Joe Cipale wrote:

Julie wrote:

Joe Cipale wrote:

Kevin A wrote:
>
> Hi,
> Is there a way to determine the name and version of the
> operating system in a portable way? (for Solaris/Linux)
>
> Thanks,
>
> Kevin

have you checked the man page for uname?
uname -a: returns all avaialble info for the given machine/OS.

Take a look at uname.


Not portable, nor standard C++.


Funny...

works the same on my BSD/Solaris/Linux machines (just a small sample)...
~sheesh~


So is your point that it is standard C++ and/or portable?
Jul 22 '05 #12

P: n/a
Julie wrote:

Joe Cipale wrote:

Julie wrote:

Joe Cipale wrote:
>
> Kevin A wrote:
> >
> > Hi,
> > Is there a way to determine the name and version of the
> > operating system in a portable way? (for Solaris/Linux)
> >
> > Thanks,
> >
> > Kevin
>
> have you checked the man page for uname?
> uname -a: returns all avaialble info for the given machine/OS.
>
> Take a look at uname.

Not portable, nor standard C++.


Funny...

works the same on my BSD/Solaris/Linux machines (just a small sample)...
~sheesh~


So is your point that it is standard C++ and/or portable?


it is portable...
--
#----------------------------------------------------------#
# Penguinix Consulting #
#----------------------------------------------------------#
# Software development, QA and testing. #
# Linux support and training. #
# "Don't fear the penguin!" #
#----------------------------------------------------------#
# Registered Linux user: #309247 http://counter.li.org #
#----------------------------------------------------------#
Jul 22 '05 #13

P: n/a
Joe Cipale wrote:

Julie wrote:

Joe Cipale wrote:

Julie wrote:
>
> Joe Cipale wrote:
> >
> > Kevin A wrote:
> > >
> > > Hi,
> > > Is there a way to determine the name and version of the
> > > operating system in a portable way? (for Solaris/Linux)
> > >
> > > Thanks,
> > >
> > > Kevin
> >
> > have you checked the man page for uname?
> > uname -a: returns all avaialble info for the given machine/OS.
> >
> > Take a look at uname.
>
> Not portable, nor standard C++.

Funny...

works the same on my BSD/Solaris/Linux machines (just a small sample)...
~sheesh~


So is your point that it is standard C++ and/or portable?


it is portable...


Not in the std C++ sense.
Jul 22 '05 #14

P: n/a
> #ifdef linux
or #ifdef Linux

should just work, specially when all you have to comply is with Linux
and Solaris compatibility.
I just found a reason why this is not the solution I'm looking for.
I'd like to have the entire name of the OS, something like
"Suse Linux" and its version number.
But the macro's can only answer: "is it Linux, yes or no?"
Kevin
"Jorge Rivera" <jo*****@rochester.rr.com> schreef in bericht
news:gQ********************@twister.nyroc.rr.com.. . Kevin A wrote:
If the OP wants a better answer, then they need to clarify as to


specifically
what they are after as their post leaves a *lot* of room for
(mis)interpretation.

We are working on a project for school (university). We work on a server
that
runs Solaris, but some of us also work on our project at home using their Linux.

For now I have solved this problem with a (ugly) temporary solution:

QString OS::getOperatingSystem() {
return "Solaris 8";
}

But under Linux this, of course, gives the wrong result.

As you can see (from the use of QString), we use Qt. Maybe Qt has a way to get
the name and version of the OS. But I can not find anything yet.

On the contrary (and contrary to what is said in the link), those compilerdefines tell nothing of the actual operating system (or version), but at


most,
tell the intended target operating system.

We compile on the same platform we run the program, so this IS a possible solution. But if possible I would like a more elegant solution than using macro's.
Kevin


More elegant solution????

Commercial code like Qt uses a lot of macros for system dependent things.

They handle compatibility with autoconf automake, which generates the
appropriate macro definitions and makefiles for compiling in your system.

#ifdef linux
or #ifdef Linux

should just work, specially when all you have to comply is with Linux
and Solaris compatibility.

another thing you can try is using make rules.

Have your makefile include a golbal rules file

localrules.$(OSNAME)

eg

localrules.Linux
localrules.Solaris

You can use 'uname' to use the rules that apply to the system you are
compiling on, and maybe define compile time variables -DLIN_COMP_VAR.

Again, this is one of the things macros are good for. Don't be afraid
to use them....

Jorge L.

Jul 22 '05 #15

P: n/a
> have you checked the man page for uname?
uname -a: returns all avaialble info for the given machine/OS.
I just looked at the manpages for uname and I tried "uname -sr".
However, I was a little surprised by the result, since it printed
"SunOS 5.8" and the comments that appear when I log in remotely
to the server say that it runs Solaris 8. (If those two are the same,
please tell me)

And another thing: how can I call this from our C++ code?
I suppose using the system() function. But then, how can I get the
result that this function prints?
Kevin
"Joe Cipale" <jo**@aracnet.com> schreef in bericht
news:40***************@aracnet.com... Kevin A wrote:

Hi,
Is there a way to determine the name and version of the
operating system in a portable way? (for Solaris/Linux)

Thanks,

Kevin


have you checked the man page for uname?
uname -a: returns all avaialble info for the given machine/OS.

Take a look at uname.
--
#----------------------------------------------------------#
# Penguinix Consulting #
#----------------------------------------------------------#
# Software development, QA and testing. #
# Linux support and training. #
# "Don't fear the penguin!" #
#----------------------------------------------------------#
# Registered Linux user: #309247 http://counter.li.org #
#----------------------------------------------------------#

Jul 22 '05 #16

P: n/a
Kevin A wrote:
have you checked the man page for uname?
uname -a: returns all avaialble info for the given machine/OS.


I just looked at the manpages for uname and I tried "uname -sr".
However, I was a little surprised by the result, since it printed
"SunOS 5.8" and the comments that appear when I log in remotely
to the server say that it runs Solaris 8. (If those two are the same,
please tell me)

And another thing: how can I call this from our C++ code?
I suppose using the system() function. But then, how can I get the
result that this function prints?


None of this is on topic, relevant, or able to be answered within this forum.

Your best bet is to take your question to a Solaris/Unix/Linux forum and post
it there.
Jul 22 '05 #17

P: n/a
"Kevin A" wrote*:
I just looked at the manpages for uname and I tried "uname -sr".
However, I was a little surprised by the result, since it printed
"SunOS 5.8" and the comments that appear when I log in remotely
to the server say that it runs Solaris 8. (If those two are the same,
please tell me)
Indeed they are. Well actually one is only the operating system and the
other one includes the remaining tools, but basically it is the same.
And another thing: how can I call this from our C++ code?
I suppose using the system() function. But then, how can I get the
result that this function prints?


try "man 2 uname" instead of "man uname".
Jul 22 '05 #18

P: n/a
Kevin A wrote:
#ifdef linux
or #ifdef Linux

should just work, specially when all you have to comply is with Linux
and Solaris compatibility.

I just found a reason why this is not the solution I'm looking for.
I'd like to have the entire name of the OS, something like
"Suse Linux" and its version number.
But the macro's can only answer: "is it Linux, yes or no?"
Kevin

You can get uname to give you more insight. I dont't rember what
exactly, but uname will indeed give you more information than just Linux
or Solaris, although I don't know if Suse will be in the information.

From there on, you will then need to define your own macros given the
information requested.

I do really think that all your problems can be solved through the use
of autoconf automake, but you'll have to do your own research on this...

Jorge L.

Jul 22 '05 #19

P: n/a
> Not in the std C++ sense.

I guess that the point here is how to do this. You are correct, and so
are they. Among any Unix variant, uname is great, and hence it is
protable...

JLR
Jul 22 '05 #20

P: n/a
Joe Cipale wrote:
Julie wrote:
Joe Cipale wrote:
Julie wrote:

Joe Cipale wrote:

>Kevin A wrote:
>
>>Hi,
>>Is there a way to determine the name and version of the
>>operating system in a portable way? (for Solaris/Linux)
>>
>>Thanks,
>>
>>Kevin
>
>have you checked the man page for uname?
>uname -a: returns all avaialble info for the given machine/OS.
>
>Take a look at uname.

Not portable, nor standard C++.

Funny...

works the same on my BSD/Solaris/Linux machines (just a small sample)...
~sheesh~


So is your point that it is standard C++ and/or portable?

it is portable...


I tried this on VRTX operating system. No such command.
I tried this on MSDOS operating system. No such command.
I tried this on VAX operating system. No such command.
I tried this on our custom OS for an embedded system. No such command.

How could it be portable?
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #21

P: n/a
Jorge Rivera wrote:
Not in the std C++ sense.

I guess that the point here is how to do this. You are correct, and so
are they. Among any Unix variant, uname is great, and hence it is
protable...

JLR


But it is only protable [sic] to *Nix operating systems.
There are plenty of operating systems out there that
are neither MS Windows or *NIX.

For example, the MS-DOS operating system doesn't have uname.
Neither does: VAX, VRTX, nor custom operating systems.
Don't assume that the entire world has your operating system.
Many embedded systems either have no operating system,
proprietary OS or off-the-shelf OS. Many of which don't
support file systems or the uname command.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #22

P: n/a
Thomas Matthews wrote:
How could it be portable?


It is portable, within a certain domain. You can 'carry' it from one
UNIX-like system to another. The word is being used in a sensible way.
Standard C++ is not "portable" if that means it has to work for every
computer ever built.

--
Regards,
Buster.
Jul 22 '05 #23

P: n/a
Buster wrote:

Thomas Matthews wrote:
How could it be portable?
It is portable, within a certain domain.


Then it is of no interrest to comp.lang.c++
Plain and simple.
You can 'carry' it from one
UNIX-like system to another. The word is being used in a sensible way.
Standard C++ is not "portable" if that means it has to work for every
computer ever built.


Exactly that is the intent of Standard C++.
IF there is a C++ compiler on that machine AND
that compiler is standard compliant AND
the program uses only standard C++ constructs AND
the program doesn't contain undefined behaviour

THEN
the compiler can compile that program and
a program run will produce identical output

Granted: There is only 1 compiler currently known which is fully
standard compliant. But all the others are close to it and the
non standard compliant areas in them mostly deal with 'esoteric'
corners of the language which are not used by many programmers.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #24

P: n/a
> Short answer: no

Long answer: no


Then C++ is too limited :)
Kevin
Jul 22 '05 #25

P: n/a
Karl Heinz Buchegger wrote:

Buster wrote:

Thomas Matthews wrote:
How could it be portable?


It is portable, within a certain domain.


Then it is of no interrest to comp.lang.c++
Plain and simple.
You can 'carry' it from one
UNIX-like system to another. The word is being used in a sensible way.
Standard C++ is not "portable" if that means it has to work for every
computer ever built.


Exactly that is the intent of Standard C++.
IF there is a C++ compiler on that machine AND
that compiler is standard compliant AND
the program uses only standard C++ constructs AND
the program doesn't contain undefined behaviour

THEN
the compiler can compile that program and
a program run will produce identical output

Granted: There is only 1 compiler currently known which is fully
standard compliant. But all the others are close to it and the
non standard compliant areas in them mostly deal with 'esoteric'
corners of the language which are not used by many programmers.

--


Agreed Karl...
--
#----------------------------------------------------------#
# Penguinix Consulting #
#----------------------------------------------------------#
# Software development, QA and testing. #
# Linux support and training. #
# "Don't fear the penguin!" #
#----------------------------------------------------------#
# Registered Linux user: #309247 http://counter.li.org #
#----------------------------------------------------------#
Jul 22 '05 #26

P: n/a
Thomas Matthews wrote:

But it is only protable [sic] to *Nix operating systems.
There are plenty of operating systems out there that
are neither MS Windows or *NIX.


The person who posted the original question posted it regarding *nix
operating systems.

Joe
--
#----------------------------------------------------------#
# "Don't fear the penguin!" #
#----------------------------------------------------------#
# Registered Linux user: #309247 http://counter.li.org #
#----------------------------------------------------------#
Jul 22 '05 #27

P: n/a
Karl Heinz Buchegger wrote:
Exactly that is the intent of Standard C++.
IF there is a C++ compiler on that machine AND
that compiler is standard compliant AND
the program uses only standard C++ constructs AND
the program doesn't contain undefined behaviour
the program doesn't contain implementation-defined behavior
the program doesn't contain unspecified behavior
the program doesn't contain locale-specific behavior
I-D is a special case of unspecified behavior, of course.

THEN
the compiler can compile that program and
a program run will produce identical output


Brian Rodenborn
Jul 22 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.