PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
BinReloc.h File Reference

Go to the source code of this file.

Enumerations

enum  BrInitError {
  BR_INIT_ERROR_NOMEM , BR_INIT_ERROR_OPEN_MAPS , BR_INIT_ERROR_READ_MAPS , BR_INIT_ERROR_INVALID_MAPS ,
  BR_INIT_ERROR_DISABLED
}
 

Functions

int br_init (BrInitError *error)
 
int br_init_lib (BrInitError *error)
 
char * br_find_exe (const char *default_exe)
 
char * br_find_exe_dir (const char *default_dir)
 
char * br_find_prefix (const char *default_prefix)
 
char * br_find_bin_dir (const char *default_bin_dir)
 
char * br_find_sbin_dir (const char *default_sbin_dir)
 
char * br_find_data_dir (const char *default_data_dir)
 
char * br_find_locale_dir (const char *default_locale_dir)
 
char * br_find_lib_dir (const char *default_lib_dir)
 
char * br_find_libexec_dir (const char *default_libexec_dir)
 
char * br_find_etc_dir (const char *default_etc_dir)
 
char * br_strcat (const char *str1, const char *str2)
 
char * br_build_path (const char *dir, const char *file)
 
char * br_dirname (const char *path)
 

Enumeration Type Documentation

◆ BrInitError

These error codes can be returned by br_init(), br_init_lib(), gbr_init() or gbr_init_lib().

Enumerator
BR_INIT_ERROR_NOMEM 

Cannot allocate memory.

BR_INIT_ERROR_OPEN_MAPS 

Unable to open /proc/self/maps; see errno for details.

BR_INIT_ERROR_READ_MAPS 

Unable to read from /proc/self/maps; see errno for details.

BR_INIT_ERROR_INVALID_MAPS 

The file format of /proc/self/maps is invalid; kernel bug?

BR_INIT_ERROR_DISABLED 

BinReloc is disabled (the ENABLE_BINRELOC macro is not defined).

Definition at line 22 of file BinReloc.h.

22 {
BrInitError
Definition BinReloc.h:22
@ BR_INIT_ERROR_OPEN_MAPS
Definition BinReloc.h:26
@ BR_INIT_ERROR_DISABLED
Definition BinReloc.h:32
@ BR_INIT_ERROR_NOMEM
Definition BinReloc.h:24
@ BR_INIT_ERROR_INVALID_MAPS
Definition BinReloc.h:30
@ BR_INIT_ERROR_READ_MAPS
Definition BinReloc.h:28

Function Documentation

◆ br_build_path()

char * br_build_path ( const char *  dir,
const char *  file 
)

Definition at line 718 of file BinReloc.cpp.

719{
720 char *dir2, *result;
721 size_t len;
722 int must_free = 0;
723
724 len = strlen (dir);
725 if (len > 0 && dir[len - 1] != '/') {
726 dir2 = br_strcat (dir, "/");
727 must_free = 1;
728 } else
729 dir2 = (char *) dir;
730
731 result = br_strcat (dir2, file);
732 if (must_free)
733 free (dir2);
734 return result;
735}
char * br_strcat(const char *str1, const char *str2)
Definition BinReloc.cpp:695

References br_strcat().

Referenced by br_find_bin_dir(), br_find_data_dir(), br_find_etc_dir(), br_find_lib_dir(), br_find_libexec_dir(), br_find_locale_dir(), and br_find_sbin_dir().

◆ br_dirname()

char * br_dirname ( const char *  path)

Extracts the directory component of a path.

Similar to g_dirname() or the dirname commandline application.

Example:

br_dirname ("/usr/local/foobar"); --> Returns: "/usr/local"
char * br_dirname(const char *path)
Definition BinReloc.cpp:776
Parameters
pathA path.
Returns
A directory name. This string should be freed when no longer needed.

Definition at line 776 of file BinReloc.cpp.

777{
778 const char *end;
779 char * 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 free (result);
793 return strdup ("/");
794 } else
795 return result;
796}
#define NULL
Definition BinReloc.cpp:317

References NULL.

Referenced by br_find_exe_dir(), and br_find_prefix().

◆ br_find_bin_dir()

char * br_find_bin_dir ( const char *  default_bin_dir)

Locate the application's binary folder.

