Discussion:
process full path on Unixware 7.1.1
(too old to reply)
adara
2004-01-09 14:13:01 UTC
Permalink
hi all,
is there any way to get the process full path in C (not using argv)
example will be great !!
Thanks.
J. L. Schilling
2004-01-10 03:48:25 UTC
Permalink
Post by adara
is there any way to get the process full path in C (not using argv)
example will be great !!
Try using dladdr() on main, then realpath() on that. Example attached.

Jonathan Schilling

$ cat fullpath.c
#include <stdio.h>
#include <dlfcn.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/param.h>

int main() {
Dl_info dli;
int ret = dladdr((void*)main, &dli);
assert(ret);
char buf[MAXPATHLEN];
void* p = realpath(dli.dli_fname, buf);
assert(p);
printf("main() is contained in file %s\n", buf);
}
$ cc -o fullpath fullpath.c
$ ./fullpath
main() is contained in file /lfs/home/jls/test/fullpath
$ cd /tmp
$ /home/jls/test/fullpath
main() is contained in file /lfs/home/jls/test/fullpath
adara
2004-01-11 06:22:25 UTC
Permalink
Post by J. L. Schilling
Post by adara
is there any way to get the process full path in C (not using argv)
example will be great !!
Try using dladdr() on main, then realpath() on that. Example attached.
Jonathan Schilling
$ cat fullpath.c
#include <stdio.h>
#include <dlfcn.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/param.h>
int main() {
Dl_info dli;
int ret = dladdr((void*)main, &dli);
assert(ret);
char buf[MAXPATHLEN];
void* p = realpath(dli.dli_fname, buf);
assert(p);
printf("main() is contained in file %s\n", buf);
}
$ cc -o fullpath fullpath.c
$ ./fullpath
main() is contained in file /lfs/home/jls/test/fullpath
$ cd /tmp
$ /home/jls/test/fullpath
main() is contained in file /lfs/home/jls/test/fullpath
Great help ,Thank you.

Loading...