Just use HOST_NAME_MAX in hostname(1)

Using sysconf() + malloc() is overkill.
This commit is contained in:
sin 2014-11-13 17:21:37 +00:00
parent 44b3ea8f76
commit e35d9e62a5
1 changed files with 2 additions and 15 deletions

View File

@ -1,7 +1,6 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
@ -15,16 +14,7 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
long sz; char host[HOST_NAME_MAX + 1];
char *host;
sz = sysconf(_SC_HOST_NAME_MAX);
if (sz < 0)
sz = 255;
host = malloc(sz + 1);
if (!host)
eprintf("malloc:");
ARGBEGIN { ARGBEGIN {
default: default:
@ -32,15 +22,12 @@ main(int argc, char *argv[])
} ARGEND; } ARGEND;
if (argc < 1) { if (argc < 1) {
if (gethostname(host, sz + 1) < 0) if (gethostname(host, sizeof(host)) < 0)
eprintf("gethostname:"); eprintf("gethostname:");
puts(host); puts(host);
} else { } else {
if (sethostname(argv[0], strlen(argv[0])) < 0) if (sethostname(argv[0], strlen(argv[0])) < 0)
eprintf("sethostname:"); eprintf("sethostname:");
} }
free(host);
return 0; return 0;
} }