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