Use sysconf() instead of HOST_NAME_MAX
This commit is contained in:
parent
7309302e25
commit
efbce07f94
17
hostname.c
17
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user