The path is generated by the following pseudo-code evaluation:

prefix + "/bin"
Parameters
default_bin_dirA default path which will used as fallback.
Returns
A string containing the bin folder's path, which must be freed when no longer necessary. If BinReloc is not initialized, or if the initialization function failed, then a copy of default_bin_dir will be returned. If default_bin_dir is NULL, then NULL will be returned.

Definition at line 466 of file BinReloc.cpp.

467{
468 char *prefix, *dir;
469
470 prefix = br_find_prefix ((const char *) NULL);
471 if (prefix == (char *) NULL) {
472 /* BinReloc not initialized. */
473 if (default_bin_dir != (const char *) NULL)
474 return strdup (default_bin_dir);
475 else
476 return (char *) NULL;
477 }
478
479 dir = br_build_path (prefix, "bin");
480 free (prefix);
481 return dir;
482}
char * br_find_prefix(const char *default_prefix)
Definition BinReloc.cpp:433
char * br_build_path(const char *dir, const char *file)
Definition BinReloc.cpp:718

References br_build_path(), br_find_prefix(), and NULL.

◆ br_find_data_dir()

char * br_find_data_dir ( const char *  default_data_dir)

Locate the application's data folder.

The path is generated by the following pseudo-code evaluation:

prefix + "/share"
Parameters
default_data_dirA default path which will used as fallback.
Returns
A string containing the data folder's path, which must be freed when no longer necessary. If BinReloc is not initialized, or if the initialization function failed, then a copy of default_data_dir will be returned. If default_data_dir is NULL, then NULL will be returned.

Definition at line 533 of file BinReloc.cpp.

534{
535 char *prefix, *dir;
536
537 prefix = br_find_prefix ((const char *) NULL);
538 if (prefix == (char *) NULL) {
539 /* BinReloc not initialized. */
540 if (default_data_dir != (const char *) NULL)
541 return strdup (default_data_dir);
542 else
543 return (char *) NULL;
544 }
545
546 dir = br_build_path (prefix, "share");
547 free (prefix);
548 return dir;
549}

References br_build_path(), br_find_prefix(), and NULL.

Referenced by br_find_locale_dir().

◆ br_find_etc_dir()

char * br_find_etc_dir ( const char *  default_etc_dir)

Locate the application's configuration files folder.

The path is generated by the following pseudo-code evaluation:

prefix + "/etc"
Parameters
default_etc_dirA default path which will used as fallback.
Returns
A string containing the etc folder's path, which must be freed when no longer necessary. If BinReloc is not initialized, or if the initialization function failed, then a copy of default_etc_dir will be returned. If default_etc_dir is NULL, then NULL will be returned.

Definition at line 665 of file BinReloc.cpp.

666{
667 char *prefix, *dir;
668
669 prefix = br_find_prefix ((const char *) NULL);
670 if (prefix == (char *) NULL) {
671 /* BinReloc not initialized. */
672 if (default_etc_dir != (const char *) NULL)
673 return strdup (default_etc_dir);
674 else
675 return (char *) NULL;
676 }
677
678 dir = br_build_path (prefix, "etc");
679 free (prefix);
680 return dir;
681}

References br_build_path(), br_find_prefix(), and NULL.

◆ br_find_exe()

char * br_find_exe ( const char *  default_exe)

Find the canonical filename of the current application.

Parameters
default_exeA default filename which will be used as fallback.
Returns
A string containing the application's canonical filename, which must be freed when no longer necessary. If BinReloc is not initialized, or if br_init() failed, then a copy of default_exe will be returned. If default_exe is NULL, then NULL will be returned.

Definition at line 377 of file BinReloc.cpp.

378{
379 if (exe == (char *) NULL) {
380 /* BinReloc is not initialized. */
381 if (default_exe != (const char *) NULL)
382 return strdup (default_exe);
383 else
384 return (char *) NULL;
385 }
386 return strdup (exe);
387}

References NULL.

Referenced by PEBLPath::Initialize(), and PEBLInterpret().

◆ br_find_exe_dir()

char * br_find_exe_dir ( const char *  default_dir)

Locate the directory in which the current application is installed.

The prefix is generated by the following pseudo-code evaluation:

dirname(exename)
Parameters
default_dirA default directory which will used as fallback.
Returns
A string containing the directory, which must be freed when no longer necessary. If BinReloc is not initialized, or if the initialization function failed, then a copy of default_dir will be returned. If default_dir is NULL, then NULL will be returned.

Definition at line 405 of file BinReloc.cpp.

406{
407 if (exe == NULL) {
408 /* BinReloc not initialized. */
409 if (default_dir != NULL)
410 return strdup (default_dir);
411 else
412 return (char*)NULL;
413 }
414
415 return br_dirname (exe);
416}

References br_dirname(), and NULL.

Referenced by PEBLPath::Initialize(), LauncherConfig::LauncherConfig(), and main().

◆ br_find_lib_dir()

char * br_find_lib_dir ( const char *  default_lib_dir)

Locate the application's library folder.

The path is generated by the following pseudo-code evaluation:

prefix + "/lib"
Parameters
default_lib_dirA default path which will used as fallback.
Returns
A string containing the library folder's path, which must be freed when no longer necessary. If BinReloc is not initialized, or if the initialization function failed, then a copy of default_lib_dir will be returned. If default_lib_dir is NULL, then NULL will be returned.

Definition at line 599 of file BinReloc.cpp.

600{
601 char *prefix, *dir;
602
603 prefix = br_find_prefix ((const char *) NULL);
604 if (prefix == (char *) NULL) {
605 /* BinReloc not initialized. */
606 if (default_lib_dir != (const char *) NULL)
607 return strdup (default_lib_dir);
608 else
609 return (char *) NULL;
610 }
611
612 dir = br_build_path (prefix, "lib");
613 free (prefix);
614 return dir;
615}

References br_build_path(), br_find_prefix(), and NULL.

◆ br_find_libexec_dir()

char * br_find_libexec_dir ( const char *  default_libexec_dir)

Locate the application's libexec folder.

The path is generated by the following pseudo-code evaluation:

prefix + "/libexec"
Parameters
default_libexec_dirA default path which will used as fallback.
Returns
A string containing the libexec folder's path, which must be freed when no longer necessary. If BinReloc is not initialized, or if the initialization function failed, then a copy of default_libexec_dir will be returned. If default_libexec_dir is NULL, then NULL will be returned.

Definition at line 632 of file BinReloc.cpp.

633{
634 char *prefix, *dir;
635
636 prefix = br_find_prefix ((const char *) NULL);
637 if (prefix == (char *) NULL) {
638 /* BinReloc not initialized. */
639 if (default_libexec_dir != (const char *) NULL)
640 return strdup (default_libexec_dir);
641 else
642 return (char *) NULL;
643 }
644
645 dir = br_build_path (prefix, "libexec");
646 free (prefix);
647 return dir;
648}

References br_build_path(), br_find_prefix(), and NULL.

◆ br_find_locale_dir()

char * br_find_locale_dir ( const char *  default_locale_dir)

Locate the application's localization folder.

The path is generated by the following pseudo-code evaluation:

prefix + "/share/locale"
Parameters
default_locale_dirA default path which will used as fallback.
Returns
A string containing the localization folder's path, which must be freed when no longer necessary. If BinReloc is not initialized, or if the initialization function failed, then a copy of default_locale_dir will be returned. If default_locale_dir is NULL, then NULL will be returned.

Definition at line 566 of file BinReloc.cpp.

567{
568 char *data_dir, *dir;
569
570 data_dir = br_find_data_dir ((const char *) NULL);
571 if (data_dir == (char *) NULL) {
572 /* BinReloc not initialized. */
573 if (default_locale_dir != (const char *) NULL)
574 return strdup (default_locale_dir);
575 else
576 return (char *) NULL;
577 }
578
579 dir = br_build_path (data_dir, "locale");
580 free (data_dir);
581 return dir;
582}
char * br_find_data_dir(const char *default_data_dir)
Definition BinReloc.cpp:533

References br_build_path(), br_find_data_dir(), and NULL.

◆ br_find_prefix()

char * br_find_prefix ( const char *  default_prefix)

Locate the prefix in which the current application is installed.

The prefix is generated by the following pseudo-code evaluation:

