Skip to content

Instantly share code, notes, and snippets.

@rightfold
Created May 21, 2015 17:41
Show Gist options
  • Save rightfold/2046d79c64fe8ea60256 to your computer and use it in GitHub Desktop.
Save rightfold/2046d79c64fe8ea60256 to your computer and use it in GitHub Desktop.
typedef int (*intfunc)(int);
typedef void (*pfunc)(int*, int);
pfunc partial(intfunc fin)
{
pfunc f;
static int idx = 0;
char cc[256], lib[256];
FILE *fp;
sprintf(lib, "/tmp/stuff%d.so", ++idx);
sprintf(cc, "cc -pipe -x c -shared -o %s -", lib);
fp = popen(cc, "w");
fprintf(fp, "#define t typedef\xat int _i,*i;t _i(*__)(_i);__ p =(__)%p;"
"void _(i _1, _i l){while(--l>-1)l[_1]=p(l[_1]);}", fin);
fclose(fp);
*(void **)(&f) = dlsym(dlopen(lib, RTLD_LAZY), "_");
unlink(lib);
return f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment