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
|
/* ISC license. */
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <limits.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/bytestr.h>
#include <skalibs/strerr2.h>
#include "hpr.h"
#define PROMPT "Please enter the machine's hostname: "
void hpr_confirm_hostname (void)
{
char *p ;
ssize_t r ;
long hostnamemax = sysconf(_SC_HOST_NAME_MAX) ;
if (hostnamemax < 0) hostnamemax = _POSIX_HOST_NAME_MAX ;
char name[hostnamemax + 1] ;
char buf[hostnamemax + 1] ;
if (!isatty(0) || !isatty(1))
strerr_diefu1x(100, "ask hostname confirmation: stdin or stdout is not a tty") ;
if (gethostname(name, hostnamemax) < 0)
strerr_diefu1sys(111, "get host name") ;
name[hostnamemax] = 0 ;
p = strchr(name, '.') ;
if (p) *p = 0 ;
if (allwrite(1, PROMPT, sizeof(PROMPT)-1) < 0)
strerr_diefu1sys(111, "write to stdout") ;
if (tcdrain(1) < 0)
strerr_diefu1sys(111, "tcdrain stdout") ;
if (tcflush(0, TCIFLUSH) < 0)
strerr_diefu1sys(111, "empty stdin buffer") ;
r = fd_read(0, buf, hostnamemax) ;
if (!r) errno = EPIPE ;
if (r <= 0) strerr_diefu1sys(111, "read from stdin") ;
buf[byte_chr(buf, r, '\n')] = 0 ;
p = strchr(buf, '.') ;
if (p) *p = 0 ;
if (strcasecmp(name, buf))
strerr_dief2x(1, "hostname mismatch: expecting ", name) ;
}
|