Esempio n. 1
0
File: cpp2.c Progetto: WangHoi/slc
ReturnCode openinclude( struct Global *global,
    char *filename,     /* Input file name         */
    int searchlocal )   /* TRUE if #include "file" */
{
    /*
     * Actually open an include file.  This routine is only called from
     * doinclude() above, but was written as a separate subroutine for
     * programmer convenience.  It searches the list of directories
     * and actually opens the file, linking it into the list of
     * active files.  Returns ReturnCode. No error message is printed.
     */

    char **incptr;
    char tmpname[NWORK]; /* Filename work area    */
    size_t len;

    if( filename[0] == '/' )
        {
        if( ! openfile( global, filename ) )
            return(FPP_OK);
        }

    if( searchlocal )
        {
        /*
         * Look in local directory first.
         * Try to open filename relative to the directory of the current
         * source file (as opposed to the current directory). (ARF, SCK).
         * Note that the fully qualified pathname is always built by
         * discarding the last pathname component of the source file
         * name then tacking on the #include argument.
         */
        if( hasdirectory( global->infile->filename, tmpname ) )
            strcat( tmpname, filename );
        else
            strcpy( tmpname, filename );

        if( ! openfile( global, tmpname ) )
            return(FPP_OK);
        }

    /*
     * Look in any directories specified by -I command line
     * arguments, then in the builtin search list.
     */
    for( incptr = global->incdir; incptr < global->incend; incptr++ )
        {
        len = strlen(*incptr);

        if( len + strlen(filename) >= sizeof(tmpname) )
            {
            cfatal( global, FATAL_FILENAME_BUFFER_OVERFLOW );

            return( FPP_FILENAME_BUFFER_OVERFLOW );
            }
        else
            {
            if( (*incptr)[len-1] != '/' )
                sprintf( tmpname, "%s/%s", *incptr, filename );
            else
                sprintf( tmpname, "%s%s", *incptr, filename );

            if( !openfile( global, tmpname ) )
                return(FPP_OK);
            }
        }

    return( FPP_NO_INCLUDE );
}
Esempio n. 2
0
File: cpp2.c Progetto: zzz99977/core
/*
 * Actually open an include file.  This routine is only called from
 * doinclude() above, but was written as a separate subroutine for
 * programmer convenience.  It searches the list of directories
 * and actually opens the file, linking it into the list of
 * active files.  Returns TRUE if the file was opened, FALSE
 * if openinclude() fails.  No error message is printed.
 */
FILE_LOCAL int openinclude(char* filename, int searchlocal)
{
    char** incptr;
    char tmpname[NFWORK]; /* Filename work area   */

    if (searchlocal)
    {
        /*
         * Look in local directory first
         */
#if HOST == SYS_UNIX
        /*
         * Try to open filename relative to the directory of the current
         * source file (as opposed to the current directory). (ARF, SCK).
         */
        if (filename[0] != '/' &&
            hasdirectory(infile->filename, tmpname, NFWORK))
        {
            int len = strlen(tmpname);
            int len2 = strlen(filename);
            if(len + len2 < NFWORK)
            {
                memcpy(tmpname + len, filename, len2);
                tmpname[len + len2] = 0;
            }
            else
            {
                cfatal("Filename work buffer overflow", NULLST);
            }
        }
        else
        {
            int len = strlen(filename);
            if(len < NFWORK)
            {
                memcpy(tmpname, filename, len);
                tmpname[len] = 0;
            }
            else
            {
                cfatal("Filename work buffer overflow", NULLST);
            }
        }
#else
        if (!hasdirectory(filename, tmpname, NFWORK) &&
            hasdirectory(infile->filename, tmpname, NFWORK))
        {
            strcat(tmpname, filename);
        }
        else
        {
            strcpy(tmpname, filename);
        }
#endif
        if (openfile(tmpname))
            return TRUE;
    }
    /*
     * Look in any directories specified by -I command line
     * arguments, then in the builtin search list.
     */
    for (incptr = incdir; incptr < incend; incptr++)
    {
        if (strlen(*incptr) + strlen(filename) >= (NFWORK - 1))
            cfatal("Filename work buffer overflow", NULLST);
        else
        {
#if HOST == SYS_UNIX
            if (filename[0] == '/')
                strcpy(tmpname, filename);
            else
                sprintf(tmpname, "%s/%s", *incptr, filename);

#elif HOST == SYS_UNKNOWN
            if (filename[0] == '\\')
                strcpy(tmpname, filename);
            else
                sprintf(tmpname, "%s\\%s", *incptr, filename);
#else
            if (!hasdirectory(filename, tmpname, NFWORK))
                sprintf(tmpname, "%s%s", *incptr, filename);
#endif
            if (openfile(tmpname))
                return TRUE;
        }
    }
    return FALSE;
}