findconfig.c revision 54360
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
14const char *
15FindConfig(
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	(void) strcpy(result, base);
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				(void) sprintf(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					(void) sprintf(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						(void) sprintf(result, "%s/default", base);
49						if (stat(result, &sbuf) == 0) {
50							goto outahere;
51						} else {
52							(void) strcpy(result, "/not/found");
53						}
54					}
55				}
56			}
57		}
58	}
59    outahere:
60	return(result);
61}
62#else
63#include "ntp_stdlib.h"
64
65const char *
66FindConfig(
67	const char *base
68	)
69{
70	return base;
71}
72#endif
73