blob: ebfc803c1f7b3b8acb27b79a46fe473107d2b227 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
/* hostname.c - Get/Set the hostname
*
* Copyright 2012 Andre Renaud <andre@bluewatersys.com>
*
* http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/hostname.html
USE_HOSTNAME(NEWTOY(hostname, "bF:", TOYFLAG_BIN))
config HOSTNAME
bool "hostname"
default y
help
usage: hostname [-b] [-F FILENAME] [newname]
Get/Set the current hostname
-b Set hostname to 'localhost' if otherwise unset
-F Set hostname to contents of FILENAME
*/
#define FOR_hostname
#include "toys.h"
GLOBALS(
char *fname;
)
void hostname_main(void)
{
const char *hostname = toys.optargs[0];
if (toys.optflags & FLAG_F) {
char *buf;
if ((hostname = buf = readfile(TT.fname, 0, 0))) {
size_t len = strlen(hostname);
char *end = buf + len - 1;
/* Trim trailing whitespace. */
while (len && isspace(*end)) {
*end-- = '\0';
len--;
}
if (!len) {
free(buf);
hostname = NULL;
if (!(toys.optflags & FLAG_b))
error_exit("empty file '%s'", TT.fname);
}
} else if (!(toys.optflags & FLAG_b))
error_exit("failed to read '%s'", TT.fname);
}
if (!hostname && toys.optflags & FLAG_b) {
/* Do nothing if hostname already set. */
if (gethostname(toybuf, sizeof(toybuf))) perror_exit("get failed");
if (strnlen(toybuf, sizeof(toybuf))) exit(0);
/* Else set hostname to localhost. */
hostname = "localhost";
}
if (hostname) {
if (sethostname(hostname, strlen(hostname)))
perror_exit("set failed '%s'", hostname);
} else {
if (gethostname(toybuf, sizeof(toybuf))) perror_exit("get failed");
xputs(toybuf);
}
}
|