![]() |
![]() |
![]() |
Find the path used to invoke the current process
#include <process.h> char * _cmdname( char * buff );
libc
The _cmdname() function determines the fullpath that the current process was invoked from and stores it in the buffer specified by buff.
To determine the size of the buff argument, call fpathconf() or pathconf() with an argument of _PC_PATH_MAX.
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <limits.h> #include <process.h> int main( void ) { size_t maximum_path; char *buff; maximum_path = (size_t) pathconf( "/", _PC_PATH_MAX ); buff = (char* )malloc( maximum_path ); if( _cmdname( buff ) ) { printf( "I'm \"%s\".\n", buff ); } else { perror( "_cmdname() failed" ); free (buff); return EXIT_FAILURE; } free (buff); return EXIT_SUCCESS; }
If this code is compiled into an executable named foo:
# ls -F /home/xyzzy/bin/foo foo* # /home/xyzzy/bin/foo I'm "/home/xyzzy/bin/foo".
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
![]() |
![]() |
![]() |