This commit is contained in:
Your Name 2023-10-23 18:48:27 +03:00
parent b926a178d4
commit deef828e3c
3 changed files with 9 additions and 43 deletions

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
all:
chmod +x build.sh
./build.sh rootfs
clean:
./build.sh clean

0
build.sh Normal file → Executable file
View File

View File

@ -3,31 +3,15 @@
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(const int argc, const char **argv) {
unsigned int f_flag = 0;
unsigned int d_flag = 0;
unsigned int i_flag = 0;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "-f"))
f_flag = 1;
else if (!strcmp(argv[i], "-d"))
d_flag = 1;
else if (!strcmp(argv[i], "-i"))
i_flag = 1;
else if (!strcmp(argv[i], "--help")) {
printf("hostname [-f (Display the FQDN)] [-d (Display domainname)] [-i (Addresses for the hostname)]/ [hostname (Set new hostname)]\n");
printf("hostname [hostname (Set new hostname)]\n");
return 0;
}
}
@ -44,37 +28,13 @@ int main(const int argc, const char **argv) {
}
/* Get info */
char hostname[HOST_NAME_MAX];
char hostname[HOST_NAME_MAX + 1];
if (gethostname(hostname, sizeof(hostname)) < 0) {
fprintf(stderr, "hostname: %s\n", strerror(errno));
return 1;
}
struct hostent *hp = gethostbyname(hostname);
if (!hp) {
fprintf(stderr, "hostname: %s\n", strerror(errno));
return 1;
}
/* Print info */
if (f_flag)
puts(hp->h_name);
else if (d_flag) {
char *ptr = strchr(hp->h_name, '.');
if (ptr)
puts(ptr + 1);
}
else if (i_flag) {
while (hp->h_addr_list[0])
printf("%s ", inet_ntoa( *(struct in_addr *) *(hp->h_addr_list++) ));
printf("\n");
}
else
puts(hostname);
puts(hostname);
return 0;
}