1*14bf419fSkrw /* $OpenBSD: open.c,v 1.2 2016/03/14 23:08:05 krw Exp $ */
2ef7aef7bStom /* $NetBSD: open.c,v 1.12 1996/09/30 16:01:21 ws Exp $ */
3ef7aef7bStom
4ef7aef7bStom /*-
5ef7aef7bStom * Copyright (c) 1993
6ef7aef7bStom * The Regents of the University of California. All rights reserved.
7ef7aef7bStom *
8ef7aef7bStom * This code is derived from software contributed to Berkeley by
9ef7aef7bStom * The Mach Operating System project at Carnegie-Mellon University.
10ef7aef7bStom *
11ef7aef7bStom * Redistribution and use in source and binary forms, with or without
12ef7aef7bStom * modification, are permitted provided that the following conditions
13ef7aef7bStom * are met:
14ef7aef7bStom * 1. Redistributions of source code must retain the above copyright
15ef7aef7bStom * notice, this list of conditions and the following disclaimer.
16ef7aef7bStom * 2. Redistributions in binary form must reproduce the above copyright
17ef7aef7bStom * notice, this list of conditions and the following disclaimer in the
18ef7aef7bStom * documentation and/or other materials provided with the distribution.
19ef7aef7bStom * 3. Neither the name of the University nor the names of its contributors
20ef7aef7bStom * may be used to endorse or promote products derived from this software
21ef7aef7bStom * without specific prior written permission.
22ef7aef7bStom *
23ef7aef7bStom * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24ef7aef7bStom * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ef7aef7bStom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ef7aef7bStom * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27ef7aef7bStom * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ef7aef7bStom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ef7aef7bStom * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ef7aef7bStom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ef7aef7bStom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ef7aef7bStom * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ef7aef7bStom * SUCH DAMAGE.
34ef7aef7bStom *
35ef7aef7bStom * @(#)open.c 8.1 (Berkeley) 6/11/93
36ef7aef7bStom *
37ef7aef7bStom *
38ef7aef7bStom * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
39ef7aef7bStom * All Rights Reserved.
40ef7aef7bStom *
41ef7aef7bStom * Author: Alessandro Forin
42ef7aef7bStom *
43ef7aef7bStom * Permission to use, copy, modify and distribute this software and its
44ef7aef7bStom * documentation is hereby granted, provided that both the copyright
45ef7aef7bStom * notice and this permission notice appear in all copies of the
46ef7aef7bStom * software, derivative works or modified versions, and any portions
47ef7aef7bStom * thereof, and that both notices appear in supporting documentation.
48ef7aef7bStom *
49ef7aef7bStom * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
50ef7aef7bStom * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
51ef7aef7bStom * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52ef7aef7bStom *
53ef7aef7bStom * Carnegie Mellon requests users of this software to return to
54ef7aef7bStom *
55ef7aef7bStom * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
56ef7aef7bStom * School of Computer Science
57ef7aef7bStom * Carnegie Mellon University
58ef7aef7bStom * Pittsburgh PA 15213-3890
59ef7aef7bStom *
60ef7aef7bStom * any improvements or extensions that they make and grant Carnegie the
61ef7aef7bStom * rights to redistribute these changes.
62ef7aef7bStom */
63ef7aef7bStom
64ef7aef7bStom #include <lib/libsa/stand.h>
65ef7aef7bStom
66ef7aef7bStom struct open_file files[SOPEN_MAX];
67ef7aef7bStom
68ef7aef7bStom /*
69ef7aef7bStom * File primitives proper
70ef7aef7bStom */
71ef7aef7bStom
72ef7aef7bStom int
73ef7aef7bStom #ifndef __INTERNAL_LIBSA_CREAD
open(const char * fname,int mode)74ef7aef7bStom open(const char *fname, int mode)
75ef7aef7bStom #else
76ef7aef7bStom oopen(const char *fname, int mode)
77ef7aef7bStom #endif
78ef7aef7bStom {
79ef7aef7bStom struct open_file *f;
80ef7aef7bStom int fd, i, error;
81ef7aef7bStom char *file;
82ef7aef7bStom
83ef7aef7bStom /* find a free file descriptor */
84ef7aef7bStom for (fd = 0, f = files; fd < SOPEN_MAX; fd++, f++)
85ef7aef7bStom if (f->f_flags == 0)
86ef7aef7bStom goto fnd;
87ef7aef7bStom errno = EMFILE;
88ef7aef7bStom return -1;
89ef7aef7bStom fnd:
90ef7aef7bStom /*
91ef7aef7bStom * Try to open the device.
92ef7aef7bStom * Convert open mode (0,1,2) to F_READ, F_WRITE.
93ef7aef7bStom */
94ef7aef7bStom f->f_flags = mode + 1;
95*14bf419fSkrw f->f_dev = NULL;
96*14bf419fSkrw f->f_ops = NULL;
97*14bf419fSkrw file = NULL;
98ef7aef7bStom error = devopen(f, fname, &file);
99ef7aef7bStom if (error ||
100*14bf419fSkrw (((f->f_flags & F_NODEV) == 0) && f->f_dev == NULL))
101ef7aef7bStom goto err;
102ef7aef7bStom
103ef7aef7bStom /* see if we opened a raw device; otherwise, 'file' is the file name. */
104*14bf419fSkrw if (file == NULL || *file == '\0') {
105ef7aef7bStom f->f_flags |= F_RAW;
106ef7aef7bStom return fd;
107ef7aef7bStom }
108ef7aef7bStom
109ef7aef7bStom /* Allow f->f_ops to be set by devopen routine. */
110ef7aef7bStom if (f->f_ops != NULL) {
111ef7aef7bStom error = f->f_ops->open(file, f);
112ef7aef7bStom if (error == 0)
113ef7aef7bStom return fd;
114ef7aef7bStom }
115ef7aef7bStom else {
116ef7aef7bStom /* pass file name to the different filesystem open routines */
117ef7aef7bStom for (i = 0; i < nfsys; i++) {
118ef7aef7bStom /* convert mode (0,1,2) to FREAD, FWRITE. */
119ef7aef7bStom error = (file_system[i].open)(file, f);
120ef7aef7bStom if (error == 0) {
121ef7aef7bStom f->f_ops = &file_system[i];
122ef7aef7bStom return (fd);
123ef7aef7bStom }
124ef7aef7bStom if (error == ENOENT || error == ENOTDIR)
125ef7aef7bStom break;
126ef7aef7bStom }
127ef7aef7bStom }
128ef7aef7bStom if (error == 0)
129ef7aef7bStom error = ENOENT;
130ef7aef7bStom
131ef7aef7bStom f->f_dev->dv_close(f);
132ef7aef7bStom err:
133ef7aef7bStom f->f_flags = 0;
134ef7aef7bStom errno = error;
135ef7aef7bStom return -1;
136ef7aef7bStom }
137