Posted to tcl by chw at Thu Apr 25 06:13:23 GMT 2019view raw

  1. #define _GNU_SOURCE
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <dlfcn.h>
  5.  
  6. /*
  7. * Find containing shared library and return its
  8. * file name in allocated string which the caller
  9. * must free. Might work at least on Linux.
  10. */
  11.  
  12. char *self_discovery(void)
  13. {
  14. void *dl;
  15. int (*dla)(void *, Dl_info *);
  16. Dl_info dli;
  17. char *result = NULL;
  18.  
  19. dl = dlopen("libdl.so.2", RTLD_NOW);
  20. if (dl) {
  21. dla = dlsym(dl, "dladdr");
  22. if (dla) {
  23. memset(&dli, 0, sizeof(dli));
  24. if (dla(self_discovery, &dli) && dli.dli_sname && dli.dli_fname[0]) {
  25. result = malloc(strlen(dli.dli_fname) + 1);
  26. if (result) {
  27. strcpy(result, dli.dli_fname);
  28. }
  29. }
  30. }
  31. dlclose(dl);
  32. }
  33. return result;
  34. }
  35.  

Comments

Posted by chw at Thu Apr 25 06:14:51 GMT 2019 [text] [code]

hypnotoad: we talked yesterday about embedding zip file systems in shared libraries...