pendant zu _pgmptr bzw. GetModuleName()
-
Hallo!
ich da mal folgende frage:
gibt es unter linux eine funktion die das gegenstück zu dem oben erwähnten bildet? also eine funktion/konstante/... die mir den vollständigen (absoluten) pfad (und dateiennamen) zu meinem programm zurückgibt?also z.b. "/usr/bin/test.bin"?
thx im voraus schon mal
-
Den Pfad zu deinem Programm erhälst du als erstes Argument, das deinem Programm übergeben wird.
int main(int argc, char* argv[]) { argv[0]; /* der relative Pfad */ return 0; }
Du kannst die Funtkion realpath benutzten, um den absoluten Pfad zu bekommen.
#include <limits.h> #include <stdlib.h> char *realpath(const char *path, char *resolved_path);
-
#include <stdio.h> #include <limits.h> #include <stdlib.h> int main(int argc, char* argv[]) { char* path = realpath(argv[0], NULL); printf("%s\n", path); return 0; }
Wobei vielleicht folgendes anzumerken ist:
NOTES The glibc implementation of realpath() provides a non-standard exten- sion. If resolved_path is specified as NULL, then realpath() uses mal- loc(3) to allocate a buffer of up to PATH_MAX bytes to hold the resolved pathname, and returns a pointer to this buffer. The caller should deallocate this buffer using free(3). BUGS Avoid using this function. It is broken by design since (unless using the non-standard resolved_path == NULL feature) it is impossible to determine a suitable size for the output buffer, resolved_path. According to POSIX a buffer of size PATH_MAX suffices, but PATH_MAX need not be a defined constant, and may have to be obtained using path- conf(). And asking pathconf() does not really help, since on the one hand POSIX warns that the result of pathconf() may be huge and unsuit- able for mallocing memory. And on the other hand pathconf() may return -1 to signify that PATH_MAX is not bounded. The libc4 and libc5 implementation contains a buffer overflow (fixed in libc-5.4.13). Thus, set-user-ID programs like mount need a private version.
-
vielen dank!!
das war genau das was ich gesucht habe!
-
gibt es keine saubere lösung?
-
-
antifrickel schrieb:
gibt es keine saubere lösung?
wasn da unsauber?
-
Avoid using this function.
-
ups, hatte das gar nicht gelesen.
Muss man halt die non-standard-Version nehmen. Oder sich Gedanken machen, wozu man das überhaupt braucht.