[comp.unix.questions] Determining system type

gregh@cup.portal.com (Greg S Hinton) (03/05/89)

Is there a "standard" -- or optimal -- way to determine at compile time
whether one is on a BSD or AT&T (Ver7, SysIII, SysV) system?
This is the best I've been able to come up with so far:

#include <stat.h>

#if defined(S_ISOCK)

/* BSD stuff */

#else

/* AT&T stuff */

#endif


Anyone have a better way?

-----------

Greg Hinton
gregh@cup.portal.com
{hplabs!pyramid,amdahl!sun}!cup.portal.com!gregh

debra@alice.UUCP (Paul De Bra) (03/06/89)

In article <15406@cup.portal.com> gregh@cup.portal.com (Greg S Hinton) writes:
}Is there a "standard" -- or optimal -- way to determine at compile time
}whether one is on a BSD or AT&T (Ver7, SysIII, SysV) system?
}This is the best I've been able to come up with so far:
}
}#include <stat.h>
}
}#if defined(S_ISOCK)
}
}/* BSD stuff */
}
}#else
}
}/* AT&T stuff */
}
}#endif
}
}
}Anyone have a better way?

The #if and #ifdef is indeed the "standard" way to do this. The real problem
is to have a make-procedure figure out what system you have. There are a
number of "clues" in the system files which can help (look for files /vmunix,
/xenix, /usr/include/string.h, /usr/include/strings.h, etc. etc, and with
a lot of work you script may decide what system you have).
There are also compiler-dependent flags. #ifdef sparc comes to mind.
But then there will always be flags you have to set by hand, like whether
you want to compile something for sunview or x11...

So the way to have these flags in the C-program is standard, but there is
no standard and/or optimal to determine the flags to give to the compiler.

Anyone with brilliant new ideas?

Paul.
-- 
------------------------------------------------------
|debra@research.att.com   | uunet!research!debra     |
------------------------------------------------------

gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/06/89)

In article <15406@cup.portal.com> gregh@cup.portal.com (Greg S Hinton) writes:
>Is there a "standard" -- or optimal -- way to determine at compile time
>whether one is on a BSD or AT&T (Ver7, SysIII, SysV) system?
>This is the best I've been able to come up with so far:
>#include <stat.h>
>#if defined(S_ISOCK)

You could also test what <sgtty.h> defines.

The real problem is that you're assuming that there are well-defined,
"pure" BSD vs. AT&T environments.  In practice, vendors have grafted
some bits of other systems into their implementations.  This will
reach its zenith with UNIX System V Release 4.  Instead of trying to
conditionalize on "system type", consider tailoring for the exact
features you're interested in; this often requires adding -Ds to
CFLAGS in the Makefile to set the appropriate "feature test macros".
Another viable solution is to isolate all system dependencies in
separate modules, compiling whichever module is appropriate.

guy@auspex.UUCP (Guy Harris) (03/06/89)

>Is there a "standard" -- or optimal -- way to determine at compile time
>whether one is on a BSD or AT&T (Ver7, SysIII, SysV) system?

No.

I suspect you're really not interested in whether you're on a "BSD" or
"AT&T" system; you're probably *really* interested in "does this feature
exist on my system?" or "how should I perform some particular
operation?"

Well, in some ways, the "AT&T" system V7 is more like the "BSD" system
4.2BSD than like the "AT&T" system "System III" or "System V".  In other
ways, it's more like the other "AT&T" systems.  And, in some ways, later
releases of S5 are more like BSD than V7!  (V7 had neither the "mkdir" nor
the "rmdir" system call, nor the "directory library" - "opendir",
"readdir", etc..)

In addition, although some system may be thought of as "BSD-based", it
may have the S5 feature you're looking for, or a "S5-based" system may
have the BSD feature you're looking for.

And, just to top things off, there may be features present in *some*
"BSD-based" systems, as well as *some* "S5-based" systems, but not
present in others.

Trying to split the world into "AT&T" and "BSD" systems won't work.  The
world is more complicated than that.

You should, instead, try to determine whether the particular feature for
which you're looking is present.  Your example is an almost-correct way
of determining whether UNIX-domain sockets are present, although you
should:

	1) be including <sys/stat.h>, not <stat.h>

	2) be including <sys/types.h> before <sys/stat.h>

	3) be testing whether "S_IFSOCK" is defined, not "S_ISOCK".

In some cases, there is no magic #define that will tell you whether a
feature is present or not.