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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/*
* Revision Control Information
*
* /projects/hsis/CVS/utilities/util/pathsearch.c,v
* rajeev
* 1.3
* 1995/08/08 22:41:24
*
*/
/* LINTLIBRARY */
#if HAVE_SYS_FILE_H
# include <sys/file.h>
#endif
#if HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#include "util.h"
/**Function********************************************************************
Synopsis [ Check that a given file is present and accessible ]
SideEffects [none]
******************************************************************************/
static int
check_file(filename, mode)
char *filename;
char *mode;
{
#if defined(HAVE_SYS_STAT_H)
struct stat stat_rec;
int access_char = mode[0];
int access_mode = R_OK;
/* First check that the file is a regular file. */
if (stat(filename,&stat_rec) == 0
&& (stat_rec.st_mode&S_IFMT) == S_IFREG) {
if (access_char == 'w') {
access_mode = W_OK;
} else if (access_char == 'x') {
access_mode = X_OK;
}
return access(filename,access_mode) == 0;
}
return 0;
#else
FILE *fp;
int got_file;
if (strcmp(mode, "x") == 0) {
mode = "r";
}
fp = fopen(filename, mode);
got_file = (fp != 0);
if (fp != 0) {
(void) fclose(fp);
}
return got_file;
#endif
}
/**Function********************************************************************
Synopsis [ Search for a program in all possible paths ]
SideEffects [none]
******************************************************************************/
char *
util_path_search(prog)
char *prog;
{
#ifdef HAVE_GETENV
return util_file_search(prog, getenv("PATH"), "x");
#else
return util_file_search(prog, NIL(char), "x");
#endif
}
char *
util_file_search(file, path, mode)
char *file; /* file we're looking for */
char *path; /* search path, colon separated */
char *mode; /* "r", "w", or "x" */
{
int quit;
char *buffer, *filename, *save_path, *cp;
if (path == 0 || strcmp(path, "") == 0) {
path = "."; /* just look in the current directory */
}
save_path = path = util_strsav(path);
quit = 0;
do {
cp = strchr(path, ':');
if (cp != 0) {
*cp = '\0';
} else {
quit = 1;
}
/* cons up the filename out of the path and file name */
if (strcmp(path, ".") == 0) {
buffer = util_strsav(file);
} else {
buffer = ALLOC(char, strlen(path) + strlen(file) + 4);
(void) sprintf(buffer, "%s/%s", path, file);
}
filename = util_tilde_expand(buffer);
FREE(buffer);
/* see if we can access it */
if (check_file(filename, mode)) {
FREE(save_path);
return filename;
}
FREE(filename);
path = ++cp;
} while (! quit);
FREE(save_path);
return 0;
}
|