diff --git a/tools/netd/Makefile b/tools/netd/Makefile index d3d2061e..6affdf3b 100644 --- a/tools/netd/Makefile +++ b/tools/netd/Makefile @@ -1,14 +1,22 @@ .phony: all install CC=x86_64-astral-gcc LD=x86_64-astral-gcc -all: netd +all: netd hostname install: mkdir -p "$(DESTDIR)/$(PREFIX)/sbin/" + mkdir -p "$(DESTDIR)/$(PREFIX)/bin/" cp netd "$(DESTDIR)/$(PREFIX)/sbin/netd" + cp hostname "$(DESTDIR)/$(PREFIX)/bin/hostname" netd: main.o $(LD) $(LDFLAGS) -g -o $@ $^ +hostname: hostname.o + $(LD) $(LDFLAGS) -g -o $@ $^ + main.o: main.c $(CC) -c $(CFLAGS) -g -o $@ $< + +hostname.o: hostname.c + $(CC) -c $(CFLAGS) -g -o $@ $< diff --git a/tools/netd/hostname.c b/tools/netd/hostname.c new file mode 100644 index 00000000..bc06f147 --- /dev/null +++ b/tools/netd/hostname.c @@ -0,0 +1,29 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + char hostname[1000]; + + if (argc == 1) { + if (gethostname(hostname, 1000)) { + fprintf(stderr, "%s: failed to get hostname: %s\n", argv[0], strerror(errno)); + return EXIT_FAILURE; + } + + printf("%s\n", hostname); + } else if (argc == 2) { + if (sethostname(argv[1], strlen(argv[1]))) { + fprintf(stderr, "%s: failed to set hostname: %s\n", argv[0], strerror(errno)); + return EXIT_FAILURE; + } + } else { + fprintf(stderr, "%s: usage: %s [hostname]\n", argv[0], argv[0]); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +}