summaryrefslogtreecommitdiff
path: root/libntp/findconfig.c
blob: 1785ff700395b5d751b3b987e156aedc4bb8377f (plain)
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

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef NEED_HPUX_FINDCONFIG
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>

const char *
FindConfig(
	const char *base
	)
{
	static char result[BUFSIZ];
	char hostname[BUFSIZ], *cp;
	struct stat sbuf; 
	struct utsname unamebuf; 

	/* All keyed by initial target being a directory */
	strlcpy(result, base, sizeof(result));
	if (stat(result, &sbuf) == 0) {
		if (S_ISDIR(sbuf.st_mode)) {

			/* First choice is my hostname */
			if (gethostname(hostname, BUFSIZ) >= 0) {
				snprintf(result, sizeof(result), "%s/%s", base, hostname);
				if (stat(result, &sbuf) == 0) {
					goto outahere;
				} else {

					/* Second choice is of form default.835 */
					(void) uname(&unamebuf);
					if (strncmp(unamebuf.machine, "9000/", 5) == 0)
					    cp = unamebuf.machine + 5;
					else
					    cp = unamebuf.machine;
					snprintf(result, sizeof(result), "%s/default.%s", base, cp);
					if (stat(result, &sbuf) == 0) {
						goto outahere;
					} else {

						/* Last choice is just default */
						snprintf(result, sizeof(result), "%s/default", base);
						if (stat(result, &sbuf) == 0) {
							goto outahere;
						} else {
							strlcpy(result,
								"/not/found",
								sizeof(result));
						}
					}
				}
			} 
		} 
	}
    outahere:
	return(result);
}
#else
#include "ntp_stdlib.h"

const char *
FindConfig(
	const char *base
	)
{
	return base;
}
#endif