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

#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <dlfcn.h>

/*
 * Find containing shared library and return its
 * file name in allocated string which the caller
 * must free. Might work at least on Linux.
 */

char *self_discovery(void)
{
  void *dl;
  int (*dla)(void *, Dl_info *);
  Dl_info dli;
  char *result = NULL;

  dl = dlopen("libdl.so.2", RTLD_NOW);
  if (dl) {
    dla = dlsym(dl, "dladdr");
    if (dla) {
      memset(&dli, 0, sizeof(dli));
      if (dla(self_discovery, &dli) && dli.dli_sname && dli.dli_fname[0]) {
        result = malloc(strlen(dli.dli_fname) + 1);
        if (result) {
          strcpy(result, dli.dli_fname);
        }
      }
    }
    dlclose(dl);
  }
  return result;
}

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...