33 lines
641 B
C
33 lines
641 B
C
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <limits.h>
|
|
#include <unistd.h>
|
|
|
|
int main(const int argc, const char **argv) {
|
|
if (argv[argc - 1][0] == '-') {
|
|
printf("hostname [hostname Set new hostname]\n");
|
|
return 0;
|
|
}
|
|
|
|
/* Set hostname */
|
|
if (argc == 2) {
|
|
if (sethostname(argv[argc - 1], strlen(argv[argc - 1])) < 0) {
|
|
fprintf(stderr, "hostname: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* Get info */
|
|
char hostname[HOST_NAME_MAX + 1];
|
|
if (gethostname(hostname, sizeof(hostname)) < 0) {
|
|
fprintf(stderr, "hostname: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
puts(hostname);
|
|
return 0;
|
|
}
|