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 int bu_which(char *argv[], int lim, const char *cmd)
00071 {
00072 char *PATH = {0};
00073 int free_path = 0;
00074 char *curr_path;
00075 int max_length;
00076
00077 char *directory;
00078 char *fullname = {0};
00079
00080 int found_it;
00081 int found_count = 0;
00082
00083 int i;
00084
00085 if (!cmd) {
00086 return 0;
00087 }
00088
00089 if (lim <= 0) {
00090 return 0;
00091 }
00092
00093 if (!argv) {
00094 bu_bomb("bu_which was given a null array?\n");
00095 }
00096
00097 if (bu_file_exists(cmd)) {
00098
00099 argv[found_count] = bu_malloc(strlen(cmd) * sizeof(char) + 1, "bu_which argv entry");
00100 strncpy(argv[found_count], cmd, strlen(cmd) + 1);
00101 found_count++;
00102 }
00103
00104
00105 #ifdef HAVE_GETENV
00106 # define bu_which_found_path 1
00107 PATH = getenv("PATH");
00108 goto found_path;
00109 #endif
00110
00111
00112 #if defined(HAVE_SYSCTL) && defined(CTL_USER) && defined(USER_CS_PATH)
00113 # define bu_which_found_path 1
00114 {
00115 int mib[2];
00116 size_t len;
00117 mib[0] = CTL_USER;
00118 mib[1] = USER_CS_PATH;
00119
00120 if (sysctl(mib, 2, NULL, &len, NULL, 0) != 0) {
00121 perror("sysctl unable to read user.cs_path");
00122 return 0;
00123 }
00124 if (len > 0) {
00125 PATH = bu_calloc(len, sizeof(char), "bu_which PATH");
00126 free_path = 1;
00127 }
00128 if (sysctl(mib, 2, PATH, &len, NULL, 0) != 0) {
00129 perror("sysctl unable to get user.cs_path");
00130 return 0;
00131 }
00132 goto found_path;
00133 }
00134 #endif
00135
00136
00137 #ifndef bu_which_found_path
00138 # error "Do not know how to read the PATH environment variable on this system"
00139 #endif
00140
00141 found_path:
00142
00143 if (!PATH) {
00144
00145 bu_log("Unable to read the environment PATH\n");
00146 return 0;
00147 }
00148
00149
00150 max_length = strlen(PATH) + strlen(cmd) + 1;
00151 fullname = (char *)bu_calloc(max_length+1, sizeof(char), "bu_which fullname");
00152
00153
00154 for (curr_path = PATH; ; *curr_path++ = BU_PATH_SEPARATOR) {
00155 directory = curr_path;
00156
00157 if ((curr_path = strchr(curr_path, BU_PATH_SEPARATOR)) != NULL) {
00158 *curr_path = '\0';
00159
00160
00161 if (directory == curr_path) {
00162 directory = ".";
00163 }
00164 } else {
00165
00166 if (strlen(directory) == 0) {
00167 directory = ".";
00168 }
00169 }
00170
00171 (void)snprintf(fullname, max_length, "%s/%s", directory, cmd);
00172
00173 if (bu_file_exists(fullname)) {
00174 found_it = 0;
00175
00176
00177 for (i = 0; i < found_count; i++) {
00178 if (strncmp(argv[i], fullname, max_length) == 0) {
00179 found_it = 1;
00180 break;
00181 }
00182 }
00183
00184
00185 if (!found_it) {
00186 argv[found_count] = bu_malloc(max_length, "bu_which argv entry");
00187 strncpy(argv[found_count], fullname, max_length);
00188 found_count++;
00189 }
00190 }
00191
00192 if (!curr_path) {
00193 break;
00194 }
00195 if (found_count >= lim) {
00196 break;
00197 }
00198 }
00199
00200 bu_free(fullname, "bu_which fullname");
00201 fullname = NULL;
00202
00203
00204 #ifdef HAVE_SYSCTL
00205 if (free_path && PATH) {
00206 bu_free(PATH, "bu_which PATH");
00207 PATH = NULL;
00208 }
00209 #endif
00210
00211 return found_count;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222