_cmdname()

Updated: October 26, 2022

Find the path used to invoke the current process

Synopsis:

#include <process.h>

char * _cmdname( char * buff );

Arguments:

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.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

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.

Returns:

A pointer to the pathname used to load the process, or NULL if an error occurred.

Note: Don't change the string that the returned value points to if you passed NULL for the buff parameter.

Examples:

#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".

Classification:

QNX Neutrino

Safety:  
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes