Example #1
0
File: input.c Project: JWasm/JWasm
FILE *SearchFile( const char *path, bool queue )
/**********************************************/
{
    FILE        *file = NULL;
    struct src_item *fl;
    const char  *fn;
    bool        isabs;
    char        fullpath[FILENAME_MAX];

    DebugMsg1(("SearchFile(%s) enter\n", path ));

    //_splitpath( path, drive, dir, fname, ext );
    //DebugMsg1(("SearchFile(): drive=%s, dir=%s, fname=%s, ext=%s\n", drive, dir, fname, ext ));
    fn = GetFNamePart( path );

    /* if no absolute path is given, then search in the directory
     * of the current source file first!
     * v2.11: various changes because field fullpath has been removed.
     */

    isabs = ISABS( path );
    //if ( dir[0] != '\\' && dir[0] != '/' ) {
    if ( !isabs ) {
        for ( fl = src_stack; fl ; fl = fl->next ) {
            if ( fl->type == SIT_FILE ) {
                const char  *fn2;
                char        *src;
                //_splitpath( GetFName( fl->srcfile )->fname, drive2, dir2, NULL, NULL );
                //DebugMsg1(("SearchFile(): curr src=%s, split into drive=%s, dir=%s\n", GetFName( fl->srcfile)->fname, drive2, dir2 ));
                src = GetFName( fl->srcfile )->fname;
                fn2 = GetFNamePart( src );
                if ( fn2 != src ) {
                    int i = fn2 - src;
                    /* v2.10: if there's a directory part, add it to the directory part of the current file.
                     * fixme: check that both parts won't exceed FILENAME_MAX!
                     * fixme: 'path' is relative, but it may contain a drive letter!
                     */
                    memcpy( fullpath, src, i );
                    strcpy( fullpath + i, path );
                    if ( (file = fopen( fullpath, "rb" )) != NULL ) {
                        DebugMsg1(("SearchFile(): file found, fopen(%s)=%X\n", fullpath, file ));
                        path = fullpath;
                    }
#ifdef DEBUG_OUT
                    else
                        DebugMsg1(("SearchFile(): fopen(%s) failed\n", fullpath ));
#endif
                }
                break;
            }
        }
    }
    if ( file == NULL ) {
        fullpath[0] = NULLC;
        file = fopen( path, "rb" );
        DebugMsg1(("SearchFile(): fopen(%s)=%X\n", path, file ));

        /* if the file isn't found yet and include paths have been set,
         * and NO absolute path is given, then search include dirs
         */
        if( file == NULL && ModuleInfo.g.IncludePath != NULL && !isabs ) {
            if ( (file = open_file_in_include_path( path, fullpath )) != NULL ) {
                DebugMsg1(("SearchFile(): open_file_in_include_path(%s)=%X [%s]\n", path, file, fullpath ));
                path = fullpath;
            }
#ifdef DEBUG_OUT
            else
                DebugMsg1(("SearchFile(): open_file_in_include_path(%s)=NULL\n", path ));
#endif
        }
        if( file == NULL ) {
            EmitErr( CANNOT_OPEN_FILE, path, ErrnoStr() );
            return( NULL );
        }
    }
    /* is the file to be added to the file stack?
     * assembly files usually are, but binary files ( INCBIN ) aren't.
     */
    if ( queue ) {
        fl = PushSrcItem( SIT_FILE, file );
        fl->srcfile = AddFile( path );
        FileCur->string_ptr = GetFName( fl->srcfile )->fname;
#if FILESEQ
        if ( Options.line_numbers && Parse_Pass == PASS_1 )
            AddFileSeq( fl->srcfile );
#endif
    }
    return( file );
}
Example #2
0
core_getpath	/* wrapped below: expand fname, return full path */
#else
char *
getpath	/* expand fname, return full path */
#endif
(
	char  *fname,
	char  *searchpath,
	int  mode
)
{
	static char  pname[PATH_MAX];
	char uname[512];
	char  *cp;
	int i;

	if (fname == NULL) { return(NULL); }

	pname[0] = '\0';		/* check for full specification */

	if (ISABS(fname)) { /* absolute path */
		strncpy(pname, fname, sizeof(pname)-1);
	} else {
		switch (*fname) {
			case '.':				/* relative to cwd */
				strncpy(pname, fname, sizeof(pname)-1);
				break;
			case '~':				/* relative to home directory */
				fname++;
				cp = uname;
				for (i=0;i<sizeof(uname)&&*fname!='\0'&&!ISDIRSEP(*fname);i++)
					*cp++ = *fname++;
				*cp = '\0';
				cp = gethomedir(uname, pname, sizeof(pname));
				if(cp == NULL) return NULL;
				strncat(pname, fname, sizeof(pname)-strlen(pname)-1);
				break;
		}
	}
	if (pname[0])		/* got it, check access if search requested */
		return(searchpath==NULL||access(pname,mode)==0 ? pname : NULL);

	if (searchpath == NULL) {			/* don't search */
		strncpy(pname, fname, sizeof(pname)-1);
		return(pname);
	}
	/* check search path */
	do {
		cp = pname;
		while (*searchpath && (*cp = *searchpath++) != PATHSEP) {
			cp++;
		}
		if (cp > pname && !ISDIRSEP(cp[-1])) {
			*cp++ = DIRSEP;
		}
		strncpy(cp, fname, sizeof(pname)-strlen(pname)-1);
		if (access(pname, mode) == 0)		/* file accessable? */
			return(pname);
	} while (*searchpath);
	/* not found */
	return(NULL);
}