mirror of https://github.com/fairyglade/ly.git
Use sysconf(_SC_HOST_NAME_MAX) to determine maximum hostname length (#70)
This commit is contained in:
parent
bd724eae93
commit
e2ed1e654b
19
src/util.c
19
src/util.c
|
|
@ -23,12 +23,24 @@ void hostname(char** out)
|
||||||
{
|
{
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo* info;
|
struct addrinfo* info;
|
||||||
char hostname[HOST_NAME_MAX + 1];
|
char* hostname;
|
||||||
char* dot;
|
char* dot;
|
||||||
|
int host_name_max;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
hostname[HOST_NAME_MAX] = '\0';
|
if ((host_name_max = sysconf(_SC_HOST_NAME_MAX)) == -1)
|
||||||
gethostname(hostname, HOST_NAME_MAX);
|
{
|
||||||
|
perror("sysconf(_SC_HOST_NAME_MAX)");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((hostname = malloc(host_name_max+1)) == NULL)
|
||||||
|
{
|
||||||
|
perror("malloc");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
gethostname(hostname, sizeof(hostname));
|
||||||
memset(&hints, 0, sizeof hints);
|
memset(&hints, 0, sizeof hints);
|
||||||
hints.ai_family = AF_UNSPEC;
|
hints.ai_family = AF_UNSPEC;
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
|
@ -47,6 +59,7 @@ void hostname(char** out)
|
||||||
|
|
||||||
hostname_backup = *out;
|
hostname_backup = *out;
|
||||||
freeaddrinfo(info);
|
freeaddrinfo(info);
|
||||||
|
free(hostname);
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_hostname()
|
void free_hostname()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue