Directory/RB_Get_FileName [ Functions ]

NAME

RB_Get_PathName -- extract the file name

SYNOPSIS

char               *RB_Get_FileName(
    char *arg_fullpath )

FUNCTION

Given a full path to a file, that is a filename, or a filename prefixed with a pathname, return the filename. So

      "./filename"           returns "filename"
      "/home/et/filename"    returns "filename"
      "filename"             return  "filename"

INPUTS

arg_fullpath -- a full path to a file, with or without a path.

RESULT

0 -- The full path did not contain a filename pointer to the filename -- otherwise.

NOTES

You are responsible for deallocating it.

SOURCE

{
    int                 n;
    int                 i;
    int                 found;
    char               *result = 0;

    assert( arg_fullpath );

    n = strlen( arg_fullpath );

    /* Try and find a path character ( ':' or '/' ) */
    for ( found = FALSE, i = 0; i < n; ++i )
    {
        if ( RB_Is_PathCharacter( arg_fullpath[i] ) )
        {
            found = TRUE;
            break;
        }
    }

    if ( !found )
    {
        /* The full path does not contain a pathname,
         * so we can return the arg_fullpath as
         * the filename.
         */
        result = RB_StrDup( arg_fullpath );
    }
    else
    {
        /* The full path does contain a pathname,
         * strip this and return the remainder
         */

        for ( i = n - 1; i > 0; --i )
        {
            if ( RB_Is_PathCharacter( arg_fullpath[i] ) )
            {
                assert( i < ( n - 1 ) );
                result = RB_StrDup( &( arg_fullpath[i + 1] ) );
                break;
            }
        }
    }

    return result;
}