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