| 1 | /* |
|---|
| 2 | * Revision Control Information |
|---|
| 3 | * |
|---|
| 4 | * $Id: pathsearch.c,v 1.1.1.1 2008-11-14 20:40:10 hhkim Exp $ |
|---|
| 5 | * |
|---|
| 6 | */ |
|---|
| 7 | /* LINTLIBRARY */ |
|---|
| 8 | |
|---|
| 9 | #if HAVE_SYS_FILE_H |
|---|
| 10 | # include <sys/file.h> |
|---|
| 11 | #endif |
|---|
| 12 | |
|---|
| 13 | #if HAVE_SYS_STAT_H |
|---|
| 14 | # include <sys/stat.h> |
|---|
| 15 | #endif |
|---|
| 16 | |
|---|
| 17 | #include "util.h" |
|---|
| 18 | |
|---|
| 19 | /**Function******************************************************************** |
|---|
| 20 | |
|---|
| 21 | Synopsis [ Check that a given file is present and accessible ] |
|---|
| 22 | |
|---|
| 23 | SideEffects [none] |
|---|
| 24 | ******************************************************************************/ |
|---|
| 25 | int |
|---|
| 26 | util_check_file(char *filename, char *mode) |
|---|
| 27 | { |
|---|
| 28 | #if defined(HAVE_SYS_STAT_H) |
|---|
| 29 | struct stat stat_rec; |
|---|
| 30 | int access_char = mode[0]; |
|---|
| 31 | int access_mode = R_OK; |
|---|
| 32 | |
|---|
| 33 | /* First check that the file is a regular file. */ |
|---|
| 34 | |
|---|
| 35 | if (stat(filename,&stat_rec) == 0 && S_ISREG(stat_rec.st_mode)) { |
|---|
| 36 | if (access_char == 'w') { |
|---|
| 37 | access_mode = W_OK; |
|---|
| 38 | } else if (access_char == 'x') { |
|---|
| 39 | access_mode = X_OK; |
|---|
| 40 | } |
|---|
| 41 | return access(filename,access_mode) == 0; |
|---|
| 42 | } |
|---|
| 43 | return 0; |
|---|
| 44 | |
|---|
| 45 | #else |
|---|
| 46 | |
|---|
| 47 | FILE *fp; |
|---|
| 48 | int got_file; |
|---|
| 49 | |
|---|
| 50 | if (strcmp(mode, "x") == 0) { |
|---|
| 51 | mode = "r"; |
|---|
| 52 | } |
|---|
| 53 | fp = fopen(filename, mode); |
|---|
| 54 | got_file = (fp != 0); |
|---|
| 55 | if (fp != 0) { |
|---|
| 56 | (void) fclose(fp); |
|---|
| 57 | } |
|---|
| 58 | return got_file; |
|---|
| 59 | |
|---|
| 60 | #endif |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /**Function******************************************************************** |
|---|
| 64 | |
|---|
| 65 | Synopsis [ Search for a program in all possible paths ] |
|---|
| 66 | |
|---|
| 67 | SideEffects [none] |
|---|
| 68 | |
|---|
| 69 | ******************************************************************************/ |
|---|
| 70 | #if 0 |
|---|
| 71 | char * |
|---|
| 72 | util_path_search(char *prog) |
|---|
| 73 | { |
|---|
| 74 | #ifdef HAVE_GETENV |
|---|
| 75 | return util_file_search(prog, getenv("PATH"), "x"); |
|---|
| 76 | #else |
|---|
| 77 | return util_file_search(prog, NIL(char), "x"); |
|---|
| 78 | #endif |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | char * |
|---|
| 82 | util_file_search( |
|---|
| 83 | char *file /* file we're looking for */, |
|---|
| 84 | char *path /* search path, colon separated */, |
|---|
| 85 | char *mode /* "r", "w", or "x" */) |
|---|
| 86 | { |
|---|
| 87 | int quit; |
|---|
| 88 | char *buffer, *filename, *save_path, *cp; |
|---|
| 89 | |
|---|
| 90 | if (path == 0 || strcmp(path, "") == 0) { |
|---|
| 91 | path = "."; /* just look in the current directory */ |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | save_path = path = util_strsav(path); |
|---|
| 95 | quit = 0; |
|---|
| 96 | do { |
|---|
| 97 | cp = strchr(path, ':'); |
|---|
| 98 | if (cp != 0) { |
|---|
| 99 | *cp = '\0'; |
|---|
| 100 | } else { |
|---|
| 101 | quit = 1; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /* cons up the filename out of the path and file name */ |
|---|
| 105 | if (strcmp(path, ".") == 0) { |
|---|
| 106 | buffer = util_strsav(file); |
|---|
| 107 | } else { |
|---|
| 108 | buffer = ALLOC(char, strlen(path) + strlen(file) + 4); |
|---|
| 109 | (void) sprintf(buffer, "%s/%s", path, file); |
|---|
| 110 | } |
|---|
| 111 | filename = util_tilde_expand(buffer); |
|---|
| 112 | FREE(buffer); |
|---|
| 113 | |
|---|
| 114 | /* see if we can access it */ |
|---|
| 115 | if (util_check_file(filename, mode)) { |
|---|
| 116 | FREE(save_path); |
|---|
| 117 | return filename; |
|---|
| 118 | } |
|---|
| 119 | FREE(filename); |
|---|
| 120 | path = ++cp; |
|---|
| 121 | } while (! quit); |
|---|
| 122 | |
|---|
| 123 | FREE(save_path); |
|---|
| 124 | return 0; |
|---|
| 125 | } |
|---|
| 126 | #endif |
|---|