dirname(dirname(exename))
Parameters
default_prefixA default prefix which will used as fallback.
Returns
A string containing the prefix, which must be freed when no longer necessary. If BinReloc is not initialized, or if the initialization function failed, then a copy of default_prefix will be returned. If default_prefix is NULL, then NULL will be returned.

Definition at line 433 of file BinReloc.cpp.

434{
435 char *dir1, *dir2;
436
437 if (exe == (char *) NULL) {
438 /* BinReloc not initialized. */
439 if (default_prefix != (const char *) NULL)
440 return strdup (default_prefix);
441 else
442 return (char *) NULL;
443 }
444
445 dir1 = br_dirname (exe);
446 dir2 = br_dirname (dir1);
447 free (dir1);
448 return dir2;
449}

References br_dirname(), and NULL.

Referenced by br_find_bin_dir(), br_find_data_dir(), br_find_etc_dir(), br_find_lib_dir(), br_find_libexec_dir(), br_find_sbin_dir(), and PEBLInterpret().

◆ br_find_sbin_dir()

char * br_find_sbin_dir ( const char *  default_sbin_dir)

Locate the application's superuser binary folder.

The path is generated by the following pseudo-code evaluation:

prefix + "/sbin"
Parameters
default_sbin_dirA default path which will used as fallback.
Returns
A string containing the sbin folder's path, which must be freed when no longer necessary. If BinReloc is not initialized, or if the initialization function failed, then a copy of default_sbin_dir will be returned. If default_bin_dir is NULL, then NULL will be returned.

Definition at line 499 of file BinReloc.cpp.

500{
501 char *prefix, *dir;
502
503 prefix = br_find_prefix ((const char *) NULL);
504 if (prefix == (char *) NULL) {
505 /* BinReloc not initialized. */
506 if (default_sbin_dir != (const char *) NULL)
507 return strdup (default_sbin_dir);
508 else
509 return (char *) NULL;
510 }
511
512 dir = br_build_path (prefix, "sbin");
513 free (prefix);
514 return dir;
515}

References br_build_path(), br_find_prefix(), and NULL.

◆ br_init()

int br_init ( BrInitError error)

Initialize the BinReloc library (for applications).

This function must be called before using any other BinReloc functions. It attempts to locate the application's canonical filename.

Note
If you want to use BinReloc for a library, then you should call br_init_lib() instead.
Parameters
errorIf BinReloc failed to initialize, then the error code will be stored in this variable. Set to NULL if you want to ignore this. See BrInitError for a list of error codes.
Returns
1 on success, 0 if BinReloc failed to initialize.

Definition at line 338 of file BinReloc.cpp.

339{
340 exe = _br_find_exe (error);
341 return exe != NULL;
342}

References NULL.

Referenced by PEBLPath::Initialize(), LauncherConfig::LauncherConfig(), main(), and PEBLInterpret().

◆ br_init_lib()

int br_init_lib ( BrInitError error)

Initialize the BinReloc library (for libraries).

This function must be called before using any other BinReloc functions. It attempts to locate the calling library's canonical filename.

Note
The BinReloc source code MUST be included in your library, or this function won't work correctly.
Parameters
errorIf BinReloc failed to initialize, then the error code will be stored in this variable. Set to NULL if you want to ignore this. See BrInitError for a list of error codes.
Returns
1 on success, 0 if a filename cannot be found.

Definition at line 360 of file BinReloc.cpp.

361{
362 exe = _br_find_exe_for_symbol ((const void *) "", error);
363 return exe != NULL;
364}

References NULL.

◆ br_strcat()

char * br_strcat ( const char *  str1,
const char *  str2 
)

Concatenate str1 and str2 to a newly allocated string.

Parameters
str1A string.
str2Another string.
Returns
A newly-allocated string. This string should be freed when no longer needed.

Definition at line 695 of file BinReloc.cpp.

696{
697 char *result;
698 size_t len1, len2;
699
700 if (str1 == NULL)
701 str1 = "";
702 if (str2 == NULL)
703 str2 = "";
704
705 len1 = strlen (str1);
706 len2 = strlen (str2);
707
708 result = (char *) malloc (len1 + len2 + 1);
709 memcpy (result, str1, len1);
710 memcpy (result + len1, str2, len2);
711 result[len1 + len2] = '\0';
712
713 return result;
714}

References NULL.

Referenced by br_build_path().