toaruos/apps/hostname.c

38 lines
836 B
C
Raw Normal View History

2018-08-15 04:07:33 +03:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
2018-08-01 21:40:43 +03:00
* This file is part of ToaruOS and is released under the terms
2018-02-25 17:05:45 +03:00
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2018 K. Lange
2018-02-25 17:05:45 +03:00
*
* hostname
*
* Prints or sets the system hostname.
*/
#include <stdio.h>
2018-08-01 21:40:43 +03:00
#include <unistd.h>
#include <string.h>
2018-02-25 17:05:45 +03:00
int main(int argc, char * argv[]) {
if ((argc > 1 && argv[1][0] == '-') || (argc < 2)) {
2018-08-01 21:40:43 +03:00
char tmp[256] = {0};
gethostname(tmp, 255);
2018-02-25 17:05:45 +03:00
printf("%s\n", tmp);
return 0;
} else {
2018-08-01 21:40:43 +03:00
if (getuid() != 0) {
2018-02-25 17:05:45 +03:00
fprintf(stderr,"Must be root to set hostname.\n");
return 1;
} else {
2018-08-01 21:40:43 +03:00
sethostname(argv[1], strlen(argv[1]));
2018-02-25 17:05:45 +03:00
FILE * file = fopen("/etc/hostname", "w");
if (!file) {
return 1;
} else {
fprintf(file, "%s\n", argv[1]);
fclose(file);
return 0;
}
}
}
return 0;
}