xref: /netbsd-src/external/bsd/ntp/dist/libntp/findconfig.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: findconfig.c,v 1.5 2020/05/25 20:47:24 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel 
4abb0f93cSkardel #ifdef HAVE_CONFIG_H
5abb0f93cSkardel # include <config.h>
6abb0f93cSkardel #endif
7abb0f93cSkardel 
8abb0f93cSkardel #ifdef NEED_HPUX_FINDCONFIG
9abb0f93cSkardel #include <string.h>
10abb0f93cSkardel #include <stdio.h>
11abb0f93cSkardel #include <sys/types.h>
12abb0f93cSkardel #include <sys/stat.h>
13abb0f93cSkardel #include <sys/utsname.h>
14abb0f93cSkardel #include <unistd.h>
15abb0f93cSkardel 
16abb0f93cSkardel const char *
FindConfig(const char * base)17abb0f93cSkardel FindConfig(
18abb0f93cSkardel 	const char *base
19abb0f93cSkardel 	)
20abb0f93cSkardel {
21abb0f93cSkardel 	static char result[BUFSIZ];
22abb0f93cSkardel 	char hostname[BUFSIZ], *cp;
23abb0f93cSkardel 	struct stat sbuf;
24abb0f93cSkardel 	struct utsname unamebuf;
25abb0f93cSkardel 
26abb0f93cSkardel 	/* All keyed by initial target being a directory */
278585484eSchristos 	strlcpy(result, base, sizeof(result));
28abb0f93cSkardel 	if (stat(result, &sbuf) == 0) {
29abb0f93cSkardel 		if (S_ISDIR(sbuf.st_mode)) {
30abb0f93cSkardel 
31abb0f93cSkardel 			/* First choice is my hostname */
32abb0f93cSkardel 			if (gethostname(hostname, BUFSIZ) >= 0) {
33f003fb54Skardel 				snprintf(result, sizeof(result), "%s/%s", base, hostname);
34abb0f93cSkardel 				if (stat(result, &sbuf) == 0) {
35abb0f93cSkardel 					goto outahere;
36abb0f93cSkardel 				} else {
37abb0f93cSkardel 
38abb0f93cSkardel 					/* Second choice is of form default.835 */
39abb0f93cSkardel 					(void) uname(&unamebuf);
40abb0f93cSkardel 					if (strncmp(unamebuf.machine, "9000/", 5) == 0)
41abb0f93cSkardel 					    cp = unamebuf.machine + 5;
42abb0f93cSkardel 					else
43abb0f93cSkardel 					    cp = unamebuf.machine;
44f003fb54Skardel 					snprintf(result, sizeof(result), "%s/default.%s", base, cp);
45abb0f93cSkardel 					if (stat(result, &sbuf) == 0) {
46abb0f93cSkardel 						goto outahere;
47abb0f93cSkardel 					} else {
48abb0f93cSkardel 
49abb0f93cSkardel 						/* Last choice is just default */
50f003fb54Skardel 						snprintf(result, sizeof(result), "%s/default", base);
51abb0f93cSkardel 						if (stat(result, &sbuf) == 0) {
52abb0f93cSkardel 							goto outahere;
53abb0f93cSkardel 						} else {
548585484eSchristos 							strlcpy(result,
558585484eSchristos 								"/not/found",
568585484eSchristos 								sizeof(result));
57abb0f93cSkardel 						}
58abb0f93cSkardel 					}
59abb0f93cSkardel 				}
60abb0f93cSkardel 			}
61abb0f93cSkardel 		}
62abb0f93cSkardel 	}
63abb0f93cSkardel     outahere:
64abb0f93cSkardel 	return(result);
65abb0f93cSkardel }
66abb0f93cSkardel #else
67abb0f93cSkardel #include "ntp_stdlib.h"
68abb0f93cSkardel 
69abb0f93cSkardel const char *
FindConfig(const char * base)70abb0f93cSkardel FindConfig(
71abb0f93cSkardel 	const char *base
72abb0f93cSkardel 	)
73abb0f93cSkardel {
74abb0f93cSkardel 	return base;
75abb0f93cSkardel }
76abb0f93cSkardel #endif
77