Find the path used to invoke the current process
#include <process.h> char * _cmdname( char * buff );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The _cmdname() function determines the full path that the current process was invoked from. If buff isn't NULL, _cmdname() copies the path into the buffer that buff points to.
A pointer to the pathname used to load the process, or NULL if an error occurred.
#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 |