1#include <dprintf.h>
2#include <fcntl.h>
3#include <stdio.h>
4#include <string.h>
5#include <core.h>
6#include <fs.h>
7
8__export char ConfigName[FILENAME_MAX];
9__export char config_cwd[FILENAME_MAX];
10
11/*
12 * This searches for a specified set of filenames in a specified set
13 * of directories.  If found, set the current working directory to
14 * match.
15 */
16int search_dirs(struct com32_filedata *filedata,
17		const char *search_directories[],
18		const char *filenames[],
19		char *realname)
20{
21    char namebuf[FILENAME_MAX];
22    const char *sd, **sdp;
23    const char *sf, **sfp;
24
25    for (sdp = search_directories; (sd = *sdp); sdp++) {
26	for (sfp = filenames; (sf = *sfp); sfp++) {
27	    snprintf(namebuf, sizeof namebuf,
28		     "%s%s%s",
29		     sd, (*sd && sd[strlen(sd)-1] == '/') ? "" : "/",
30		     sf);
31	    if (realpath(realname, namebuf, FILENAME_MAX) == (size_t)-1)
32		continue;
33	    dprintf("Config search: %s\n", realname);
34	    if (open_file(realname, O_RDONLY, filedata) >= 0) {
35		chdir(sd);
36		return 0;	/* Got it */
37	    }
38	}
39    }
40
41    return -1;
42}
43