getenv() fails II

Giorgos Keramidas keramida at ceid.upatras.gr
Tue Jul 13 20:31:28 PDT 2004


On 2004-07-13 22:25, Miguel Cardenas <mfcardenas at prodigy.net.mx> wrote:
> Efectively was that way... in Linux it used to work fine... then my
> next question should be... is there any other way to retrieve the
> hostname? and...  as a normal user?

Yep.  See the manpage of gethostname().  This should work fine:

     1	#include <stdio.h>
     2	#include <stdlib.h>
     3	#include <unistd.h>
     4
     5	#define HOST_NAME_MAX 255
     6	static char buf[HOST_NAME_MAX + 1];
     7
     8	int
     9	main(void)
    10	{
    11
    12		if (gethostname(buf, HOST_NAME_MAX) == -1) {
    13			fprintf(stderr,
    14			    "error: my hostname is not defined\n");
    15			exit(EXIT_FAILURE);
    16		}
    17		printf("%s\n", buf);
    18		return EXIT_SUCCESS;
    19	}

Giorgos



More information about the freebsd-questions mailing list