-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhostname_u.c
215 lines (205 loc) · 4.57 KB
/
hostname_u.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* hostname - toolbox
Copyright 2015-2022 Rivoreo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
*/
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#ifdef _WIN32
#include <windows.h>
#include <winsock2.h>
#define HOST_NAME_MAX 16
static int _sethostname(const char *name, size_t len) {
if(len > HOST_NAME_MAX) {
//errno = EINVAL;
errno = ENAMETOOLONG;
return -1;
}
#ifdef _WIN32_WCE
/* The Winsock2 for Windows CE have a sethostname implementation
* but quite incompatible from the BSD sethostname(3) */
extern size_t strnlen(const char *, size_t);
size_t name_len = strnlen(name, len);
if(!name_len) {
errno = EINVAL;
return -1;
}
if(len > name_len) len = name_len;
char buffer[len + 1];
memcpy(buffer, name, len);
buffer[len] = 0;
return sethostname(buffer, len + 1);
#else
#if 0
char buffer[len + 1];
memcpy(buffer, name, len)[len] = 0;
return SetComputerNameA(buffer) ? 0 : -1;
#else
wchar_t buffer[len + 1];
int wlen = mbstowcs(buffer, name, len);
if(wlen < 0) {
errno = EINVAL;
return -1;
}
buffer[wlen] = 0;
return SetComputerNameW(buffer) ? 0 : -1;
#endif
#endif
}
#define sethostname _sethostname
#else
#include <sys/param.h>
#ifdef __SVR4
#include <netdb.h>
#ifdef __sun
extern int sethostname(char *, int);
#endif
#endif /* __SVR4 */
#ifndef HOST_NAME_MAX
//#if defined _SC_HOST_NAME_MAX && defined _POSIX_HOST_NAME_MAX && _POSIX_HOST_NAME_MAX != (-1)
#if 0
#define HOST_NAME_MAX (sysconf(_SC_HOST_NAME_MAX) > 0 ? : _POSIX_HOST_NAME_MAX)
#elif defined MAXHOSTNAMELEN
#define HOST_NAME_MAX (MAXHOSTNAMELEN-1)
#elif defined _POSIX_HOST_NAME_MAX && _POSIX_HOST_NAME_MAX != (-1)
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
#else
#define HOST_NAME_MAX 16
#endif
#endif /* !HOST_NAME_MAX */
#endif
static void print_usage(const char *name) {
fprintf(stderr, "Usage:\n"
" %s [-f|-s]\n"
" %s {-F|--file} <file>\n"
" %s <hostname>\n\n",
name, name, name);
}
static char *get_name_from_file(const char *filename) {
static char buffer[HOST_NAME_MAX+1];
int fd = open(filename, O_RDONLY);
if(fd == -1) return NULL;
while(1) {
char *p = buffer;
int s = read(fd, buffer, HOST_NAME_MAX);
if(s < 0) {
int e = errno;
close(fd);
errno = e;
return NULL;
}
buffer[s] = 0;
/* Support comment */
if(*p == '#') {
while(*++p != '\n') if(!*p) {
close(fd);
return p;
}
p++;
if(s == HOST_NAME_MAX) {
lseek(fd, p - buffer, SEEK_SET);
continue;
}
}
char *pp = p;
while(*pp) {
/* End of the hostname */
if(*pp == '\n') {
*pp = 0;
if(pp != buffer && pp[-1] == '\r') pp[-1] = 0;
break;
}
pp++;
}
close(fd);
return p;
}
}
int hostname_main(int argc, char **argv) {
static struct option long_options[3] = {
{ "file", 1, NULL, 'F' },
{ "help", 0, NULL, 'h' }
};
const char *filename = NULL;
int short_name = 0;
while(1) {
int c = getopt_long(argc, argv, "F:fsh", long_options, NULL);
if(c == -1) break;
switch(c) {
case 'F':
filename = optarg;
break;
case 'f':
short_name = 0;
break;
case 's':
short_name = 1;
break;
case 'h':
print_usage(argv[0]);
return 0;
case '?':
return -1;
}
}
/* If user input excess arguments */
if(argc > optind + !filename) {
print_usage(argv[0]);
return -1;
}
/* Get user input hostname */
char *hostname = argv[optind];
if(!hostname) {
if(filename) {
hostname = get_name_from_file(filename);
if(!hostname) {
perror(filename);
return 1;
}
} else {
// Just print the current host name
char hostname[HOST_NAME_MAX+1];
#if defined _WIN32 && !defined _WIN32_WCE
WSADATA d;
WSAStartup(MAKEWORD(2, 2), &d);
#endif
if(gethostname(hostname, sizeof hostname) < 0) {
perror("gethostname");
return 1;
}
if(short_name) {
char *p = hostname;
while(*p) {
if(*p == '.') {
*p = 0;
break;
}
p++;
}
}
puts(hostname);
return 0;
}
}
#ifdef __INTERIX
fprintf(stderr, "%s: %s\n", argv[0], strerror(ENOSYS));
return 1;
#else
if(sethostname(hostname, strlen(hostname)) < 0) {
perror("sethostname");
return 1;
}
return 0;
#endif
}