#include <stdio.h>
#include <unistd.h>
char* getexecpath() {
static char buf[30], execpath[2048];
sprintf(buf,"/proc/%d/exe", getpid()); /* [1] Linux */
/* sprintf(buf,"/proc/%d/file", getpid()); /* [2] FreeBSD (Ergänzung -- hl) */
memset(execpath, 0, sizeof(execpath));
readlink(buf, execpath, sizeof(execpath));
return execpath;
}
int main(int argc, char *argv[]) {
printf("execpath: %s\n", getexecpath());
remove(getexecpath()); /* [3] */
printf("execpath: %s\n", getexecpath()); /* [4] */
return 0;
} |