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#define FOR_hostname
34#define FORCE_FLAGS
35#include "toys.h"
36
37GLOBALS(
38 char *F;
39)
40
41void hostname_main(void)
42{
43 char *hostname = toybuf, *dot;
44 struct hostent *h;
45
46 gethostname(toybuf, sizeof(toybuf)-1);
47 if (TT.F && (hostname = xreadfile(TT.F, 0, 0))) {
48 if (!*chomp(hostname)) {
49 if (CFG_TOYBOX_FREE) free(hostname);
50 if (!FLAG(b)) error_exit("empty '%s'", TT.F);
51 hostname = 0;
52 }
53 } else hostname = (FLAG(b) && !*toybuf) ? "localhost" : *toys.optargs;
54
55
56 if (hostname) {
57 if (sethostname(hostname, strlen(hostname)))
58 perror_exit("set '%s'", hostname);
59 return;
60 }
61
62
63 if (FLAG(d) || FLAG(f)) {
64 if (!(h = gethostbyname(toybuf)))
65 error_exit("gethostbyname: %s", hstrerror(h_errno));
66 snprintf(toybuf, sizeof(toybuf), "%s", h->h_name);
67 }
68 dot = toybuf+strcspn(toybuf, ".");
69 if (FLAG(s)) *dot = '\0';
70 xputs(FLAG(d) ? dot+1 : toybuf);
71}
72
73void dnsdomainname_main(void)
74{
75 toys.optflags = FLAG_d;
76 hostname_main();
77}
78