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