1 /*
2  *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
3  *      Copyright (c) 1996-2005 Michael T Pins.  All rights reserved.
4  *
5  *	"generic" gethostname() emulation:
6  *
7  *	Possibilities are used in following order:
8  *
9  *	HAVE_GETHOSTNAME		-- use gethostname()
10  *	HAVE_UNAME			-- use sysV uname().nodename
11  *	HOSTNAME_FILE "/.../..."	-- read hostname from file
12  *	HOSTNAME_WHOAMI			-- use sysname from whoami.h
13  *	HOSTNAME "host"			-- hard-coded hostname
14  *	You lose!
15  */
16 
17 #include <unistd.h>
18 #include <string.h>
19 #include "config.h"
20 
21 #undef DONE
22 
23 #ifdef HAVE_GETHOSTNAME
24 /*
25  *	Easy -- we already got it
26  */
27 void
nn_gethostname(char * name,int length)28 nn_gethostname(char *name, int length)
29 {
30     gethostname(name, length);
31 }
32 
33 #define DONE
34 #endif
35 
36 #ifndef DONE
37 
38 #ifdef HAVE_UNAME
39 /*
40  *	System V uname() is available.	Use nodename field.
41  */
42 
43 #include <sys/utsname.h>
44 
45 void
nn_gethostname(char * name,int length)46 nn_gethostname(char *name, int length)
47 {
48     struct utsname  un;
49 
50     uname(&un);
51     strncpy(name, un.nodename, length);
52 }
53 
54 #define DONE
55 #endif
56 
57 #endif
58 
59 #ifndef DONE
60 
61 #ifdef HOSTNAME_FILE
62 /*
63  *	Hostname is available in a file.
64  *	The name of the file is defined in HOSTNAME_FILE.
65  *	This is not intended to fail (or exit would have been via nn_exit)
66  */
67 
68 void
nn_gethostname(char * name,int length)69 nn_gethostname(char *name, int length)
70 {
71     FILE           *f;
72     register char  *p;
73 
74     f = fopen(HOSTNAME_FILE, "r");	/* Generic code -- don't use
75 					 * open_file */
76     if (f == NULL)
77 	goto err;
78     if (fgets(name, length, f) == NULL)
79 	goto err;
80     if ((p = strchr(name, NL)) != NULL)
81 	*p = NUL;
82     fclose(f);
83     return;
84 
85 err:
86     fprintf(stderr, "HOSTNAME NOT FOUND IN %s\n", HOSTNAME_FILE);
87     exit(77);
88 }
89 
90 #define DONE
91 #endif
92 
93 #endif
94 
95 #ifndef DONE
96 
97 #ifdef HOSTNAME_WHOAMI
98 /*
99  *	Hostname is found in whoami.h
100  */
101 
102 void
nn_gethostname(char * name,int length)103 nn_gethostname(char *name, int length)
104 {
105     FILE           *f;
106     char            buf[512];
107     register char  *p, *q;
108 
109     f = fopen("/usr/include/whoami.h", "r");
110     if (f == NULL)
111 	goto err;
112 
113     while (fgets(buf, 512, f) != NULL) {
114 	if (buf[0] != '#')
115 	    continue;
116 	if ((p = strchr(buf, '"')) == NULL)
117 	    continue;
118 	*p++ = NUL;
119 	if (strncmp(buf, "#define sysname", 15))
120 	    continue;
121 	if ((q = strchr(p, '"')) == NULL)
122 	    break;
123 	*q = NUL;
124 	strncpy(name, p, length);
125 	return;
126     }
127 
128 err:
129     fprintf(stderr, "HOSTNAME (sysname) NOT DEFINED IN whoami.h\n");
130     exit(77);
131 }
132 
133 #define DONE
134 #endif
135 
136 #endif
137 
138 #ifndef DONE
139 
140 #ifdef HOSTNAME
141 void
nn_gethostname(char * name,int length)142 nn_gethostname(char *name, int length)
143 {
144     strncpy(name, HOSTNAME, length);
145 }
146 
147 #define DONE
148 #endif
149 
150 #endif
151 
152 #ifndef DONE
153 YOU LOSE ON     GETHOSTNAME-- DEFINE HOSTNAME IN CONFIG.H
154 #endif
155