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