199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: open.c,v 1.16 1997/01/28 09:41:03 pk Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This code is derived from software contributed to Berkeley by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Mach Operating System project at Carnegie-Mellon University.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @(#)open.c 8.1 (Berkeley) 6/11/93
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All Rights Reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Author: Alessandro Forin
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Permission to use, copy, modify and distribute this software and its
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation is hereby granted, provided that both the copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice and this permission notice appear in all copies of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * software, derivative works or modified versions, and any portions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * thereof, and that both notices appear in supporting documentation.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Carnegie Mellon requests users of this software to return to
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
199767f8919635c4928607450d9e0abb932109ceToomas Soome * School of Computer Science
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Carnegie Mellon University
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Pittsburgh PA 15213-3890
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * any improvements or extensions that they make and grant Carnegie the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * rights to redistribute these changes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct fs_ops *exclusive_file_system;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct open_file files[SOPEN_MAX];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeo_gethandle(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (fd = 0; fd < SOPEN_MAX; fd++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (files[fd].f_flags == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomeo_rainit(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_rabuf = malloc(SOPEN_RASIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_ralen = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_raoffset = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeopen(const char *fname, int mode)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct fs_ops *fs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_file *f;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fd, i, error, besterror;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *file;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((fd = o_gethandle()) == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EMFILE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome f = &files[fd];
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_flags = mode + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_dev = (struct devsw *)0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_ops = (struct fs_ops *)0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_offset = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_devdata = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome file = (char *)0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (exclusive_file_system != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs = exclusive_file_system;
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = (fs->fo_open)(fname, f);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto ok;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto fail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = devopen(f, fname, &file);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome (((f->f_flags & F_NODEV) == 0) && f->f_dev == (struct devsw *)0))
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* see if we opened a raw device; otherwise, 'file' is the file name. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (file == (char *)0 || *file == '\0') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_flags |= F_RAW;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_rabuf = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* pass file name to the different filesystem open routines */
199767f8919635c4928607450d9e0abb932109ceToomas Soome besterror = ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; file_system[i] != NULL; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs = file_system[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = (fs->fo_open)(file, f);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto ok;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error != EINVAL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome besterror = error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = besterror;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fail:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((f->f_flags & F_NODEV) == 0 && f->f_dev != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_dev->dv_close(f);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome devclose(f);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err:
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_flags = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ok:
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_ops = fs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome o_rainit(f);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}