[hackers] [sbase] Just use HOST_NAME_MAX in hostname(1) || sin
commit e35d9e62a562e74964fa7aeccd2285d8e37c751a
Author: sin <sin_AT_2f30.org>
Date: Thu Nov 13 17:21:37 2014 +0000
Just use HOST_NAME_MAX in hostname(1)
Using sysconf() + malloc() is overkill.
diff --git a/hostname.c b/hostname.c
index 4b42848..1e9211b 100644
--- a/hostname.c
+++ b/hostname.c
_AT_@ -1,7 +1,6 @@
/* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
_AT_@ -15,16 +14,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- long sz;
- char *host;
-
- sz = sysconf(_SC_HOST_NAME_MAX);
- if (sz < 0)
- sz = 255;
-
- host = malloc(sz + 1);
- if (!host)
- eprintf("malloc:");
+ char host[HOST_NAME_MAX + 1];
ARGBEGIN {
default:
_AT_@ -32,15 +22,12 @@ main(int argc, char *argv[])
} ARGEND;
if (argc < 1) {
- if (gethostname(host, sz + 1) < 0)
+ if (gethostname(host, sizeof(host)) < 0)
eprintf("gethostname:");
puts(host);
} else {
if (sethostname(argv[0], strlen(argv[0])) < 0)
eprintf("sethostname:");
}
-
- free(host);
-
return 0;
}
Received on Thu Nov 13 2014 - 18:22:16 CET
This archive was generated by hypermail 2.3.0
: Thu Nov 13 2014 - 18:24:10 CET