1
0
mirror of git://git.suckless.org/sbase synced 2024-12-24 07:53:08 +00:00
sbase/hostname.c
sin 13cbb19adc Fix wrong length for sethostname()
This was messed up in the previous commit.
2014-02-14 15:13:20 +00:00

47 lines
692 B
C

/* 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"
static void
usage(void)
{
eprintf("usage: %s [name]\n", argv0);
}
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:");
ARGBEGIN {
default:
usage();
} ARGEND;
if (argc < 1) {
if (gethostname(host, sz + 1) < 0)
eprintf("gethostname:");
puts(host);
} else {
if (sethostname(argv[0], strlen(argv[0])) < 0)
eprintf("sethostname:");
}
free(host);
return EXIT_SUCCESS;
}