1 #include "elm_defs.h"
2
3 #ifdef DOUNAME
4 # include <sys/utsname.h>
5 #endif
6
7
8 /* return local host name (with any domain elided) */
get_hostname(retval,retsiz)9 void get_hostname(retval, retsiz)
10 char *retval;
11 int retsiz;
12 {
13 char *s;
14
15 retval[0] = '\0';
16
17 #ifdef HOSTCOMPILED
18 (void) strfcpy(retval, HOSTNAME, retsiz);
19 #endif
20
21 #ifdef GETHOSTNAME
22 /* warning - some systems return FQDN */
23 if (retval[0] == '\0')
24 (void) gethostname(retval, retsiz-1);
25 #endif
26
27 #ifdef DOUNAME
28 /* warning - some systems return FQDN */
29 if (retval[0] == '\0') {
30 struct utsname uts;
31 extern int uname();
32 (void) uname(&uts);
33 (void) strfcpy(retval, uts.nodename, retsiz);
34 }
35 #endif
36
37 #if (defined(XENIX) || defined(M_UNIX))
38 if (retval[0] == '\0') {
39 FILE *fp;
40 if ((fp = fopen("/etc/systemid", "r")) != NULL) {
41 if (fgets(retval, retsiz-1, fp) != NULL) {
42 if ((s = strchr(retval, '\n')) != NULL)
43 *s = '\0';
44 }
45 (void) fclose(fp);
46 }
47 }
48 #endif
49
50 if (retval[0] == '\0')
51 (void) strfcpy(retval, HOSTNAME, retsiz);
52
53 if ((s = strchr(retval, '.')) != NULL)
54 *s = '\0';
55 }
56
57
58 /* return local domain (with leading dot) */
get_hostdomain(retval,retsiz)59 void get_hostdomain(retval, retsiz)
60 char *retval;
61 int retsiz;
62 {
63 char buf[SLEN], *s;
64 FILE *fp;
65
66 buf[0] = '\0';
67
68 if ((fp = fopen(system_hostdom_file, "r")) != NULL) {
69 if (fgets(buf, sizeof(buf), fp) != NULL) {
70 if ((s = strchr(buf, '\n')) != NULL)
71 *s = '\0';
72 }
73 (void) fclose(fp);
74 }
75
76 #ifdef USEGETDOMAINNAME
77 if (buf[0] == '\0')
78 (void) getdomainname(buf, sizeof(buf));
79 #endif
80
81 #ifdef GETHOSTNAME
82 if (buf[0] == '\0') {
83 char fqdn[SLEN];
84 (void) gethostname(fqdn, sizeof(fqdn));
85 if ((s = strchr(fqdn, '.')) != NULL)
86 (void) strfcpy(buf, s, sizeof(buf));
87 }
88 #endif
89
90 if (buf[0] == '\0')
91 (void) strfcpy(buf, DEFAULT_DOMAIN, sizeof(buf));
92
93 if (buf[0] != '.') {
94 *retval++ = '.';
95 --retsiz;
96 }
97 (void) strfcpy(retval, buf, retsiz);
98 }
99
100
101 #ifdef _TEST
main()102 main()
103 {
104 char buf[SLEN];
105 get_hostname(buf, sizeof(buf));
106 printf("get_hostname() returns \"%s\"\n", buf);
107 get_hostdomain(buf, sizeof(buf));
108 printf("get_hostdomain() returns \"%s\"\n", buf);
109 exit(0);
110 }
111 #endif
112
113