| 1 |
/* |
| 2 |
* paths.c - get 'known' pathnames, such as the system's library directory. |
| 3 |
* |
| 4 |
* Copyright (c) 2005-2024 Shiro Kawai <[email protected]> |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
/* This file is used by both libgauche (included from libeval.scm) and |
| 35 |
* gauche-config (included from gauche-config.c). The latter |
| 36 |
* doesn't use ScmObj, so the function works on bare C strings. |
| 37 |
* Note that this also included in libextra.scm for testing, so be careful |
| 38 |
* not to put any public definitions here to avoid duplicate definitions. |
| 39 |
* Do not include this from other files. |
| 40 |
* |
| 41 |
* The includer must define two macros: |
| 42 |
* const void *PATH_ALLOC(size_t size) - allocation routine |
| 43 |
* void PATH_ERROR(const char *fmt, ...) - print error message and exit |
| 44 |
*/ |
| 45 |
|
| 46 |
#define LIBGAUCHE_BODY |
| 47 |
#include <stdio.h> |
| 48 |
#include <string.h> |
| 49 |
#include "gauche.h" |
| 50 |
#include "gauche/priv/configP.h" |
| 51 |
|
| 52 |
#if !defined(PATH_ALLOC) |
| 53 |
#define PATH_ALLOC(n) malloc(n) |
| 54 |
#endif |
| 55 |
#if !defined(PATH_ERROR) |
| 56 |
static void errfn(const char *fmt, ...) |
| 57 |
{ |
| 58 |
va_list ap; |
| 59 |
va_start(ap, fmt); |
| 60 |
vfprintf(stderr, fmt, ap); |
| 61 |
va_end(ap); |
| 62 |
exit(1); |
| 63 |
} |
| 64 |
|
| 65 |
#define PATH_ERROR(...) errfn(__VA_ARGS__) |
| 66 |
#endif |
| 67 |
|
| 68 |
/* |
| 69 |
* A couple of utilities used commoly in platform-specific routines: |
| 70 |
*/ |
| 71 |
|
| 72 |
/* remove N components from path */ |
| 73 |
static char *remove_components(const char *path, int n) |
| 74 |
{ |
| 75 |
ssize_t len = strlen(path); |
| 76 |
for (ssize_t i = len-1, cnt = 0; i >= 0; i--) { |
| 77 |
if (path[i] == '/' |
| 78 |
#if defined(GAUCHE_WINDOWS) |
| 79 |
|| path[i] == '\\' |
| 80 |
#endif |
| 81 |
) { |
| 82 |
cnt++; |
| 83 |
if (cnt == n) { |
| 84 |
char *buf = PATH_ALLOC(i+1); |
| 85 |
memcpy(buf, path, i); |
| 86 |
buf[i] = '\0'; |
| 87 |
return buf; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
|
| 94 |
/* remove SUFFIX from dir */ |
| 95 |
static char *remove_suffix(char *dir, const char *suffix) |
| 96 |
{ |
| 97 |
size_t len = strlen(dir); |
| 98 |
size_t suflen = strlen(suffix); |
| 99 |
if (len > suflen && strncmp(dir + len - suflen, suffix, suflen) == 0) { |
| 100 |
dir[len-suflen] = '\0'; |
| 101 |
return dir; |
| 102 |
} |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* Platform-specifc routines to obtain runtime directories. |
| 108 |
* |
| 109 |
* For each platform, we need the following procedures: |
| 110 |
* get_libgauche_path() - path of the running libgauche*. |
| 111 |
* get_executable_path() - path of the running executable. |
| 112 |
* get_install_dir() - the installation directory (the libraries |
| 113 |
* can be found under lib/ of this directory). |
| 114 |
* Those may return NULL if the directory can't be determined. |
| 115 |
* Can return either static string or a string allocated by PATH_ALLOC; doesn't |
| 116 |
* need to worry about freeing it. |
| 117 |
* Those may throw an error with PATH_ERROR if unexpected situation occurs. |
| 118 |
*/ |
| 119 |
|
| 120 |
|
| 121 |
#if defined(GAUCHE_WINDOWS) |
| 122 |
|
| 123 |
static const char *get_libgauche_path() |
| 124 |
{ |
| 125 |
TCHAR path[MAX_PATH]; |
| 126 |
const TCHAR *libname = _T("libgauche-"GAUCHE_ABI_VERSION".dll"); |
| 127 |
|
| 128 |
HMODULE mod = GetModuleHandle(libname); |
| 129 |
if (mod == NULL) return NULL; |
| 130 |
DWORD r = GetModuleFileName(mod, path, MAX_PATH); |
| 131 |
if (r == 0) PATH_ERROR("GetModuleFileName failed"); |
| 132 |
return SCM_WCS2MBS(path); |
| 133 |
} |
| 134 |
|
| 135 |
static const char *get_executable_path() |
| 136 |
{ |
| 137 |
TCHAR path[MAX_PATH]; |
| 138 |
HMODULE mod = GetModuleHandle(NULL); |
| 139 |
if (mod == NULL) return NULL; |
| 140 |
DWORD r = GetModuleFileName(mod, path, MAX_PATH); |
| 141 |
if (r == 0) PATH_ERROR("GetModuleFileName failed"); |
| 142 |
return SCM_WCS2MBS(path); |
| 143 |
} |
| 144 |
|
| 145 |
static const char *get_install_dir() |
| 146 |
{ |
| 147 |
/* path is either $PREFIX\lib\gauche-$ABI\$VERSION\$ARCH or $PREFIX\lib */ |
| 148 |
const char *path = get_libgauche_path(); |
| 149 |
if (path != NULL) { |
| 150 |
/* remove libgauche-$ABI.dll */ |
| 151 |
char *dir = remove_components(path, 1); |
| 152 |
if (dir == NULL) return NULL; |
| 153 |
/* first, try $PREFIX\lib */ |
| 154 |
char *dir1 = remove_suffix(dir, "\\lib"); |
| 155 |
if (dir1 != NULL) return dir1; |
| 156 |
/* now we try $PREFIX\lib\gauche-$ABI\$VERSION\$ARCH */ |
| 157 |
dir1 = remove_components(dir, 3); |
| 158 |
if (dir1 != NULL) { |
| 159 |
dir1 = remove_suffix(dir1, "\\lib"); |
| 160 |
if (dir1 != NULL) return dir1; |
| 161 |
} |
| 162 |
/* now we try $PREFIX\src for buliding */ |
| 163 |
dir1 = remove_suffix(dir, "\\src"); |
| 164 |
if (dir1 != NULL) return dir1; |
| 165 |
} |
| 166 |
|
| 167 |
/* executable is in $PREFIX\lib\gauche-$ABI\$VERSION\$ARCH or $PREFIX\bin */ |
| 168 |
path = get_executable_path(); |
| 169 |
if (path != NULL) { |
| 170 |
/* remove binary name */ |
| 171 |
char *dir = remove_components(path, 1); |
| 172 |
if (dir == NULL) return NULL; |
| 173 |
/* first, try $PREFIX\bin */ |
| 174 |
char *dir1 = remove_suffix(dir, "\\bin"); |
| 175 |
if (dir1 != NULL) return dir1; |
| 176 |
/* now we try $PREFIX\lib\gauche-$ABI\$VERSION\$ARCH */ |
| 177 |
dir1 = remove_components(dir, 3); |
| 178 |
if (dir1 != NULL) { |
| 179 |
dir1 = remove_suffix(dir1, "\\lib"); |
| 180 |
if (dir1 != NULL) return dir1; |
| 181 |
} |
| 182 |
/* now we try $PREFIX\src for buliding */ |
| 183 |
dir1 = remove_suffix(dir, "\\src"); |
| 184 |
if (dir1 != NULL) return dir1; |
| 185 |
} |
| 186 |
|
| 187 |
/* At this moment, we're probably in a statically linked binary. |
| 188 |
We don't need a particular path, but we need something to substitute |
| 189 |
'@' in the load path. */ |
| 190 |
return ""; |
| 191 |
} |
| 192 |
|
| 193 |
#elif defined(HAVE_LIBPROC_H) && defined(HAVE_LIBPROC) |
| 194 |
|
| 195 |
/* OSX */ |
| 196 |
#include <libproc.h> |
| 197 |
|
| 198 |
static const char *get_libgauche_path() |
| 199 |
{ |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
static const char *get_executable_path() |
| 204 |
{ |
| 205 |
pid_t selfpid = getpid(); |
| 206 |
char buf[PROC_PIDPATHINFO_MAXSIZE]; |
| 207 |
memset(buf, 0, PROC_PIDPATHINFO_MAXSIZE); |
| 208 |
int r = proc_pidpath(selfpid, buf, PROC_PIDPATHINFO_MAXSIZE); |
| 209 |
if (r < 0) return NULL; |
| 210 |
if (r == PROC_PIDPATHINFO_MAXSIZE) return NULL; /* too long? something's funny */ |
| 211 |
char *path = PATH_ALLOC(r+1); |
| 212 |
memcpy(path, buf, r); |
| 213 |
path[r] = '\0'; |
| 214 |
return path; |
| 215 |
} |
| 216 |
|
| 217 |
static const char *get_install_dir() |
| 218 |
{ |
| 219 |
const char *self = get_executable_path(); |
| 220 |
if (self == NULL) return NULL; |
| 221 |
/* remove executable name */ |
| 222 |
char *dir = remove_components(self, 1); |
| 223 |
if (dir == NULL) return NULL; |
| 224 |
/* first, try $PREFIX/bin */ |
| 225 |
char *dir1 = remove_suffix(dir, "/bin"); |
| 226 |
if (dir1 != NULL) return dir; |
| 227 |
/* next, try $PREFIX/lib/gauche-$ABI/$VERSION/$ARCH */ |
| 228 |
dir1 = remove_components(dir, 3); |
| 229 |
if (dir1 == NULL) return NULL; |
| 230 |
return remove_suffix(dir1, "/lib"); |
| 231 |
} |
| 232 |
|
| 233 |
#elif defined(GAUCHE_MACOSX_FRAMEWORK) |
| 234 |
|
| 235 |
#include <libgen.h> |
| 236 |
#include <CoreFoundation/CoreFoundation.h> |
| 237 |
|
| 238 |
/* Must match the id in Info.plist */ |
| 239 |
#define LIBGAUCHE_ID "com.schemearts.gauche" |
| 240 |
|
| 241 |
/* Subdirs appended to the bundle path */ |
| 242 |
#define SUBDIR "/Versions/Current/" |
| 243 |
|
| 244 |
static const char *get_libgauche_path() |
| 245 |
{ |
| 246 |
return NULL; /* Placeholder for now */ |
| 247 |
} |
| 248 |
|
| 249 |
static const char *get_executable_path() |
| 250 |
{ |
| 251 |
return NULL; /* Placeholder for now */ |
| 252 |
} |
| 253 |
|
| 254 |
static const char *get_install_dir() |
| 255 |
{ |
| 256 |
CFBundleRef bundle = NULL; |
| 257 |
CFURLRef bundleURL = NULL; |
| 258 |
CFStringRef bundlePath = NULL; |
| 259 |
|
| 260 |
#define CLEANUP \ |
| 261 |
do { \ |
| 262 |
if (bundlePath) CFRelease(bundlePath); \ |
| 263 |
if (bundleURL) CFRelease(bundleURL); \ |
| 264 |
if (bundle) CFRelease(bundle); \ |
| 265 |
} while (0) |
| 266 |
|
| 267 |
bundle = CFBundleGetBundleWithIdentifier(CFSTR(LIBGAUCHE_ID)); |
| 268 |
if (bundle == NULL) { |
| 269 |
/* This call fails when gosh is called during the build process |
| 270 |
(thus, the framework hasn't been created). For the time |
| 271 |
being, we just return a dummy directory. */ |
| 272 |
CLEANUP; |
| 273 |
return "."; |
| 274 |
} |
| 275 |
/* Ownership of bundle follows the Get Rule of Core Foundation. |
| 276 |
ie. we must claim ownership (with the CFRetain function). |
| 277 |
We are then responsible for relinquishing ownership when we |
| 278 |
have finished with it. */ |
| 279 |
CFRetain(bundle); |
| 280 |
|
| 281 |
bundleURL = CFBundleCopyBundleURL(bundle); |
| 282 |
if (bundleURL == NULL) { |
| 283 |
CLEANUP; |
| 284 |
PATH_ERROR("CFBundleCopyBundleURL failed"); |
| 285 |
} |
| 286 |
/* Ownership of bundleURL follows the Create Rule of Core Foundation. |
| 287 |
ie. it is our responsibility to relinquish ownership (using CFRelease) |
| 288 |
when we have finished with it. */ |
| 289 |
|
| 290 |
bundlePath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle); |
| 291 |
if (bundlePath == NULL) { |
| 292 |
CLEANUP; |
| 293 |
PATH_ERROR("CFURLCopyFileSystemPath failed"); |
| 294 |
} |
| 295 |
/* Ownership follows the Create Rule. */ |
| 296 |
|
| 297 |
/* Estimate string length in utf8. This is provisional; we'll refine |
| 298 |
the code later. */ |
| 299 |
size_t utf16len = (size_t)CFStringGetLength(bundlePath); |
| 300 |
size_t maxlen = 3 * (utf16len+1)/2; |
| 301 |
size_t bufsiz = maxlen + strlen(SUBDIR) + 1; |
| 302 |
char* buf = PATH_ALLOC(bufsiz); |
| 303 |
|
| 304 |
if (!CFStringGetCString(bundlePath, buf, maxlen, kCFStringEncodingUTF8)) { |
| 305 |
CLEANUP; |
| 306 |
PATH_ERROR("CFStringGetCString failed"); |
| 307 |
} |
| 308 |
strcat(buf, SUBDIR); |
| 309 |
CLEANUP; |
| 310 |
return buf; |
| 311 |
#undef CLEANUP |
| 312 |
} |
| 313 |
|
| 314 |
#else |
| 315 |
/* |
| 316 |
* The fallback case. We try procfs, for it is supported on several platforms |
| 317 |
* and doesn't use special API functions. |
| 318 |
*/ |
| 319 |
#define MAPS_LINE_MAX 4096 |
| 320 |
|
| 321 |
static const char *get_libgauche_path() |
| 322 |
{ |
| 323 |
FILE *fp = fopen("/proc/self/maps", "r"); |
| 324 |
if (fp == NULL) return NULL; |
| 325 |
const char *const libgauche = "libgauche-"GAUCHE_ABI_VERSION".so"; |
| 326 |
|
| 327 |
char buf[MAPS_LINE_MAX+1]; |
| 328 |
while (fgets(buf, MAPS_LINE_MAX, fp) != NULL) { |
| 329 |
const char *p = strstr(buf, libgauche); |
| 330 |
if (p) { |
| 331 |
const char *tail = p + strlen(libgauche); |
| 332 |
for (const char *q = p-1; q >= buf; q--) { |
| 333 |
if (*q == ' ') { |
| 334 |
q++; |
| 335 |
if (q == p) { |
| 336 |
return libgauche; |
| 337 |
} else { |
| 338 |
char *r = PATH_ALLOC(tail-q+1); |
| 339 |
strncpy(r, q, tail-q); |
| 340 |
r[tail-q] = '\0'; |
| 341 |
return r; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
if (ferror(fp)) PATH_ERROR("Read error from /proc/self/maps"); |
| 348 |
return NULL; |
| 349 |
} |
| 350 |
|
| 351 |
static const char *get_executable_path() |
| 352 |
{ |
| 353 |
const char *self = "/proc/self/exe"; |
| 354 |
ssize_t buflen = MAPS_LINE_MAX; |
| 355 |
char *buf = PATH_ALLOC(buflen); |
| 356 |
ssize_t r = readlink(self, buf, buflen); |
| 357 |
if (r < 0) return NULL; /* procfs may not be available */ |
| 358 |
if (r == buflen) return NULL; /* name is suspiciously long; something's wrong. */ |
| 359 |
buf[r] = '\0'; |
| 360 |
return buf; |
| 361 |
} |
| 362 |
|
| 363 |
static const char *get_install_dir() |
| 364 |
{ |
| 365 |
/* path is either $PREFIX/lib/gauche-$ABI/$VERSION/$ARCH or $PREFIX/lib */ |
| 366 |
const char *path = get_libgauche_path(); |
| 367 |
if (path != NULL) { |
| 368 |
/* remove libgauche-$ABI.so */ |
| 369 |
char *dir = remove_components(path, 1); |
| 370 |
if (dir == NULL) return NULL; |
| 371 |
/* first, try $PREFIX/lib */ |
| 372 |
char *dir1 = remove_suffix(dir, "/lib"); |
| 373 |
if (dir1 != NULL) return dir1; |
| 374 |
/* now we try $PREFIX/lib/gauche-$ABI/$VERSION/$ARCH */ |
| 375 |
dir1 = remove_components(dir, 3); |
| 376 |
if (dir1 != NULL) { |
| 377 |
dir1 = remove_suffix(dir1, "/lib"); |
| 378 |
if (dir1 != NULL) return dir1; |
| 379 |
} |
| 380 |
} |
| 381 |
/* executable is in $PREFIX/lib/gauche-$ABI/$VERSION/$ARCH or $PREFIX/bin */ |
| 382 |
path = get_executable_path(); |
| 383 |
if (path != NULL) { |
| 384 |
/* remove binary name */ |
| 385 |
char *dir = remove_components(path, 1); |
| 386 |
if (dir == NULL) return NULL; |
| 387 |
/* first, try $PREFIX/bin */ |
| 388 |
char *dir1 = remove_suffix(dir, "/bin"); |
| 389 |
if (dir1 != NULL) return dir1; |
| 390 |
/* now we try $PREFIX/lib/gauche-$ABI/$VERSION/$ARCH */ |
| 391 |
dir1 = remove_components(dir, 3); |
| 392 |
if (dir1 != NULL) { |
| 393 |
dir1 = remove_suffix(dir1, "/lib"); |
| 394 |
if (dir1 != NULL) return dir1; |
| 395 |
} |
| 396 |
} |
| 397 |
return NULL; |
| 398 |
} |
| 399 |
#endif |
| 400 |
|
| 401 |
/* |
| 402 |
* Common routines |
| 403 |
*/ |
| 404 |
|
| 405 |
static const char *substitute_all(const char *input, |
| 406 |
const char *mark, |
| 407 |
const char *subst) |
| 408 |
{ |
| 409 |
size_t ilen = strlen(input); |
| 410 |
size_t mlen = strlen(mark); |
| 411 |
size_t slen = strlen(subst); |
| 412 |
|
| 413 |
int noccurs = 0; |
| 414 |
const char *p = input; |
| 415 |
const char *pend = p + ilen; |
| 416 |
while (p < pend) { |
| 417 |
const char *p1 = strstr(p, mark); |
| 418 |
if (p1 == NULL) break; |
| 419 |
noccurs++; |
| 420 |
p = p1 + mlen; |
| 421 |
} |
| 422 |
|
| 423 |
if (noccurs == 0) return input; |
| 424 |
size_t buflen = noccurs * slen + ilen - noccurs * mlen; |
| 425 |
char *buf = (char*)PATH_ALLOC(buflen+1); |
| 426 |
char *q = buf; |
| 427 |
for (p = input; noccurs > 0; noccurs--) { |
| 428 |
const char *p1 = strstr(p, mark); |
| 429 |
memcpy(q, p, p1-p); |
| 430 |
q += p1-p; |
| 431 |
memcpy(q, subst, slen); |
| 432 |
q += slen; |
| 433 |
p = p1 + mlen; |
| 434 |
} |
| 435 |
strncpy(q, p, pend-p); |
| 436 |
buf[buflen] = '\0'; |
| 437 |
return buf; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
/* The configure-generated path may have '@' in the pathnames. We replace |
| 442 |
it with the installation directory. |
| 443 |
|
| 444 |
NB: This is a static function, but called from gauche-config.c (it includes |
| 445 |
paths.c). |
| 446 |
*/ |
| 447 |
static const char *replace_install_dir(const char *orig) |
| 448 |
{ |
| 449 |
if (strstr(orig, "@") == NULL) return orig; /* no replace */ |
| 450 |
const char *idir = get_install_dir(); |
| 451 |
if (idir == NULL) { |
| 452 |
PATH_ERROR("Couldn't obtain installation directory."); |
| 453 |
} |
| 454 |
return substitute_all(orig, "@", idir); |
| 455 |
} |