commit efbce07f941f163a6705bb58250c1e3977d7e685
parent 7309302e25a05aaa69e1d65ac9ae70e82e32c1d9
Author: sin <sin@2f30.org>
Date: Fri, 14 Feb 2014 15:01:15 +0000
Use sysconf() instead of HOST_NAME_MAX
Diffstat:
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/hostname.c b/hostname.c
@@ -15,7 +15,16 @@ usage(void)
int
main(int argc, char *argv[])
{
- char host[HOST_NAME_MAX + 1];
+ long sz;
+ char *host;
+
+ sz = sysconf(_SC_HOST_NAME_MAX);
+ if (sz < 0)
+ sz = 255;
+
+ host = malloc(sz + 1);
+ if (!host)
+ eprintf("malloc:");
ARGBEGIN {
default:
@@ -23,13 +32,15 @@ main(int argc, char *argv[])
} ARGEND;
if (argc < 1) {
- if (gethostname(host, sizeof(host)) < 0)
+ if (gethostname(host, sz + 1) < 0)
eprintf("gethostname:");
puts(host);
} else {
- if (sethostname(argv[0], strlen(argv[0])) < 0)
+ if (sethostname(argv[0], sz + 1) < 0)
eprintf("sethostname:");
}
+ free(host);
+
return EXIT_SUCCESS;
}