1238106Sdes/*	$NetBSD: open.c,v 1.16 1997/01/28 09:41:03 pk Exp $	*/
2238106Sdes
3238106Sdes/*-
4238106Sdes * Copyright (c) 1993
5238106Sdes *	The Regents of the University of California.  All rights reserved.
6238106Sdes *
7238106Sdes * This code is derived from software contributed to Berkeley by
8238106Sdes * The Mach Operating System project at Carnegie-Mellon University.
9238106Sdes *
10238106Sdes * Redistribution and use in source and binary forms, with or without
11238106Sdes * modification, are permitted provided that the following conditions
12238106Sdes * are met:
13238106Sdes * 1. Redistributions of source code must retain the above copyright
14238106Sdes *    notice, this list of conditions and the following disclaimer.
15238106Sdes * 2. Redistributions in binary form must reproduce the above copyright
16238106Sdes *    notice, this list of conditions and the following disclaimer in the
17238106Sdes *    documentation and/or other materials provided with the distribution.
18238106Sdes * 4. Neither the name of the University nor the names of its contributors
19238106Sdes *    may be used to endorse or promote products derived from this software
20238106Sdes *    without specific prior written permission.
21238106Sdes *
22238106Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23238106Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24238106Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25238106Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26238106Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27238106Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28238106Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29238106Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30238106Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31238106Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32238106Sdes * SUCH DAMAGE.
33238106Sdes *
34238106Sdes *	@(#)open.c	8.1 (Berkeley) 6/11/93
35238106Sdes *
36238106Sdes *
37238106Sdes * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
38238106Sdes * All Rights Reserved.
39238106Sdes *
40238106Sdes * Author: Alessandro Forin
41238106Sdes *
42238106Sdes * Permission to use, copy, modify and distribute this software and its
43238106Sdes * documentation is hereby granted, provided that both the copyright
44238106Sdes * notice and this permission notice appear in all copies of the
45238106Sdes * software, derivative works or modified versions, and any portions
46238106Sdes * thereof, and that both notices appear in supporting documentation.
47238106Sdes *
48238106Sdes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49238106Sdes * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50238106Sdes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51238106Sdes *
52238106Sdes * Carnegie Mellon requests users of this software to return to
53238106Sdes *
54238106Sdes *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55238106Sdes *  School of Computer Science
56238106Sdes *  Carnegie Mellon University
57238106Sdes *  Pittsburgh PA 15213-3890
58238106Sdes *
59238106Sdes * any improvements or extensions that they make and grant Carnegie the
60238106Sdes * rights to redistribute these changes.
61238106Sdes */
62238106Sdes
63238106Sdes#include <sys/cdefs.h>
64238106Sdes__FBSDID("$FreeBSD: releng/10.2/lib/libstand/open.c 198542 2009-10-28 14:13:45Z brueffer $");
65238106Sdes
66238106Sdes#include "stand.h"
67238106Sdes
68238106Sdesstruct open_file files[SOPEN_MAX];
69238106Sdes
70238106Sdesstatic int
71238106Sdeso_gethandle(void)
72238106Sdes{
73238106Sdes    int		fd;
74238106Sdes
75238106Sdes    for (fd = 0; fd < SOPEN_MAX; fd++)
76238106Sdes	if (files[fd].f_flags == 0)
77238106Sdes	    return(fd);
78238106Sdes    return(-1);
79238106Sdes}
80238106Sdes
81238106Sdesstatic void
82238106Sdeso_rainit(struct open_file *f)
83238106Sdes{
84238106Sdes    f->f_rabuf = malloc(SOPEN_RASIZE);
85238106Sdes    f->f_ralen = 0;
86238106Sdes    f->f_raoffset = 0;
87238106Sdes}
88238106Sdes
89238106Sdesint
90238106Sdesopen(const char *fname, int mode)
91238106Sdes{
92238106Sdes    struct open_file	*f;
93238106Sdes    int			fd, i, error, besterror;
94238106Sdes    const char		*file;
95238106Sdes
96238106Sdes    if ((fd = o_gethandle()) == -1) {
97238106Sdes	errno = EMFILE;
98238106Sdes	return(-1);
99238106Sdes    }
100238106Sdes
101238106Sdes    f = &files[fd];
102238106Sdes    f->f_flags = mode + 1;
103238106Sdes    f->f_dev = (struct devsw *)0;
104238106Sdes    f->f_ops = (struct fs_ops *)0;
105238106Sdes    f->f_offset = 0;
106238106Sdes    f->f_devdata = NULL;
107238106Sdes    file = (char *)0;
108238106Sdes    error = devopen(f, fname, &file);
109238106Sdes    if (error ||
110238106Sdes	(((f->f_flags & F_NODEV) == 0) && f->f_dev == (struct devsw *)0))
111238106Sdes	goto err;
112238106Sdes
113238106Sdes    /* see if we opened a raw device; otherwise, 'file' is the file name. */
114238106Sdes    if (file == (char *)0 || *file == '\0') {
115238106Sdes	f->f_flags |= F_RAW;
116238106Sdes	f->f_rabuf = NULL;
117238106Sdes	return (fd);
118238106Sdes    }
119238106Sdes
120238106Sdes    /* pass file name to the different filesystem open routines */
121238106Sdes    besterror = ENOENT;
122238106Sdes    for (i = 0; file_system[i] != NULL; i++) {
123238106Sdes
124238106Sdes	error = ((*file_system[i]).fo_open)(file, f);
125238106Sdes	if (error == 0) {
126238106Sdes
127238106Sdes	    f->f_ops = file_system[i];
128238106Sdes	    o_rainit(f);
129238106Sdes	    return (fd);
130238106Sdes	}
131238106Sdes	if (error != EINVAL)
132238106Sdes	    besterror = error;
133238106Sdes    }
134238106Sdes    error = besterror;
135238106Sdes
136238106Sdes    if ((f->f_flags & F_NODEV) == 0)
137238106Sdes	f->f_dev->dv_close(f);
138238106Sdes    if (error)
139238106Sdes	devclose(f);
140238106Sdes
141238106Sdes err:
142238106Sdes    f->f_flags = 0;
143238106Sdes    errno = error;
144238106Sdes    return (-1);
145238106Sdes}
146238106Sdes