1833857 Members
2883 Online
110063 Solutions
New Discussion

Re: gethostid

 
SOLVED
Go to solution
David_246
Trusted Contributor

gethostid

Hi,

I am working on getting C-knowledge, but it's not completely working yet for me :)
Can anyone help me out in receiving a machines hostid ??

#include

int long g;
g = gethostid(void);

int main(void)
{
printf ("The hostid of this machine is : %c\n, g");
}

I am missing something elemantary here. Any help will be much apreciated !!

Regs David
@yourservice
8 REPLIES 8
Massimo Bianchi
Honored Contributor

Re: gethostid

Hi,
neither i know C, but

gethostid returns a lon, whereas the printf is expecting a pointer to a null terminated strings.

It may be this ?

Massimo
David_246
Trusted Contributor

Re: gethostid

Hi Massimo,

You are probably right ( I think I understand you :)
when compiling it says :

# gcc tmp.c
tmp.c:5: conflicting types for `g'
tmp.c:4: previous declaration of `g'
tmp.c:5: parse error before "void"

Can you please let me know how the program should look like ?

Regs David
@yourservice
James R. Ferguson
Acclaimed Contributor
Solution

Re: gethostid

Hi David:

This will work:

#include
int long g;
int main(void)
{
g = gethostid();
printf ("The hostid of this machine is : %d\n", g);
}

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: gethostid

Hi (again) David:

If you want to use ANSI prototypes the code would look like this:

#include

long gethostid(void);
int long g;

int main(void)
{
g=gethostid();
printf ("The hostid of this machine is : %d\n",g);
return 0;
}

...note that I added a return code since your main() declaration was of type 'int' not 'void'.

Regards!

...JRF...
David_246
Trusted Contributor

Re: gethostid

Hhm,
The actual value of g should be set from inside main(). The declaration should be done before and the printf of course should be %d.

Many, many thanks !!

Regs David
@yourservice
David_246
Trusted Contributor

Re: gethostid

Hhm,
The actual value of g should be set from inside main(). The declaration should be done before and the printf of course should be %d.

Many, many thanks !!

Regs David
@yourservice
Mike Stroyan
Honored Contributor

Re: gethostid

The format for printing a long should really be "%ld". The %d format will break if you compile for 64-bit pointers and longs.

You don't need to declare gethostid explicitly because it is declared in unistd.h.

You should include stdio.h because you are calling printf.

The 11i manual entry for gethostid warns that it will be obsolete someday. You might want to shift to using confstr with _CS_MACHINE_IDENT or _CS_PARTITION_IDENT. (Those ident strings get longer on IPF systems.)

#include
#include
#include

int main(void)
{
long g;
struct utsname name;
char ident[200];
g=gethostid();
printf ("The hostid of this machine is : %ld\n",g);
uname(&name);
printf ("The idnumber of this machine is : %s\n", name.idnumber);
confstr(_CS_MACHINE_IDENT, ident, sizeof(ident));
printf ("The MACHINE_IDENT of this machine is : %s\n", ident);
confstr(_CS_PARTITION_IDENT, ident, sizeof(ident));
printf ("The PARTITION_IDENT of this partition is : %s\n", ident);
return 0;
}
David_246
Trusted Contributor

Re: gethostid

Thanks for your help !!

Regs David
@yourservice