2023-10-22 15:17:00 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
int main(const int argc, const char **argv) {
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (argv[i][0] != '-')
|
|
|
|
break;
|
|
|
|
|
|
|
|
else if (!strcmp(argv[i], "--help")) {
|
2023-11-04 18:02:29 +03:00
|
|
|
printf("hostname [hostname Set new hostname]\n");
|
2023-10-22 15:17:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Set hostname */
|
|
|
|
if (argc - i == 1) {
|
|
|
|
if (sethostname(argv[argc - 1], strlen(argv[argc - 1])) < 0) {
|
|
|
|
fprintf(stderr, "hostname: %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get info */
|
2023-10-23 18:48:27 +03:00
|
|
|
char hostname[HOST_NAME_MAX + 1];
|
2023-10-22 15:17:00 +03:00
|
|
|
if (gethostname(hostname, sizeof(hostname)) < 0) {
|
|
|
|
fprintf(stderr, "hostname: %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-23 18:48:27 +03:00
|
|
|
puts(hostname);
|
2023-10-22 15:17:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|