Вернуть путь, используемый для обращения к текущему процессу
#include <process.h>char * _cmdname( char *buff );
NULL
, or a pointer to a buffer in which the function can store the path. To determine the size required for the buffer, call fpathconf() or pathconf() with an argument of _PC_PATH_MAX
, then add 1
for the terminating null character.libc
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.
![]() | Don't change the string that the returned value points to if you passed NULL for the buff parameter. |
#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".
ЗОСРВ «Нейтрино»
basename(), _cmdfd(), __progname
Предыдущий раздел: Описание API системной библиотеки