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

/* vpopen - view path version of the open system call */

#include "vp.h"
#if defined(FOR_WIN32)
#include <sys/types.h>
#endif
#if defined(__BORLANDC__) || defined(FOR_WIN32)
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <ctype.h>
#endif

#define READ	0

vpopen(path, oflag)
char	*path;
int	oflag;
{
	char	buf[MAXPATH + 1];
	int	returncode;
	int	i;

#if defined(__BORLANDC__) || defined(FOR_WIN32)
	if ((returncode = open(compath(path), oflag)) == -1 &&
	    *path != '/' && *path != '\\' && *path != ':' && (oflag & O_RDONLY)) {
#else
	if ((returncode = open(path, oflag, 0666)) == -1 && *path != '/' &&
	    oflag == READ) {
#endif
		vpinit((char *) 0);
		for (i = 1; i < vpndirs; i++) {
			(void) sprintf(buf, "%s/%s", vpdirs[i], path);
#if defined(__BORLANDC__) || defined(FOR_WIN32)
			if ((returncode = open(compath(buf), oflag)) != -1) {
#else
			if ((returncode = open(compath(buf), oflag, 0666)) != -1) {
#endif
				break;
			}
		}
	}
	return(returncode);
}
