FreeBSD/sys/contrib/dev/acpica/compiler/aslfiles.c
/*******************************************************************************
 *
 * FUNCTION:    FlMergePathnames
 *
 * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be NULL or
 *                                a zero length string.
 *              FilePathname    - The include filename from the source ASL.
 *
 * RETURN:      Merged pathname string
 *
 * DESCRIPTION: Merge two pathnames that (probably) have common elements, to
 *              arrive at a minimal length string. Merge can occur if the
 *              FilePathname is relative to the PrefixDir.
 *
 ******************************************************************************/
FlMergePathnames
PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be NULL or
a zero length string.
FilePathname    - The include filename from the source ASL.

Merged pathname string

Merge two pathnames that (probably) have common elements, to
arrive at a minimal length string. Merge can occur if the
FilePathname is relative to the PrefixDir.
FlMergePathnames PARAMETERS : PrefixDir - Prefix directory pathname . Can be NULL or a zero length string . FilePathname - The include filename from the source ASL . Merged pathname string Merge two pathnames that ( probably ) have common elements to arrive at a minimal length string . Merge can occur if the FilePathname is relative to the PrefixDir .
char *
FlMergePathnames (
    char                    *PrefixDir,
    char                    *FilePathname)
{
    char                    *CommonPath;
    char                    *Pathname;
    char                    *LastElement;
    DbgPrint (ASL_PARSE_OUTPUT, "Include: Prefix path - \"%s\"\n"
        "Include: FilePathname - \"%s\"\n",
         PrefixDir, FilePathname);
    if (!PrefixDir || (!*PrefixDir) ||
        (*FilePathname == '/') ||
         (FilePathname[1] == ':'))
    {
        Pathname = UtStringCacheCalloc (strlen (FilePathname) + 1);
        strcpy (Pathname, FilePathname);
        goto ConvertBackslashes;
    }
    CommonPath = UtStringCacheCalloc (strlen (PrefixDir) + 1);
    strcpy (CommonPath, PrefixDir);
    while (*FilePathname && (!strncmp (FilePathname, "../", 3)))
    {
        LastElement = strrchr (CommonPath, '/');
        if (!LastElement)
        {
            goto ConcatenatePaths;
        }
        *LastElement = 0;   
        FilePathname += 3;  
    }
    LastElement = strrchr (CommonPath, '/');
    if (LastElement)
    {
        *LastElement = 0;
    }
ConcatenatePaths:
    Pathname = UtStringCacheCalloc (
        strlen (CommonPath) + strlen (FilePathname) + 2);
    if (LastElement && *CommonPath)
    {
        strcpy (Pathname, CommonPath);
        strcat (Pathname, "/");
    }
    strcat (Pathname, FilePathname);
ConvertBackslashes:
    UtConvertBackslashes (Pathname);
    DbgPrint (ASL_PARSE_OUTPUT, "Include: Merged Pathname - \"%s\"\n",
         Pathname);
    return (Pathname);
}
char * FlMergePathnames ( char * PrefixDir , char * FilePathname ) { char * CommonPath ; char * Pathname ; char * LastElement ; DbgPrint ( ASL_PARSE_OUTPUT , " \" \" \n " " \" \" \n " , PrefixDir , FilePathname ) ; if ( ! PrefixDir || ( ! * PrefixDir ) || ( * FilePathname == ' ' ) || ( FilePathname [ 1 ] == ' ' ) ) { Pathname = UtStringCacheCalloc ( strlen ( FilePathname ) + 1 ) ; strcpy ( Pathname , FilePathname ) ; goto ConvertBackslashes ; } CommonPath = UtStringCacheCalloc ( strlen ( PrefixDir ) + 1 ) ; strcpy ( CommonPath , PrefixDir ) ; while ( * FilePathname && ( ! strncmp ( FilePathname , " " , 3 ) ) ) { LastElement = strrchr ( CommonPath , ' ' ) ; if ( ! LastElement ) { goto ConcatenatePaths ; } * LastElement = 0 ; FilePathname += 3 ; } LastElement = strrchr ( CommonPath , ' ' ) ; if ( LastElement ) { * LastElement = 0 ; } ConcatenatePaths : Pathname = UtStringCacheCalloc ( strlen ( CommonPath ) + strlen ( FilePathname ) + 2 ) ; if ( LastElement && * CommonPath ) { strcpy ( Pathname , CommonPath ) ; strcat ( Pathname , " " ) ; } strcat ( Pathname , FilePathname ) ; ConvertBackslashes : UtConvertBackslashes ( Pathname ) ; DbgPrint ( ASL_PARSE_OUTPUT , " \" \" \n " , Pathname ) ; return ( Pathname ) ; }
FUNCTION:    FlMergePathnames
 PARAMETERS:  PrefixDir       - Prefix directory pathname.
FUNCTION : FlMergePathnames PARAMETERS : PrefixDir - Prefix directory pathname .
/*
     * If there is no prefix directory or if the file pathname is absolute,
     * just return the original file pathname
     */ /* Need a local copy of the prefix directory path */ /*
     * Walk forward through the file path, and simultaneously backward
     * through the prefix directory path until there are no more
     * relative references at the start of the file path.
     */ /* Remove last element of the prefix directory path */ /* Terminate CommonPath string */ /* Point to next path element */ /*
     * Remove the last element of the prefix directory path (it is the same as
     * the first element of the file pathname), and build the final merged
     * pathname.
     */ /* Build the final merged pathname */ /* Convert all backslashes to normal slashes */