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
|
/*
* (c) Copyright 1990, Kim Fabricius Storm. All rights reserved.
* Copyright (c) 1996-2005 Michael T Pins. All rights reserved.
*
* "generic" gethostname() emulation:
*
* Possibilities are used in following order:
*
* HAVE_GETHOSTNAME -- use gethostname()
* HAVE_UNAME -- use sysV uname().nodename
* HOSTNAME_FILE "/.../..." -- read hostname from file
* HOSTNAME_WHOAMI -- use sysname from whoami.h
* HOSTNAME "host" -- hard-coded hostname
* You lose!
*/
#include <unistd.h>
#include <string.h>
#include "config.h"
#undef DONE
#ifdef HAVE_GETHOSTNAME
/*
* Easy -- we already got it
*/
void
nn_gethostname(char *name, int length)
{
gethostname(name, length);
}
#define DONE
#endif
#ifndef DONE
#ifdef HAVE_UNAME
/*
* System V uname() is available. Use nodename field.
*/
#include <sys/utsname.h>
void
nn_gethostname(char *name, int length)
{
struct utsname un;
uname(&un);
strncpy(name, un.nodename, length);
}
#define DONE
#endif
#endif
#ifndef DONE
#ifdef HOSTNAME_FILE
/*
* Hostname is available in a file.
* The name of the file is defined in HOSTNAME_FILE.
* This is not intended to fail (or exit would have been via nn_exit)
*/
void
nn_gethostname(char *name, int length)
{
FILE *f;
register char *p;
f = fopen(HOSTNAME_FILE, "r"); /* Generic code -- don't use
* open_file */
if (f == NULL)
goto err;
if (fgets(name, length, f) == NULL)
goto err;
if ((p = strchr(name, NL)) != NULL)
*p = NUL;
fclose(f);
return;
err:
fprintf(stderr, "HOSTNAME NOT FOUND IN %s\n", HOSTNAME_FILE);
exit(77);
}
#define DONE
#endif
#endif
#ifndef DONE
#ifdef HOSTNAME_WHOAMI
/*
* Hostname is found in whoami.h
*/
void
nn_gethostname(char *name, int length)
{
FILE *f;
char buf[512];
register char *p, *q;
f = fopen("/usr/include/whoami.h", "r");
if (f == NULL)
goto err;
while (fgets(buf, 512, f) != NULL) {
if (buf[0] != '#')
continue;
if ((p = strchr(buf, '"')) == NULL)
continue;
*p++ = NUL;
if (strncmp(buf, "#define sysname", 15))
continue;
if ((q = strchr(p, '"')) == NULL)
break;
*q = NUL;
strncpy(name, p, length);
return;
}
err:
fprintf(stderr, "HOSTNAME (sysname) NOT DEFINED IN whoami.h\n");
exit(77);
}
#define DONE
#endif
#endif
#ifndef DONE
#ifdef HOSTNAME
void
nn_gethostname(char *name, int length)
{
strncpy(name, HOSTNAME, length);
}
#define DONE
#endif
#endif
#ifndef DONE
YOU LOSE ON GETHOSTNAME-- DEFINE HOSTNAME IN CONFIG.H
#endif
|