| 1 |
/* |
| 2 |
* BinReloc - a library for creating relocatable executables |
| 3 |
* Written by: Hongli Lai <[email protected]> |
| 4 |
* http://autopackage.org/ |
| 5 |
* |
| 6 |
* This source code is public domain. You can relicense this code |
| 7 |
* under whatever license you want. |
| 8 |
* |
| 9 |
* See http://autopackage.org/docs/binreloc/ for |
| 10 |
* more information and how to use this. |
| 11 |
*/ |
| 12 |
|
| 13 |
#ifndef __BINRELOC_C__ |
| 14 |
#define __BINRELOC_C__ |
| 15 |
|
| 16 |
#include "autoconfig.h" // ASF: added, to not expect ENABLE_BINRELOC from command line |
| 17 |
|
| 18 |
#ifdef ENABLE_BINRELOC |
| 19 |
#include <sys/types.h> |
| 20 |
#include <sys/stat.h> |
| 21 |
#include <unistd.h> |
| 22 |
#endif /* ENABLE_BINRELOC */ |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <limits.h> |
| 26 |
#include <string.h> |
| 27 |
#include "binreloc.h" |
| 28 |
|
| 29 |
#ifdef __cplusplus |
| 30 |
extern "C" { |
| 31 |
#endif /* __cplusplus */ |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
/** @internal |
| 36 |
* Find the canonical filename of the executable. Returns the filename |
| 37 |
* (which must be freed) or NULL on error. If the parameter 'error' is |
| 38 |
* not NULL, the error code will be stored there, if an error occured. |
| 39 |
*/ |
| 40 |
static char * |
| 41 |
_br_find_exe (BrInitError *error) |
| 42 |
{ |
| 43 |
#ifndef ENABLE_BINRELOC |
| 44 |
if (error) |
| 45 |
*error = BR_INIT_ERROR_DISABLED; |
| 46 |
return NULL; |
| 47 |
#else |
| 48 |
char *line, *result; |
| 49 |
size_t buf_size = (PATH_MAX > SSIZE_MAX ? SSIZE_MAX : PATH_MAX) - 1; |
| 50 |
ssize_t size; |
| 51 |
struct stat stat_buf; |
| 52 |
FILE *f; |
| 53 |
|
| 54 |
/* Read from /proc/self/exe (symlink) */ |
| 55 |
char* path = (char *) malloc (buf_size); |
| 56 |
if (path == NULL) |
| 57 |
{ |
| 58 |
/* Cannot allocate memory. */ |
| 59 |
if (error) |
| 60 |
*error = BR_INIT_ERROR_NOMEM; |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
char* path2 = (char *) malloc (buf_size); |
| 64 |
if (path2 == NULL) { |
| 65 |
/* Cannot allocate memory. */ |
| 66 |
if (error) |
| 67 |
*error = BR_INIT_ERROR_NOMEM; |
| 68 |
free (path); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
strncpy (path2, "/proc/self/exe", buf_size - 1); |
| 73 |
|
| 74 |
while (1) |
| 75 |
{ |
| 76 |
int i; |
| 77 |
|
| 78 |
size = readlink (path2, path, buf_size - 1); |
| 79 |
if (size == -1) |
| 80 |
{ |
| 81 |
/* Error. */ |
| 82 |
free (path2); |
| 83 |
break; |
| 84 |
} |
| 85 |
|
| 86 |
/* readlink() success. */ |
| 87 |
path[size] = '\0'; |
| 88 |
|
| 89 |
/* Check whether the symlink's target is also a symlink. |
| 90 |
* We want to get the final target. */ |
| 91 |
i = stat (path, &stat_buf); |
| 92 |
if (i == -1) |
| 93 |
{ |
| 94 |
/* Error. */ |
| 95 |
free (path2); |
| 96 |
break; |
| 97 |
} |
| 98 |
|
| 99 |
/* stat() success. */ |
| 100 |
if (!S_ISLNK (stat_buf.st_mode)) |
| 101 |
{ |
| 102 |
/* path is not a symlink. Done. */ |
| 103 |
free (path2); |
| 104 |
return path; |
| 105 |
} |
| 106 |
|
| 107 |
/* path is a symlink. Continue loop and resolve this. */ |
| 108 |
strncpy (path, path2, buf_size - 1); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/* readlink() or stat() failed; this can happen when the program is |
| 113 |
* running in Valgrind 2.2. Read from /proc/self/maps as fallback. */ |
| 114 |
|
| 115 |
buf_size = PATH_MAX + 128; |
| 116 |
line = (char *) realloc (path, buf_size); |
| 117 |
if (line == NULL) |
| 118 |
{ |
| 119 |
/* Cannot allocate memory. */ |
| 120 |
free (path); |
| 121 |
if (error) |
| 122 |
*error = BR_INIT_ERROR_NOMEM; |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
f = fopen ("/proc/self/maps", "r"); |
| 127 |
if (f == NULL) |
| 128 |
{ |
| 129 |
free (line); |
| 130 |
if (error) |
| 131 |
*error = BR_INIT_ERROR_OPEN_MAPS; |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
/* The first entry should be the executable name. */ |
| 136 |
result = fgets (line, (int) buf_size, f); |
| 137 |
if (result == NULL) |
| 138 |
{ |
| 139 |
fclose (f); |
| 140 |
free (line); |
| 141 |
if (error) |
| 142 |
*error = BR_INIT_ERROR_READ_MAPS; |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
|
| 146 |
/* Get rid of newline character. */ |
| 147 |
buf_size = strlen (line); |
| 148 |
if (buf_size <= 0) |
| 149 |
{ |
| 150 |
/* Huh? An empty string? */ |
| 151 |
fclose (f); |
| 152 |
free (line); |
| 153 |
if (error) |
| 154 |
*error = BR_INIT_ERROR_INVALID_MAPS; |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
if (line[buf_size - 1] == 10) |
| 158 |
line[buf_size - 1] = 0; |
| 159 |
|
| 160 |
/* Extract the filename; it is always an absolute path. */ |
| 161 |
path = strchr (line, '/'); |
| 162 |
|
| 163 |
/* Sanity check. */ |
| 164 |
if (strstr (line, " r-xp ") == NULL || path == NULL) |
| 165 |
{ |
| 166 |
fclose (f); |
| 167 |
free (line); |
| 168 |
if (error) |
| 169 |
*error = BR_INIT_ERROR_INVALID_MAPS; |
| 170 |
return NULL; |
| 171 |
} |
| 172 |
|
| 173 |
path = strdup (path); |
| 174 |
free (line); |
| 175 |
fclose (f); |
| 176 |
return path; |
| 177 |
#endif /* ENABLE_BINRELOC */ |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/** @internal |
| 182 |
* Find the canonical filename of the executable which owns symbol. |
| 183 |
* Returns a filename which must be freed, or NULL on error. |
| 184 |
*/ |
| 185 |
static char * |
| 186 |
_br_find_exe_for_symbol (const void *symbol, BrInitError *error) |
| 187 |
{ |
| 188 |
#ifndef ENABLE_BINRELOC |
| 189 |
if (error) |
| 190 |
*error = BR_INIT_ERROR_DISABLED; |
| 191 |
return (char *) NULL; |
| 192 |
#else |
| 193 |
#define SIZE PATH_MAX + 100 |
| 194 |
FILE *f; |
| 195 |
size_t address_string_len; |
| 196 |
char *address_string, line[SIZE], *found; |
| 197 |
|
| 198 |
if (symbol == NULL) |
| 199 |
return (char *) NULL; |
| 200 |
|
| 201 |
f = fopen ("/proc/self/maps", "r"); |
| 202 |
if (f == NULL) |
| 203 |
return (char *) NULL; |
| 204 |
|
| 205 |
address_string_len = 4; |
| 206 |
address_string = (char *) malloc (address_string_len); |
| 207 |
found = (char *) NULL; |
| 208 |
|
| 209 |
while (!feof (f)) |
| 210 |
{ |
| 211 |
char *start_addr, *end_addr, *end_addr_end, *file; |
| 212 |
void *start_addr_p, *end_addr_p; |
| 213 |
size_t len; |
| 214 |
|
| 215 |
if (fgets (line, SIZE, f) == NULL) |
| 216 |
break; |
| 217 |
|
| 218 |
/* Sanity check. */ |
| 219 |
if ( !(strstr(line, " r-xp ") || strstr(line, " r--p ")) || strchr (line, '/') == NULL) |
| 220 |
continue; |
| 221 |
|
| 222 |
/* Parse line. */ |
| 223 |
start_addr = line; |
| 224 |
end_addr = strchr (line, '-'); |
| 225 |
file = strchr (line, '/'); |
| 226 |
|
| 227 |
/* More sanity check. */ |
| 228 |
if (!(file > end_addr && end_addr != NULL && end_addr[0] == '-')) |
| 229 |
continue; |
| 230 |
|
| 231 |
end_addr[0] = '\0'; |
| 232 |
end_addr++; |
| 233 |
end_addr_end = strchr (end_addr, ' '); |
| 234 |
if (end_addr_end == NULL) |
| 235 |
continue; |
| 236 |
|
| 237 |
end_addr_end[0] = '\0'; |
| 238 |
len = strlen (file); |
| 239 |
if (len == 0) |
| 240 |
continue; |
| 241 |
if (file[len - 1] == '\n') |
| 242 |
file[len - 1] = '\0'; |
| 243 |
|
| 244 |
/* Get rid of "(deleted)" from the filename. */ |
| 245 |
len = strlen (file); |
| 246 |
if (len > 10 && strcmp (file + len - 10, " (deleted)") == 0) |
| 247 |
file[len - 10] = '\0'; |
| 248 |
|
| 249 |
/* I don't know whether this can happen but better safe than sorry. */ |
| 250 |
len = strlen (start_addr); |
| 251 |
if (len != strlen (end_addr)) |
| 252 |
continue; |
| 253 |
|
| 254 |
|
| 255 |
/* Transform the addresses into a string in the form of 0xdeadbeef, |
| 256 |
* then transform that into a pointer. */ |
| 257 |
if (address_string_len < len + 3) |
| 258 |
{ |
| 259 |
address_string_len = len + 3; |
| 260 |
address_string = (char *) realloc (address_string, address_string_len); |
| 261 |
} |
| 262 |
|
| 263 |
memcpy (address_string, "0x", 2); |
| 264 |
memcpy (address_string + 2, start_addr, len); |
| 265 |
address_string[2 + len] = '\0'; |
| 266 |
sscanf (address_string, "%p", &start_addr_p); |
| 267 |
|
| 268 |
memcpy (address_string, "0x", 2); |
| 269 |
memcpy (address_string + 2, end_addr, len); |
| 270 |
address_string[2 + len] = '\0'; |
| 271 |
sscanf (address_string, "%p", &end_addr_p); |
| 272 |
|
| 273 |
|
| 274 |
if (symbol >= start_addr_p && symbol < end_addr_p) |
| 275 |
{ |
| 276 |
found = file; |
| 277 |
break; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
free (address_string); |
| 282 |
fclose (f); |
| 283 |
|
| 284 |
if (found == NULL) |
| 285 |
return (char*) NULL; |
| 286 |
|
| 287 |
return strdup (found); |
| 288 |
#endif /* ENABLE_BINRELOC */ |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
#ifndef BINRELOC_RUNNING_DOXYGEN |
| 293 |
#undef NULL |
| 294 |
#define NULL ((void *) 0) /* typecasted as char* for C++ type safeness */ |
| 295 |
#endif |
| 296 |
|
| 297 |
static char *exe = (char *) NULL; |
| 298 |
|
| 299 |
static void br_exit(void) |
| 300 |
{ |
| 301 |
if (exe) |
| 302 |
{ |
| 303 |
free(exe); |
| 304 |
exe = (char *) NULL; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/** Initialize the BinReloc library (for applications). |
| 309 |
* |
| 310 |
* This function must be called before using any other BinReloc functions. |
| 311 |
* It attempts to locate the application's canonical filename. |
| 312 |
* |
| 313 |
* @note If you want to use BinReloc for a library, then you should call |
| 314 |
* br_init_lib() instead. |
| 315 |
* |
| 316 |
* @param error If BinReloc failed to initialize, then the error code will |
| 317 |
* be stored in this variable. Set to NULL if you want to |
| 318 |
* ignore this. See #BrInitError for a list of error codes. |
| 319 |
* |
| 320 |
* @returns 1 on success, 0 if BinReloc failed to initialize. |
| 321 |
*/ |
| 322 |
int |
| 323 |
br_init (BrInitError *error) |
| 324 |
{ |
| 325 |
int was = exe == NULL ? 0 : 1; |
| 326 |
exe = _br_find_exe (error); |
| 327 |
if (exe && !was) |
| 328 |
atexit(br_exit); |
| 329 |
return exe != NULL; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
/** Initialize the BinReloc library (for libraries). |
| 334 |
* |
| 335 |
* This function must be called before using any other BinReloc functions. |
| 336 |
* It attempts to locate the calling library's canonical filename. |
| 337 |
* |
| 338 |
* @note The BinReloc source code MUST be included in your library, or this |
| 339 |
* function won't work correctly. |
| 340 |
* |
| 341 |
* @param error If BinReloc failed to initialize, then the error code will |
| 342 |
* be stored in this variable. Set to NULL if you want to |
| 343 |
* ignore this. See #BrInitError for a list of error codes. |
| 344 |
* |
| 345 |
* @returns 1 on success, 0 if a filename cannot be found. |
| 346 |
*/ |
| 347 |
int |
| 348 |
br_init_lib (BrInitError *error) |
| 349 |
{ |
| 350 |
int was = exe == NULL ? 0 : 1; |
| 351 |
exe = _br_find_exe_for_symbol ((const void *) "", error); |
| 352 |
if (exe && !was) |
| 353 |
atexit(br_exit); |
| 354 |
return exe != NULL; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
/** Find the canonical filename of the current application. |
| 359 |
* |
| 360 |
* @param default_exe A default filename which will be used as fallback. |
| 361 |
* @returns A string containing the application's canonical filename, |
| 362 |
* which must be freed when no longer necessary. If BinReloc is |
| 363 |
* not initialized, or if br_init() failed, then a copy of |
| 364 |
* default_exe will be returned. If default_exe is NULL, then |
| 365 |
* NULL will be returned. |
| 366 |
*/ |
| 367 |
char * |
| 368 |
br_find_exe (const char *default_exe) |
| 369 |
{ |
| 370 |
if (exe == (char *) NULL) |
| 371 |
{ |
| 372 |
/* BinReloc is not initialized. */ |
| 373 |
if (default_exe != (const char *) NULL) |
| 374 |
return strdup (default_exe); |
| 375 |
|
| 376 |
return (char *) NULL; |
| 377 |
} |
| 378 |
return strdup (exe); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
/** Locate the directory in which the current application is installed. |
| 383 |
* |
| 384 |
* The prefix is generated by the following pseudo-code evaluation: |
| 385 |
* \code |
| 386 |
* dirname(exename) |
| 387 |
* \endcode |
| 388 |
* |
| 389 |
* @param default_dir A default directory which will used as fallback. |
| 390 |
* @return A string containing the directory, which must be freed when no |
| 391 |
* longer necessary. If BinReloc is not initialized, or if the |
| 392 |
* initialization function failed, then a copy of default_dir |
| 393 |
* will be returned. If default_dir is NULL, then NULL will be |
| 394 |
* returned. |
| 395 |
*/ |
| 396 |
char * |
| 397 |
br_find_exe_dir (const char *default_dir) |
| 398 |
{ |
| 399 |
if (exe == NULL) |
| 400 |
{ |
| 401 |
/* BinReloc not initialized. */ |
| 402 |
if (default_dir != NULL) |
| 403 |
return strdup (default_dir); |
| 404 |
|
| 405 |
return NULL; |
| 406 |
} |
| 407 |
|
| 408 |
return br_dirname (exe); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
/** Locate the prefix in which the current application is installed. |
| 413 |
* |
| 414 |
* The prefix is generated by the following pseudo-code evaluation: |
| 415 |
* \code |
| 416 |
* dirname(dirname(exename)) |
| 417 |
* \endcode |
| 418 |
* |
| 419 |
* @param default_prefix A default prefix which will used as fallback. |
| 420 |
* @return A string containing the prefix, which must be freed when no |
| 421 |
* longer necessary. If BinReloc is not initialized, or if |
| 422 |
* the initialization function failed, then a copy of default_prefix |
| 423 |
* will be returned. If default_prefix is NULL, then NULL will be returned. |
| 424 |
*/ |
| 425 |
char * |
| 426 |
br_find_prefix (const char *default_prefix) |
| 427 |
{ |
| 428 |
char *dir1, *dir2; |
| 429 |
|
| 430 |
if (exe == (char *) NULL) |
| 431 |
{ |
| 432 |
/* BinReloc not initialized. */ |
| 433 |
if (default_prefix != (const char *) NULL) |
| 434 |
return strdup (default_prefix); |
| 435 |
|
| 436 |
return (char *) NULL; |
| 437 |
} |
| 438 |
|
| 439 |
dir1 = br_dirname (exe); |
| 440 |
dir2 = br_dirname (dir1); |
| 441 |
free (dir1); |
| 442 |
return dir2; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
/** Locate the application's binary folder. |
| 447 |
* |
| 448 |
* The path is generated by the following pseudo-code evaluation: |
| 449 |
* \code |
| 450 |
* prefix + "/bin" |
| 451 |
* \endcode |
| 452 |
* |
| 453 |
* @param default_bin_dir A default path which will used as fallback. |
| 454 |
* @return A string containing the bin folder's path, which must be freed when |
| 455 |
* no longer necessary. If BinReloc is not initialized, or if |
| 456 |
* the initialization function failed, then a copy of default_bin_dir will |
| 457 |
* be returned. If default_bin_dir is NULL, then NULL will be returned. |
| 458 |
*/ |
| 459 |
char * |
| 460 |
br_find_bin_dir (const char *default_bin_dir) |
| 461 |
{ |
| 462 |
char *prefix, *dir; |
| 463 |
|
| 464 |
prefix = br_find_prefix ((const char *) NULL); |
| 465 |
if (prefix == (char *) NULL) |
| 466 |
{ |
| 467 |
/* BinReloc not initialized. */ |
| 468 |
if (default_bin_dir != (const char *) NULL) |
| 469 |
return strdup (default_bin_dir); |
| 470 |
|
| 471 |
return (char *) NULL; |
| 472 |
} |
| 473 |
|
| 474 |
dir = br_build_path (prefix, "bin"); |
| 475 |
free (prefix); |
| 476 |
return dir; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
/** Locate the application's superuser binary folder. |
| 481 |
* |
| 482 |
* The path is generated by the following pseudo-code evaluation: |
| 483 |
* \code |
| 484 |
* prefix + "/sbin" |
| 485 |
* \endcode |
| 486 |
* |
| 487 |
* @param default_sbin_dir A default path which will used as fallback. |
| 488 |
* @return A string containing the sbin folder's path, which must be freed when |
| 489 |
* no longer necessary. If BinReloc is not initialized, or if the |
| 490 |
* initialization function failed, then a copy of default_sbin_dir will |
| 491 |
* be returned. If default_bin_dir is NULL, then NULL will be returned. |
| 492 |
*/ |
| 493 |
char * |
| 494 |
br_find_sbin_dir (const char *default_sbin_dir) |
| 495 |
{ |
| 496 |
char *prefix, *dir; |
| 497 |
|
| 498 |
prefix = br_find_prefix ((const char *) NULL); |
| 499 |
if (prefix == (char *) NULL) |
| 500 |
{ |
| 501 |
/* BinReloc not initialized. */ |
| 502 |
if (default_sbin_dir != (const char *) NULL) |
| 503 |
return strdup (default_sbin_dir); |
| 504 |
|
| 505 |
return (char *) NULL; |
| 506 |
} |
| 507 |
|
| 508 |
dir = br_build_path (prefix, "sbin"); |
| 509 |
free (prefix); |
| 510 |
return dir; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
/** Locate the application's data folder. |
| 515 |
* |
| 516 |
* The path is generated by the following pseudo-code evaluation: |
| 517 |
* \code |
| 518 |
* prefix + "/share" |
| 519 |
* \endcode |
| 520 |
* |
| 521 |
* @param default_data_dir A default path which will used as fallback. |
| 522 |
* @return A string containing the data folder's path, which must be freed when |
| 523 |
* no longer necessary. If BinReloc is not initialized, or if the |
| 524 |
* initialization function failed, then a copy of default_data_dir |
| 525 |
* will be returned. If default_data_dir is NULL, then NULL will be |
| 526 |
* returned. |
| 527 |
*/ |
| 528 |
char * |
| 529 |
br_find_data_dir (const char *default_data_dir) |
| 530 |
{ |
| 531 |
char *prefix, *dir; |
| 532 |
|
| 533 |
prefix = br_find_prefix ((const char *) NULL); |
| 534 |
if (prefix == (char *) NULL) |
| 535 |
{ |
| 536 |
/* BinReloc not initialized. */ |
| 537 |
if (default_data_dir != (const char *) NULL) |
| 538 |
return strdup (default_data_dir); |
| 539 |
|
| 540 |
return (char *) NULL; |
| 541 |
} |
| 542 |
|
| 543 |
dir = br_build_path (prefix, "share"); |
| 544 |
free (prefix); |
| 545 |
return dir; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
/** Locate the application's localization folder. |
| 550 |
* |
| 551 |
* The path is generated by the following pseudo-code evaluation: |
| 552 |
* \code |
| 553 |
* prefix + "/share/locale" |
| 554 |
* \endcode |
| 555 |
* |
| 556 |
* @param default_locale_dir A default path which will used as fallback. |
| 557 |
* @return A string containing the localization folder's path, which must be freed when |
| 558 |
* no longer necessary. If BinReloc is not initialized, or if the |
| 559 |
* initialization function failed, then a copy of default_locale_dir will be returned. |
| 560 |
* If default_locale_dir is NULL, then NULL will be returned. |
| 561 |
*/ |
| 562 |
char * |
| 563 |
br_find_locale_dir (const char *default_locale_dir) |
| 564 |
{ |
| 565 |
char *data_dir, *dir; |
| 566 |
|
| 567 |
data_dir = br_find_data_dir ((const char *) NULL); |
| 568 |
if (data_dir == (char *) NULL) |
| 569 |
{ |
| 570 |
/* BinReloc not initialized. */ |
| 571 |
if (default_locale_dir != (const char *) NULL) |
| 572 |
return strdup (default_locale_dir); |
| 573 |
|
| 574 |
return (char *) NULL; |
| 575 |
} |
| 576 |
|
| 577 |
dir = br_build_path (data_dir, "locale"); |
| 578 |
free (data_dir); |
| 579 |
return dir; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
/** Locate the application's library folder. |
| 584 |
* |
| 585 |
* The path is generated by the following pseudo-code evaluation: |
| 586 |
* \code |
| 587 |
* prefix + "/lib" |
| 588 |
* \endcode |
| 589 |
* |
| 590 |
* @param default_lib_dir A default path which will used as fallback. |
| 591 |
* @return A string containing the library folder's path, which must be freed when |
| 592 |
* no longer necessary. If BinReloc is not initialized, or if the initialization |
| 593 |
* function failed, then a copy of default_lib_dir will be returned. |
| 594 |
* If default_lib_dir is NULL, then NULL will be returned. |
| 595 |
*/ |
| 596 |
char * |
| 597 |
br_find_lib_dir (const char *default_lib_dir) |
| 598 |
{ |
| 599 |
char *prefix, *dir; |
| 600 |
|
| 601 |
prefix = br_find_prefix ((const char *) NULL); |
| 602 |
if (prefix == (char *) NULL) |
| 603 |
{ |
| 604 |
/* BinReloc not initialized. */ |
| 605 |
if (default_lib_dir != (const char *) NULL) |
| 606 |
return strdup (default_lib_dir); |
| 607 |
|
| 608 |
return (char *) NULL; |
| 609 |
} |
| 610 |
|
| 611 |
dir = br_build_path (prefix, "lib"); |
| 612 |
free (prefix); |
| 613 |
return dir; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
/** Locate the application's libexec folder. |
| 618 |
* |
| 619 |
* The path is generated by the following pseudo-code evaluation: |
| 620 |
* \code |
| 621 |
* prefix + "/libexec" |
| 622 |
* \endcode |
| 623 |
* |
| 624 |
* @param default_libexec_dir A default path which will used as fallback. |
| 625 |
* @return A string containing the libexec folder's path, which must be freed when |
| 626 |
* no longer necessary. If BinReloc is not initialized, or if the initialization |
| 627 |
* function failed, then a copy of default_libexec_dir will be returned. |
| 628 |
* If default_libexec_dir is NULL, then NULL will be returned. |
| 629 |
*/ |
| 630 |
char * |
| 631 |
br_find_libexec_dir (const char *default_libexec_dir) |
| 632 |
{ |
| 633 |
char *prefix, *dir; |
| 634 |
|
| 635 |
prefix = br_find_prefix ((const char *) NULL); |
| 636 |
if (prefix == (char *) NULL) |
| 637 |
{ |
| 638 |
/* BinReloc not initialized. */ |
| 639 |
if (default_libexec_dir != (const char *) NULL) |
| 640 |
return strdup (default_libexec_dir); |
| 641 |
|
| 642 |
return (char *) NULL; |
| 643 |
} |
| 644 |
|
| 645 |
dir = br_build_path (prefix, "libexec"); |
| 646 |
free (prefix); |
| 647 |
return dir; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
/** Locate the application's configuration files folder. |
| 652 |
* |
| 653 |
* The path is generated by the following pseudo-code evaluation: |
| 654 |
* \code |
| 655 |
* prefix + "/etc" |
| 656 |
* \endcode |
| 657 |
* |
| 658 |
* @param default_etc_dir A default path which will used as fallback. |
| 659 |
* @return A string containing the etc folder's path, which must be freed when |
| 660 |
* no longer necessary. If BinReloc is not initialized, or if the initialization |
| 661 |
* function failed, then a copy of default_etc_dir will be returned. |
| 662 |
* If default_etc_dir is NULL, then NULL will be returned. |
| 663 |
*/ |
| 664 |
char * |
| 665 |
br_find_etc_dir (const char *default_etc_dir) |
| 666 |
{ |
| 667 |
char *prefix, *dir; |
| 668 |
|
| 669 |
prefix = br_find_prefix ((const char *) NULL); |
| 670 |
if (prefix == (char *) NULL) |
| 671 |
{ |
| 672 |
/* BinReloc not initialized. */ |
| 673 |
if (default_etc_dir != (const char *) NULL) |
| 674 |
return strdup (default_etc_dir); |
| 675 |
|
| 676 |
return (char *) NULL; |
| 677 |
} |
| 678 |
|
| 679 |
dir = br_build_path (prefix, "etc"); |
| 680 |
free (prefix); |
| 681 |
return dir; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
/*********************** |
| 686 |
* Utility functions |
| 687 |
***********************/ |
| 688 |
|
| 689 |
/** Concatenate str1 and str2 to a newly allocated string. |
| 690 |
* |
| 691 |
* @param str1 A string. |
| 692 |
* @param str2 Another string. |
| 693 |
* @returns A newly-allocated string. This string should be freed when no longer needed. |
| 694 |
*/ |
| 695 |
char * |
| 696 |
br_strcat (const char *str1, const char *str2) |
| 697 |
{ |
| 698 |
char *result; |
| 699 |
size_t len1, len2; |
| 700 |
|
| 701 |
if (str1 == NULL) |
| 702 |
str1 = ""; |
| 703 |
if (str2 == NULL) |
| 704 |
str2 = ""; |
| 705 |
|
| 706 |
len1 = strlen (str1); |
| 707 |
len2 = strlen (str2); |
| 708 |
|
| 709 |
result = (char *) malloc (len1 + len2 + 1); |
| 710 |
memcpy (result, str1, len1); |
| 711 |
memcpy (result + len1, str2, len2); |
| 712 |
result[len1 + len2] = '\0'; |
| 713 |
|
| 714 |
return result; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
char * |
| 719 |
br_build_path (const char *dir, const char *file) |
| 720 |
{ |
| 721 |
char *dir2, *result; |
| 722 |
size_t len; |
| 723 |
int must_free = 0; |
| 724 |
|
| 725 |
len = strlen (dir); |
| 726 |
if (len > 0 && dir[len - 1] != '/') |
| 727 |
{ |
| 728 |
dir2 = br_strcat (dir, "/"); |
| 729 |
must_free = 1; |
| 730 |
} |
| 731 |
else |
| 732 |
dir2 = (char *) dir; |
| 733 |
|
| 734 |
result = br_strcat (dir2, file); |
| 735 |
if (must_free) |
| 736 |
free (dir2); |
| 737 |
return result; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
/* Emulates glibc's strndup() */ |
| 742 |
static char * |
| 743 |
br_strndup (const char *str, size_t size) |
| 744 |
{ |
| 745 |
char *result = (char *) NULL; |
| 746 |
size_t len; |
| 747 |
|
| 748 |
if (str == (const char *) NULL) |
| 749 |
return (char *) NULL; |
| 750 |
|
| 751 |
len = strlen (str); |
| 752 |
if (len == 0) |
| 753 |
return strdup (""); |
| 754 |
if (size > len) |
| 755 |
size = len; |
| 756 |
|
| 757 |
result = (char *) malloc (len + 1); |
| 758 |
memcpy (result, str, size); |
| 759 |
result[size] = '\0'; |
| 760 |
return result; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
/** Extracts the directory component of a path. |
| 765 |
* |
| 766 |
* Similar to g_dirname() or the dirname commandline application. |
| 767 |
* |
| 768 |
* Example: |
| 769 |
* \code |
| 770 |
* br_dirname ("/usr/local/foobar"); --> Returns: "/usr/local" |
| 771 |
* \endcode |
| 772 |
* |
| 773 |
* @param path A path. |
| 774 |
* @returns A directory name. This string should be freed when no longer needed. |
| 775 |
*/ |
| 776 |
char * |
| 777 |
br_dirname (const char *path) |
| 778 |
{ |
| 779 |
char *end, *result; |
| 780 |
|
| 781 |
if (path == (const char *) NULL) |
| 782 |
return (char *) NULL; |
| 783 |
|
| 784 |
end = strrchr (path, '/'); |
| 785 |
if (end == (const char *) NULL) |
| 786 |
return strdup ("."); |
| 787 |
|
| 788 |
while (end > path && *end == '/') |
| 789 |
end--; |
| 790 |
result = br_strndup (path, end - path + 1); |
| 791 |
if (result[0] == 0) |
| 792 |
{ |
| 793 |
free (result); |
| 794 |
return strdup ("/"); |
| 795 |
} |
| 796 |
|
| 797 |
return result; |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
#ifdef __cplusplus |
| 802 |
} |
| 803 |
#endif /* __cplusplus */ |
| 804 |
|
| 805 |
#endif /* __BINRELOC_C__ */ |