hostname: use POSIX routine to get hostname rather than reading from procfs

This commit is contained in:
Ali H. Fardan 2016-10-08 19:55:25 +03:00
parent fb440d8f62
commit 4339c8330a

View File

@ -271,14 +271,10 @@ hostname(void)
char buf[HOST_NAME_MAX]; char buf[HOST_NAME_MAX];
FILE *fp; FILE *fp;
fp = fopen("/proc/sys/kernel/hostname", "r"); if (gethostname(buf, sizeof(buf)) == -1) {
if (fp == NULL) { warn(1, "hostname");
warn("Failed to open file /proc/sys/kernel/hostname");
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
fgets(buf, sizeof(buf), fp);
buf[strlen(buf)-1] = '\0';
fclose(fp);
return smprintf("%s", buf); return smprintf("%s", buf);
} }