1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
/* findfile.c 2.9.0 92/07/06 -- small routine to find a file using a searchpath
(Simplified from earlier version) */
/* Copyright (c) 1991 Damian Cugley -- see file COPYING for details. */
#include <errno.h>
#include "config.h"
#include "strmisc.h" /* includes <string(s).h> */
#include "searchpath.h"
extern int errno;
#define OK_P(FNAME,SUFFIX) \
(access((FNAME), 04) == 0 || \
((SUFFIX) && *(SUFFIX) && access(strcat((FNAME), (SUFFIX)), 04) == 0))
#define TRUE 1
#define FALSE 0
int /* boolean */
findfile(buf, fname, path, suffix)
char *buf;
const char *fname, *path, *suffix;
{
int access ARGS((const char *, int));
const char *p;
char *q;
int ok = FALSE;
/*
* If no file name, give up:
*/
if (!fname || !*fname || !buf) {errno = EINVAL; return FALSE;}
/*
* If no path, or file is absolute, then don't search:
*/
if (!path || *path == '\0' || absolute_path_p(fname))
return OK_P(strcpy(buf, fname), suffix);
q = buf, p = path;
for ( ;; ) /* scan the path */
if ( !(*q++ = *p++) || *(q - 1) == path_sep_ch
#ifdef path_sep_ch2
|| *(q - 1) == path_sep_ch2
#endif
)
{
if (--q != buf && (*(q - 1) != dir_sep_ch
#ifdef dir_sep_ch2
|| *(q - 1) != dir_sep_ch2
#endif
))
*q++ = dir_sep_ch;
/* append / iff nonempty dirname */
(void)strcpy(q, fname);
if ((ok = OK_P(buf, suffix)) || !*(p - 1)) break;
/* if match or end of path then break */
q = buf;
}
return ok;
}
|