Platform type in Unix

Hi all,

How does one get the platform type in UNIX (for e.g. sparc/i386)? I need an function call and not the command like uname -p.

thanks!

See if you have the uname system call available on your machine. Check for man 2 uname

It is there but it doesn't give platform type/architecture :frowning:

You are seeing a following example.

#include <sys/utsname.h>

main()
{
    struct utsname sys_inf;

    uname(&sys_inf);

    printf("System name - %s \n", sys_inf.sysname);
    printf("Nodename    - %s \n", sys_inf.nodename);
    printf("Release     - %s \n", sys_inf.release);
    printf("Version     - %s \n", sys_inf.version);
    printf("Machine     - %s \n", sys_inf.machine);
}

Best regards,
Iliyan Varshilov

Does this mean you are running compiled code? If so you should already know what architecture, or at least the compiler does.

For example, a C compiler may predefine one of the following as #defines:

__ppc__
__386__
__x86_64__
__mips__
__sparc__ or __sparc
__hppa
__alpha

Also, look at "config.guess"

But none of them gives processor architecture :frowning:

-rw-r--r-- 1 ananias ananias 2050 Jun 5 12:59 -l

how do i remove such a filename -l, that i exdentaly created but if u do ls -al it say it cannot open and it i say cat -l
cat: illegal option -- l
usage: cat [-benstuv] [file ...]

In my Linux OS output is following:

System name - Linux 
Nodename    - linux 
Release     - 2.6.13-15.8-default 
Version     - #1 Tue Feb 7 11:07:24 UTC 2006 
Machine     - x86_64 

Where "Machine - x86_64 " is processor architecture.

Best regards,
Iliyan Varshilov

I am not sure why this has been posted in this thread,

Use

rm -- -l 

,Here "--" mark the end of options.

Thanks
Nagarajan G

thnx Nagarajan G it really worked

Am NEW to UNIX World and i have been studyin UNIX for about 2 weeks as from my point of view it seem that i ve known most of the commands needed for a start what area of interest should i still look at beside commands coz i ve studied most of the commands needed in viewind to coppyin and editiind and moving file so please advice coz i run out of ideas on what else to study

PLEASE I NEED HELP..

anaias - why are you hijacking this thread?

slash_blog - the uname() call can produce the same output as the uname command.
Is there a command line utility that produces what you want on your machine?

what do u mean "why am i hijacking this thread?

slash_blog - the uname() call can produce the same output as the uname command.
Is there a command line utility that produces what you want on your machine?"

i don understant explain pls

do u mean trying to steer a web forum discussion thread off topic by discussing a subject entirely unrelated to the subject at hand. ...

You're asking questions about basic UNIX stuff in a C programming thread about uname.

I think on some UNIX flavors the documented command to get the platform is uname -p and not uname -m. The uname() call produces output which is more synonymous with uname -m. So while this works for Linux, for some other flavors such as Sun Solaris and HP-UX this isn't always correct.

However porter's approach is working for me so far. Though I am using
#ifdef _POWER

for IBM AIX powerpc architecture, because I couldn't find a __ppc__ or __powerpc__ defined by gcc on AIX. Is it correct to use _POWER?

thanks all for your help.

i know that is why i need u guys to tell me what i should learn man i need ur advices

Sounds right, sometimes the macros don't have the trailing "__". As always because we don't know why you need the processor architecture we can't advise you more.

As a couple of suggestions you may be trying to find out if you are big or little endian, or perhaps 32 or 64 bit.

In the first case for macros like "_BYTE_ORDER", "_LITTLE_ENDIAN" and "_BIG_ENDIAN".

In the second case look for macros like LP64 or __LP64_ or even "_WIN64".

This may end up long winded but worth it in the long run, I have a single file I extend as I test more architectures.

#if (!defined(_PLATFORM_BIG_ENDIAN_)) && (!defined(_PLATFORM_LITTLE_ENDIAN_))
/* 
 *      first try and determine from sys/param.h 
 */
#       include <sys/param.h>
#       if defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
#               if (_BIG_ENDIAN != _LITTLE_ENDIAN)
#                       if (_BYTE_ORDER==_BIG_ENDIAN)
#                               define _PLATFORM_BIG_ENDIAN_
#                       else
#                               if (_BYTE_ORDER==_LITTLE_ENDIAN)
#                                       define _PLATFORM_LITTLE_ENDIAN_
#                               endif
#                       endif
#               endif
#       endif
#endif

#if (!defined(_PLATFORM_BIG_ENDIAN_)) && (!defined(_PLATFORM_LITTLE_ENDIAN_))
/* 
 * second, try a couple of simple agnostic macros
 */
#       if defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
#               define _PLATFORM_BIG_ENDIAN_
#       endif
#       if defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
#               define _PLATFORM_LITTLE_ENDIAN_
#       endif
#endif

#if (!defined(_PLATFORM_BIG_ENDIAN_)) && (!defined(_PLATFORM_LITTLE_ENDIAN_))
/* 
 * thirdly, look for processor specific flags and go from there
 * mips and ia64 could be either endian
 */
#       if  defined(__ppc__) || \
                ((defined(__mips__)||defined(__mips)) && ( defined(__MIPSEB__) || defined(MIPSEB) ) ) || \
                defined(__sparc__) || \
                defined(__sparc) || \
                defined(__hppa) || \
                defined(_sun3_param_h)
#               define _PLATFORM_BIG_ENDIAN_
#       else
#               if  defined(__i386__) || \
                        defined(__alpha) || \
                        defined(__x86_64__) || \
                        ( (defined(__mips__)||defined(__mips)) && ( defined(__MIPSEL__) || defined(MIPSEL) ) )
#                       define _PLATFORM_LITTLE_ENDIAN_
#               endif
#       endif
#endif

#if (!defined(_PLATFORM_BIG_ENDIAN_)) && (!defined(_PLATFORM_LITTLE_ENDIAN_))
#       error could not determine endianness, not enough clues
#endif

#if defined(_PLATFORM_BIG_ENDIAN_) && defined(_PLATFORM_LITTLE_ENDIAN_)
#       error could not determine endianness, result confusing
#endif

Porter:

That's a lot of header work...I wonder how much of that evolved as you tested and found the original tricks didn't work on a particual machine?

For all that...a simple startup application call during runtime may have been worth it. Something like:

if ((uint16_t) 1 == htons((uint16_t) 1))
  /* BIG */
else
  /* NOT BIG (may assume LITTLE; exists a chance that we could be MIDDLE) */

Network order is defined as big endian...so if the machine stores differently than the network order then htons will switch up the bytes. This will, of course, result in its returning something other than the input value.

Easy enough if you need the data at runtime...doesn't help you much if you need it at compile time, of course.

True.

Been there, done that, however that fails in the following two cases..

  1. cross compiling for another machine, eg building on a 386 for a target of an m68k, eg building PalmOS programs.

  2. building FAT binaries on MacOSX/Darwin where the target is simultaneously i386 and ppc and hence both big-endian and little-endian.

In both cases running a program tells you everything about your compilation host and nothing about your target system.

My preferred code is as follows...

short s=*(const short *)"\001\000\000\000";
if (s < 256)
{
    ... little endian ...
}
else
{
    ... big endian ...
}