1ef64b99roberto
2ef64b99roberto#ifdef HAVE_CONFIG_H
3ef64b99roberto# include <config.h>
4ef64b99roberto#endif
5ef64b99roberto
6ef64b99roberto#ifdef NEED_HPUX_FINDCONFIG
7ef64b99roberto#include <string.h>
8ef64b99roberto#include <stdio.h>
9ef64b99roberto#include <sys/types.h>
10ef64b99roberto#include <sys/stat.h>
11ef64b99roberto#include <sys/utsname.h>
12ef64b99roberto#include <unistd.h>
13ef64b99roberto
14ef64b99robertoconst char *
15ef64b99robertoFindConfig(
16ef64b99roberto	const char *base
17ef64b99roberto	)
18ef64b99roberto{
19ef64b99roberto	static char result[BUFSIZ];
20ef64b99roberto	char hostname[BUFSIZ], *cp;
21ef64b99roberto	struct stat sbuf;
22ef64b99roberto	struct utsname unamebuf;
23ef64b99roberto
24ef64b99roberto	/* All keyed by initial target being a directory */
25047f369cy	strlcpy(result, base, sizeof(result));
26ef64b99roberto	if (stat(result, &sbuf) == 0) {
27ef64b99roberto		if (S_ISDIR(sbuf.st_mode)) {
28ef64b99roberto
29ef64b99roberto			/* First choice is my hostname */
30ef64b99roberto			if (gethostname(hostname, BUFSIZ) >= 0) {
31d54cfbdroberto				snprintf(result, sizeof(result), "%s/%s", base, hostname);
32ef64b99roberto				if (stat(result, &sbuf) == 0) {
33ef64b99roberto					goto outahere;
34ef64b99roberto				} else {
35ef64b99roberto
36ef64b99roberto					/* Second choice is of form default.835 */
37ef64b99roberto					(void) uname(&unamebuf);
38ef64b99roberto					if (strncmp(unamebuf.machine, "9000/", 5) == 0)
39ef64b99roberto					    cp = unamebuf.machine + 5;
40ef64b99roberto					else
41ef64b99roberto					    cp = unamebuf.machine;
42d54cfbdroberto					snprintf(result, sizeof(result), "%s/default.%s", base, cp);
43ef64b99roberto					if (stat(result, &sbuf) == 0) {
44ef64b99roberto						goto outahere;
45ef64b99roberto					} else {
46ef64b99roberto
47ef64b99roberto						/* Last choice is just default */
48d54cfbdroberto						snprintf(result, sizeof(result), "%s/default", base);
49ef64b99roberto						if (stat(result, &sbuf) == 0) {
50ef64b99roberto							goto outahere;
51ef64b99roberto						} else {
52047f369cy							strlcpy(result,
53047f369cy								"/not/found",
54047f369cy								sizeof(result));
55ef64b99roberto						}
56ef64b99roberto					}
57ef64b99roberto				}
58ef64b99roberto			}
59ef64b99roberto		}
60ef64b99roberto	}
61ef64b99roberto    outahere:
62ef64b99roberto	return(result);
63ef64b99roberto}
64ef64b99roberto#else
65ef64b99roberto#include "ntp_stdlib.h"
66ef64b99roberto
67ef64b99robertoconst char *
68ef64b99robertoFindConfig(
69ef64b99roberto	const char *base
70ef64b99roberto	)
71ef64b99roberto{
72ef64b99roberto	return base;
73ef64b99roberto}
74ef64b99roberto#endif
75