open.c revision 65470
165470Smsmith/* $FreeBSD: head/lib/libstand/open.c 65470 2000-09-05 09:52:50Z msmith $ */ 238451Smsmith/* $NetBSD: open.c,v 1.16 1997/01/28 09:41:03 pk Exp $ */ 338451Smsmith 438451Smsmith/*- 538451Smsmith * Copyright (c) 1993 638451Smsmith * The Regents of the University of California. All rights reserved. 738451Smsmith * 838451Smsmith * This code is derived from software contributed to Berkeley by 938451Smsmith * The Mach Operating System project at Carnegie-Mellon University. 1038451Smsmith * 1138451Smsmith * Redistribution and use in source and binary forms, with or without 1238451Smsmith * modification, are permitted provided that the following conditions 1338451Smsmith * are met: 1438451Smsmith * 1. Redistributions of source code must retain the above copyright 1538451Smsmith * notice, this list of conditions and the following disclaimer. 1638451Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1738451Smsmith * notice, this list of conditions and the following disclaimer in the 1838451Smsmith * documentation and/or other materials provided with the distribution. 1938451Smsmith * 3. All advertising materials mentioning features or use of this software 2038451Smsmith * must display the following acknowledgement: 2138451Smsmith * This product includes software developed by the University of 2238451Smsmith * California, Berkeley and its contributors. 2338451Smsmith * 4. Neither the name of the University nor the names of its contributors 2438451Smsmith * may be used to endorse or promote products derived from this software 2538451Smsmith * without specific prior written permission. 2638451Smsmith * 2738451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2838451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2938451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3038451Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3138451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3238451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3338451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3438451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3538451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3638451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3738451Smsmith * SUCH DAMAGE. 3838451Smsmith * 3938451Smsmith * @(#)open.c 8.1 (Berkeley) 6/11/93 4038451Smsmith * 4138451Smsmith * 4238451Smsmith * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University 4338451Smsmith * All Rights Reserved. 4438451Smsmith * 4538451Smsmith * Author: Alessandro Forin 4638451Smsmith * 4738451Smsmith * Permission to use, copy, modify and distribute this software and its 4838451Smsmith * documentation is hereby granted, provided that both the copyright 4938451Smsmith * notice and this permission notice appear in all copies of the 5038451Smsmith * software, derivative works or modified versions, and any portions 5138451Smsmith * thereof, and that both notices appear in supporting documentation. 5238451Smsmith * 5338451Smsmith * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 5438451Smsmith * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 5538451Smsmith * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 5638451Smsmith * 5738451Smsmith * Carnegie Mellon requests users of this software to return to 5838451Smsmith * 5938451Smsmith * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 6038451Smsmith * School of Computer Science 6138451Smsmith * Carnegie Mellon University 6238451Smsmith * Pittsburgh PA 15213-3890 6338451Smsmith * 6438451Smsmith * any improvements or extensions that they make and grant Carnegie the 6538451Smsmith * rights to redistribute these changes. 6638451Smsmith */ 6738451Smsmith 6838451Smsmith#include "stand.h" 6938451Smsmith 7038451Smsmithstruct open_file files[SOPEN_MAX]; 7138451Smsmith 7238451Smsmithstatic int 7338451Smsmitho_gethandle(void) 7438451Smsmith{ 7538451Smsmith int fd; 7638451Smsmith 7738451Smsmith for (fd = 0; fd < SOPEN_MAX; fd++) 7838451Smsmith if (files[fd].f_flags == 0) 7938451Smsmith return(fd); 8038451Smsmith return(-1); 8138451Smsmith} 8238451Smsmith 8365470Smsmithstatic void 8465470Smsmitho_rainit(struct open_file *f) 8565470Smsmith{ 8665470Smsmith f->f_rabuf = malloc(SOPEN_RASIZE); 8765470Smsmith f->f_ralen = 0; 8865470Smsmith f->f_raoffset = 0; 8965470Smsmith} 9038451Smsmith 9138451Smsmithint 9238451Smsmithopen(const char *fname, int mode) 9338451Smsmith{ 9438451Smsmith struct open_file *f; 9538451Smsmith int fd, i, error, besterror; 9639672Sdfr const char *file; 9738451Smsmith 9838451Smsmith if ((fd = o_gethandle()) == -1) { 9938451Smsmith errno = EMFILE; 10038451Smsmith return(-1); 10138451Smsmith } 10238451Smsmith 10338451Smsmith f = &files[fd]; 10438451Smsmith f->f_flags = mode + 1; 10538451Smsmith f->f_dev = (struct devsw *)0; 10638451Smsmith f->f_ops = (struct fs_ops *)0; 10738451Smsmith f->f_offset = 0; 10838451Smsmith f->f_devdata = NULL; 10938451Smsmith file = (char *)0; 11038451Smsmith error = devopen(f, fname, &file); 11138451Smsmith if (error || 11238451Smsmith (((f->f_flags & F_NODEV) == 0) && f->f_dev == (struct devsw *)0)) 11338451Smsmith goto err; 11438451Smsmith 11538451Smsmith /* see if we opened a raw device; otherwise, 'file' is the file name. */ 11638451Smsmith if (file == (char *)0 || *file == '\0') { 11738451Smsmith f->f_flags |= F_RAW; 11838451Smsmith return (fd); 11938451Smsmith } 12038451Smsmith 12138451Smsmith /* pass file name to the different filesystem open routines */ 12238451Smsmith besterror = ENOENT; 12338451Smsmith for (i = 0; file_system[i] != NULL; i++) { 12438451Smsmith 12538451Smsmith error = ((*file_system[i]).fo_open)(file, f); 12638451Smsmith if (error == 0) { 12738451Smsmith 12838451Smsmith f->f_ops = file_system[i]; 12965470Smsmith o_rainit(f); 13038451Smsmith return (fd); 13138451Smsmith } 13238451Smsmith if (error != EINVAL) 13338451Smsmith besterror = error; 13438451Smsmith } 13538451Smsmith error = besterror; 13638451Smsmith 13738451Smsmith if ((f->f_flags & F_NODEV) == 0) 13838451Smsmith f->f_dev->dv_close(f); 13938451Smsmith if (error) 14038451Smsmith devclose(f); 14138451Smsmith 14238451Smsmith err: 14338451Smsmith f->f_flags = 0; 14438451Smsmith errno = error; 14538451Smsmith return (-1); 14638451Smsmith} 147