154359Sroberto
254359Sroberto#ifdef HAVE_CONFIG_H
354359Sroberto# include <config.h>
454359Sroberto#endif
554359Sroberto
654359Sroberto#ifdef NEED_HPUX_FINDCONFIG
754359Sroberto#include <string.h>
854359Sroberto#include <stdio.h>
954359Sroberto#include <sys/types.h>
1054359Sroberto#include <sys/stat.h>
1154359Sroberto#include <sys/utsname.h>
1254359Sroberto#include <unistd.h>
1354359Sroberto
1454359Srobertoconst char *
1554359SrobertoFindConfig(
1654359Sroberto	const char *base
1754359Sroberto	)
1854359Sroberto{
1954359Sroberto	static char result[BUFSIZ];
2054359Sroberto	char hostname[BUFSIZ], *cp;
2154359Sroberto	struct stat sbuf;
2254359Sroberto	struct utsname unamebuf;
2354359Sroberto
2454359Sroberto	/* All keyed by initial target being a directory */
25280849Scy	strlcpy(result, base, sizeof(result));
2654359Sroberto	if (stat(result, &sbuf) == 0) {
2754359Sroberto		if (S_ISDIR(sbuf.st_mode)) {
2854359Sroberto
2954359Sroberto			/* First choice is my hostname */
3054359Sroberto			if (gethostname(hostname, BUFSIZ) >= 0) {
31280849Scy				snprintf(result, sizeof(result), "%s/%s", base, hostname);
3254359Sroberto				if (stat(result, &sbuf) == 0) {
3354359Sroberto					goto outahere;
3454359Sroberto				} else {
3554359Sroberto
3654359Sroberto					/* Second choice is of form default.835 */
3754359Sroberto					(void) uname(&unamebuf);
3854359Sroberto					if (strncmp(unamebuf.machine, "9000/", 5) == 0)
3954359Sroberto					    cp = unamebuf.machine + 5;
4054359Sroberto					else
4154359Sroberto					    cp = unamebuf.machine;
42280849Scy					snprintf(result, sizeof(result), "%s/default.%s", base, cp);
4354359Sroberto					if (stat(result, &sbuf) == 0) {
4454359Sroberto						goto outahere;
4554359Sroberto					} else {
4654359Sroberto
4754359Sroberto						/* Last choice is just default */
48280849Scy						snprintf(result, sizeof(result), "%s/default", base);
4954359Sroberto						if (stat(result, &sbuf) == 0) {
5054359Sroberto							goto outahere;
5154359Sroberto						} else {
52280849Scy							strlcpy(result,
53280849Scy								"/not/found",
54280849Scy								sizeof(result));
5554359Sroberto						}
5654359Sroberto					}
5754359Sroberto				}
5854359Sroberto			}
5954359Sroberto		}
6054359Sroberto	}
6154359Sroberto    outahere:
6254359Sroberto	return(result);
6354359Sroberto}
6454359Sroberto#else
6554359Sroberto#include "ntp_stdlib.h"
6654359Sroberto
6754359Srobertoconst char *
6854359SrobertoFindConfig(
6954359Sroberto	const char *base
7054359Sroberto	)
7154359Sroberto{
7254359Sroberto	return base;
7354359Sroberto}
7454359Sroberto#endif
75