00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "common.h"
00039
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include <string.h>
00043 #ifdef HAVE_SYS_TYPES_H
00044 # include <sys/types.h>
00045 #endif
00046 #ifdef HAVE_SYS_SYSCTL_H
00047 # ifdef HAVE_SYS_PARAM_H
00048 # include <sys/param.h>
00049 # endif
00050 # include <sys/sysctl.h>
00051 #endif
00052
00053 #include "machine.h"
00054 #include "bu.h"
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 int bu_whereis(char *argv[], int lim, const char *cmd)
00072 {
00073 char *PATH = {0};
00074 int free_path = 0;
00075 char *curr_path;
00076 int max_length;
00077
00078 char *directory;
00079 char *fullname = {0};
00080
00081 int found_it;
00082 int found_count = 0;
00083
00084 int i;
00085
00086 if (!cmd) {
00087 return 0;
00088 }
00089
00090 if (lim <= 0) {
00091 return 0;
00092 }
00093
00094 if (!argv) {
00095 bu_bomb("bu_whereis was given a null array?\n");
00096 }
00097
00098
00099
00100 #if defined(HAVE_SYSCTL) && defined(CTL_USER) && defined(USER_CS_PATH)
00101 # define bu_whereis_found_path 1
00102 {
00103 int mib[2];
00104 size_t len;
00105 mib[0] = CTL_USER;
00106 mib[1] = USER_CS_PATH;
00107
00108 if (sysctl(mib, 2, NULL, &len, NULL, 0) != 0) {
00109 perror("sysctl unable to read user.cs_path");
00110 return 0;
00111 }
00112 if (len > 0) {
00113 PATH = bu_calloc(len, sizeof(char), "bu_whereis PATH");
00114 free_path = 1;
00115 }
00116 if (sysctl(mib, 2, PATH, &len, NULL, 0) != 0) {
00117 perror("sysctl unable to get user.cs_path");
00118 return 0;
00119 }
00120 goto found_path;
00121 }
00122 #endif
00123
00124
00125 #ifdef HAVE_GETENV
00126 # define bu_whereis_found_path 1
00127 PATH = getenv("PATH");
00128 goto found_path;
00129 #endif
00130
00131
00132 #ifndef bu_whereis_found_path
00133 # error "Do not know how to read the PATH environment variable on this system"
00134 #endif
00135
00136 found_path:
00137
00138 if (!PATH) {
00139
00140 bu_log("Unable to read the environment PATH\n");
00141 return 0;
00142 }
00143
00144
00145
00146 max_length = strlen(PATH) + strlen(cmd) + 1;
00147 fullname = (char *)bu_calloc(max_length+1, sizeof(char), "bu_whereis fullname");
00148
00149
00150 for (curr_path = PATH; ; *curr_path++ = BU_PATH_SEPARATOR) {
00151 directory = curr_path;
00152
00153 if ((curr_path = strchr(curr_path, BU_PATH_SEPARATOR)) != NULL) {
00154 *curr_path = '\0';
00155
00156
00157 if (directory == curr_path) {
00158 directory = ".";
00159 }
00160 } else {
00161
00162 if (strlen(directory) == 0) {
00163 directory = ".";
00164 }
00165 }
00166
00167 (void)snprintf(fullname, max_length, "%s/%s", directory, cmd);
00168
00169 if (bu_file_exists(fullname)) {
00170 found_it = 0;
00171
00172
00173 for (i = 0; i < found_count; i++) {
00174 if (strncmp(argv[i], fullname, max_length) == 0) {
00175 found_it = 1;
00176 break;
00177 }
00178 }
00179
00180
00181 if (!found_it) {
00182 argv[found_count] = bu_malloc(max_length+1, "bu_whereis argv entry");
00183 strncpy(argv[found_count], fullname, max_length);
00184 found_count++;
00185 }
00186 }
00187
00188 if (!curr_path) {
00189 break;
00190 }
00191 if (found_count >= lim) {
00192 break;
00193 }
00194 }
00195
00196 bu_free(fullname, "bu_whereis fullname");
00197 fullname = NULL;
00198
00199
00200 #ifdef HAVE_SYSCTL
00201 if (free_path && PATH) {
00202 bu_free(PATH, "bu_whereis PATH");
00203 PATH = NULL;
00204 }
00205 #endif
00206
00207 return found_count;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219