How would one get the name of the server (Linux) that the C code runs on? I want to fetch and display the name of the server once the program is executed.
Printable View
How would one get the name of the server (Linux) that the C code runs on? I want to fetch and display the name of the server once the program is executed.
Do you want the server name, or the host name? They aren't necessarily the same thing.
For the server name do man 2 uname
For the host name do man 2 gethostname
Right, thanks. I googled them and came up with the following:
http://www.cl.cam.ac.uk/cgi-bin/manpage?2+uname
So how would I implement this in my code? I include the sys/utsname.h library, and include the definiton int uname(struct utsname *buf); but how do I use uname in my code?
I don't have a compiler handy, but something like
Code:#include <stdio.h>
#include <sys/utsname.h>
int main(void)
{
int rc;
struct utsname buf;
rc = uname(&buf);
if (rc == 0)
{
printf("Server name is %s\n", buf.nodename);
}
else
{
/* Error */
}
return(0);
}
If you are on linux, then there is no need to google the man pages.... just man them on the CLI.
thanks but that didn't work. anyway, i got the function implemented using system()... problem solved.
lol, why not just drop C altogether and start shell scripting 100%Quote:
Originally Posted by Opel_Corsa
i wasn't initially planning to use unix system calls but because i was short on time i had no option...Quote:
Originally Posted by Laserve
What do you mean it didn't work? It worked fine on the linux box I ran it on later.Quote:
thanks but that didn't work. anyway, i got the function implemented using system()... problem solved.
> thanks but that didn't work. anyway, i got the function implemented using system()... problem solved.
*shakes head*
Oh dear oh dear oh dear