138451Smsmith/* $NetBSD: open.c,v 1.16 1997/01/28 09:41:03 pk Exp $ */ 238451Smsmith 338451Smsmith/*- 438451Smsmith * Copyright (c) 1993 538451Smsmith * The Regents of the University of California. All rights reserved. 638451Smsmith * 738451Smsmith * This code is derived from software contributed to Berkeley by 838451Smsmith * The Mach Operating System project at Carnegie-Mellon University. 938451Smsmith * 1038451Smsmith * Redistribution and use in source and binary forms, with or without 1138451Smsmith * modification, are permitted provided that the following conditions 1238451Smsmith * are met: 1338451Smsmith * 1. Redistributions of source code must retain the above copyright 1438451Smsmith * notice, this list of conditions and the following disclaimer. 1538451Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1638451Smsmith * notice, this list of conditions and the following disclaimer in the 1738451Smsmith * documentation and/or other materials provided with the distribution. 1838451Smsmith * 4. Neither the name of the University nor the names of its contributors 1938451Smsmith * may be used to endorse or promote products derived from this software 2038451Smsmith * without specific prior written permission. 2138451Smsmith * 2238451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2338451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2438451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2538451Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2638451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2738451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2838451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2938451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3038451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3138451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3238451Smsmith * SUCH DAMAGE. 3338451Smsmith * 3438451Smsmith * @(#)open.c 8.1 (Berkeley) 6/11/93 3538451Smsmith * 36344291Skevans * 3738451Smsmith * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University 3838451Smsmith * All Rights Reserved. 3938451Smsmith * 4038451Smsmith * Author: Alessandro Forin 41344291Skevans * 4238451Smsmith * Permission to use, copy, modify and distribute this software and its 4338451Smsmith * documentation is hereby granted, provided that both the copyright 4438451Smsmith * notice and this permission notice appear in all copies of the 4538451Smsmith * software, derivative works or modified versions, and any portions 4638451Smsmith * thereof, and that both notices appear in supporting documentation. 47344291Skevans * 4838451Smsmith * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 4938451Smsmith * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 5038451Smsmith * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51344291Skevans * 5238451Smsmith * Carnegie Mellon requests users of this software to return to 53344291Skevans * 5438451Smsmith * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 5538451Smsmith * School of Computer Science 5638451Smsmith * Carnegie Mellon University 5738451Smsmith * Pittsburgh PA 15213-3890 58344291Skevans * 5938451Smsmith * any improvements or extensions that they make and grant Carnegie the 6038451Smsmith * rights to redistribute these changes. 6138451Smsmith */ 6238451Smsmith 6384221Sdillon#include <sys/cdefs.h> 6484221Sdillon__FBSDID("$FreeBSD: stable/11/stand/libsa/open.c 344291 2019-02-19 18:50:20Z kevans $"); 6584221Sdillon 6638451Smsmith#include "stand.h" 6738451Smsmith 68269308Smarcelstruct fs_ops *exclusive_file_system; 69269308Smarcel 7038451Smsmithstruct open_file files[SOPEN_MAX]; 7138451Smsmith 7238451Smsmithstatic int 73344291Skevanso_gethandle(void) 7438451Smsmith{ 75344291Skevans int fd; 76344291Skevans 77344291Skevans for (fd = 0; fd < SOPEN_MAX; fd++) 78344291Skevans if (files[fd].f_flags == 0) 79344291Skevans return (fd); 80344291Skevans return (-1); 8138451Smsmith} 8238451Smsmith 8365470Smsmithstatic void 8465470Smsmitho_rainit(struct open_file *f) 8565470Smsmith{ 86344291Skevans f->f_rabuf = malloc(SOPEN_RASIZE); 87344291Skevans f->f_ralen = 0; 88344291Skevans f->f_raoffset = 0; 8965470Smsmith} 9038451Smsmith 9138451Smsmithint 9238451Smsmithopen(const char *fname, int mode) 9338451Smsmith{ 94344291Skevans struct fs_ops *fs; 95344291Skevans struct open_file *f; 96344291Skevans int fd, i, error, besterror; 97344291Skevans const char *file; 9838451Smsmith 99344291Skevans if ((fd = o_gethandle()) == -1) { 100344291Skevans errno = EMFILE; 101344291Skevans return (-1); 102344291Skevans } 10338451Smsmith 104344291Skevans f = &files[fd]; 105344291Skevans f->f_flags = mode + 1; 106344291Skevans f->f_dev = NULL; 107344291Skevans f->f_ops = NULL; 108344291Skevans f->f_offset = 0; 109344291Skevans f->f_devdata = NULL; 110344291Skevans file = NULL; 111269308Smarcel 112344291Skevans if (exclusive_file_system != NULL) { 113344291Skevans fs = exclusive_file_system; 114344291Skevans error = (fs->fo_open)(fname, f); 115344291Skevans if (error == 0) 116344291Skevans goto ok; 117344291Skevans goto err; 118344291Skevans } 119269308Smarcel 120344291Skevans error = devopen(f, fname, &file); 121344291Skevans if (error || 122344291Skevans (((f->f_flags & F_NODEV) == 0) && f->f_dev == NULL)) 123344291Skevans goto err; 12438451Smsmith 125344291Skevans /* see if we opened a raw device; otherwise, 'file' is the file name. */ 126344291Skevans if (file == NULL || *file == '\0') { 127344291Skevans f->f_flags |= F_RAW; 128344291Skevans f->f_rabuf = NULL; 129344291Skevans return (fd); 130344291Skevans } 13138451Smsmith 132344291Skevans /* pass file name to the different filesystem open routines */ 133344291Skevans besterror = ENOENT; 134344291Skevans for (i = 0; file_system[i] != NULL; i++) { 135344291Skevans fs = file_system[i]; 136344291Skevans error = (fs->fo_open)(file, f); 137344291Skevans if (error == 0) 138344291Skevans goto ok; 139344291Skevans if (error != EINVAL) 140344291Skevans besterror = error; 141344291Skevans } 142344291Skevans error = besterror; 14338451Smsmith 144344291Skevans if ((f->f_flags & F_NODEV) == 0 && f->f_dev != NULL) 145344291Skevans f->f_dev->dv_close(f); 146344291Skevans if (error) 147344291Skevans devclose(f); 14838451Smsmith 149344291Skevanserr: 150344291Skevans f->f_flags = 0; 151344291Skevans errno = error; 152344291Skevans return (-1); 153269308Smarcel 154344291Skevansok: 155344291Skevans f->f_ops = fs; 156344291Skevans o_rainit(f); 157344291Skevans return (fd); 15838451Smsmith} 159