/* gethostname -- for now, fake by looking in environment */
/* (written by Eric R. Smith, placed in the public domain) */

#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#define MAXLEN 127

int
gethostname(buf, len)
	char *buf;
	size_t len;
{
	char *foo;
	char xbuf[MAXLEN+1];
	int fd, r;

	foo = getenv("HOSTNAME");
	if (!foo) {
/* try looking for the file /local/hostname; if it's present,
 * it contains the name, otherwise we punt
 */
		fd = open("/local/hostname", O_RDONLY);
		if (fd >= 0) {
			r = read(fd, xbuf, MAXLEN);
			if (r > 0) {
				xbuf[r] = 0;
				foo = xbuf;
				while (*foo) {
					if (*foo == '\r' || *foo == '\n')
						*foo = 0;
					foo++;
				}
				foo = xbuf;
			}
			close(fd);
		}
	}
	if (!foo)
		foo = "unknown";
	strncpy(buf, foo, len);
	return 0;
}
