/*
 *   Copyright (C) 1988-2006 Lucent Technologies Inc. and others.
 *   All Rights Reserved.
 */

/* vpfopen - view path version of the fopen library function */

#include <stdio.h>
#ifdef __BORLANDC__
#include <ctype.h>
#endif
#include "vp.h"

FILE *
vpfopen(filename, type)
char	*filename, *type;
{
	char	buf[MAXPATH + 1];
	FILE	*returncode;
	int	i;

#if defined(__BORLANDC__) || defined(FOR_WIN32)
	compath(filename);
	if ((returncode = fopen(filename, type)) == NULL && filename[0] != '/' && filename[0] != '\\' &&
#else
	if ((returncode = fopen(filename, type)) == NULL && filename[0] != '/' &&
#endif
#if defined(__BORLANDC__) || defined(FOR_WIN32)
	    *type == 'r'	/* allow for "rt" or "rb" */
#else
	    strcmp(type, "r") == 0
#endif
	    ) {
		vpinit((char *) 0);
		for (i = 1; i < vpndirs; i++) {
			(void) sprintf(buf, "%s/%s", vpdirs[i], filename);
			if ((returncode = fopen(compath(buf), type)) != NULL) {
				break;
			}

		}
	}
	return(returncode);
}
