6 static int check_file (char const *, char const *);
9 util_path_search(char const *prog
)
12 return util_file_search(prog
, getenv("PATH"), (char *) "x");
14 return util_file_search(prog
, NIL(char), (char *) "x");
21 char const *file
, /* file we're looking for */
22 char *path
, /* search path, colon separated */
23 char const *mode
/* "r", "w", or "x" */)
26 char *buffer
, *filename
, *save_path
, *cp
;
28 if (path
== 0 || strcmp(path
, "") == 0) {
29 path
= (char *) "."; /* just look in the current directory */
32 save_path
= path
= strsav(path
);
35 cp
= strchr(path
, ':');
42 /* cons up the filename out of the path and file name */
43 if (strcmp(path
, ".") == 0) {
44 buffer
= strsav(file
);
46 buffer
= ALLOC(char, strlen(path
) + strlen(file
) + 4);
47 (void) sprintf(buffer
, "%s/%s", path
, file
);
49 filename
= util_tilde_expand(buffer
);
52 /* see if we can access it */
53 if (check_file(filename
, mode
)) {
67 check_file(char const *filename
, char const *mode
)
70 int access_mode
= /*F_OK*/ 0;
72 if (strcmp(mode
, "r") == 0) {
73 access_mode
= /*R_OK*/ 4;
74 } else if (strcmp(mode
, "w") == 0) {
75 access_mode
= /*W_OK*/ 2;
76 } else if (strcmp(mode
, "x") == 0) {
77 access_mode
= /*X_OK*/ 1;
79 return access(filename
, access_mode
) == 0;
84 if (strcmp(mode
, "x") == 0) {
87 fp
= fopen(filename
, mode
);