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