2012-01-30 23:29:13 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
*
|
|
|
|
* hostname
|
2012-07-07 08:08:28 +04:00
|
|
|
*
|
|
|
|
* Prints or sets the system hostname.
|
2012-01-30 23:29:13 +04:00
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
|
|
|
|
#define ROOT_UID 0
|
|
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
if (argc < 2) {
|
|
|
|
char tmp[256];
|
|
|
|
syscall_gethostname(tmp);
|
|
|
|
printf("%s\n", tmp);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (syscall_getuid() != ROOT_UID) {
|
|
|
|
fprintf(stderr,"Must be root to set hostname.\n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
syscall_sethostname(argv[1]);
|
|
|
|
FILE * file = fopen("/etc/hostname", "w");
|
|
|
|
if (!file) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
fprintf(file, "%s\n", argv[1]);
|
|
|
|
fclose(file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|