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
|
/* hostname.c from init.c (c) copyright 1986 (Dan Heller) */
#include "mush.h"
#include <pwd.h>
#if defined(BSD) || defined(HPUX) || defined(IRIX4) || defined(__linux__)
#include <netdb.h>
#endif /* BSD || HPUX || IRIX4 || __linux__ */
#if defined(SYSV) && !defined(HPUX) && !defined(IRIX4)
#include <sys/utsname.h>
#endif /* SYSV && !HPUX && !IRIX4 */
void
init_host()
{
#if defined(SYSV) && !defined(HPUX) && !defined(IRIX4) && !defined(__linux__)
struct utsname ourhost;
#else
char ourhost[128];
#endif /* SYSV && !HPUX && !IRIX4 */
register const char *p;
struct passwd *entry;
int cnt;
#if defined(BSD) || defined(HPUX) || defined(IRIX4) || defined(__linux__)
struct hostent *hp;
#endif /* BSD || HPUX || IRIX4 */
#if defined(BSD) || defined(HPUX) || defined(IRIX4) || defined(__linux__)
(void) gethostname(ourhost, sizeof ourhost);
if (!(hp = gethostbyname(ourhost))) {
if (ourname = (char **)calloc((unsigned)2, sizeof (char *)))
strdup(ourname[0], ourhost);
} else {
int n = -1;
cnt = 2; /* 1 for ourhost and 1 for NULL terminator */
for (p = hp->h_name; p && *p; p = hp->h_aliases[++n])
if (strcmp(ourhost, p)) /* if host name is different */
cnt++;
if (ourname = (char **)malloc((unsigned)cnt * sizeof (char *))) {
n = -1;
cnt = 0;
ourname[cnt++] = savestr(ourhost);
for (p = hp->h_name; p && *p; p = hp->h_aliases[++n])
if (strcmp(ourhost, p)) /* if host name is different */
ourname[cnt++] = savestr(p);
ourname[cnt++] = NULL;
}
}
#else
#ifdef SYSV
if (ourname = (char **)calloc((unsigned)2, sizeof (char *))) {
if ((uname (&ourhost) >= 0) && (*ourhost.nodename))
ourname[0] = savestr(ourhost.nodename);
else {
/* Try to use uuname -l to get host's name if uname didn't work */
char buff[50];
char *p;
FILE *F;
if (F = popen("exec uuname -l", "r")) {
if ((fgets(buff, sizeof buff, F) == buff) &&
(p = strchr(buff, '\n'))) {
*p = '\0'; /* eliminate newline */
ourname[0] = savestr (buff);
}
(void)pclose(F);
}
}
}
#endif /* SYSV */
#endif /* BSD || HPUX || IRIX4 */
}
|