1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
7 *
8 * %sccs.include.redist.c%
9 *
10 * @(#)open.c 8.1 (Berkeley) 06/11/93
11 *
12 *
13 * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
14 * All Rights Reserved.
15 *
16 * Author: Alessandro Forin
17 *
18 * Permission to use, copy, modify and distribute this software and its
19 * documentation is hereby granted, provided that both the copyright
20 * notice and this permission notice appear in all copies of the
21 * software, derivative works or modified versions, and any portions
22 * thereof, and that both notices appear in supporting documentation.
23 *
24 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
25 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
26 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
27 *
28 * Carnegie Mellon requests users of this software to return to
29 *
30 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
31 * School of Computer Science
32 * Carnegie Mellon University
33 * Pittsburgh PA 15213-3890
34 *
35 * any improvements or extensions that they make and grant Carnegie the
36 * rights to redistribute these changes.
37 */
38
39 #include <stand/stand.h>
40 #include <stand/ufs.h>
41
42 /*
43 * File primitives proper
44 */
45
46 struct fs_ops file_system[] = {
47 { ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat }
48 };
49 #define NFSYS (sizeof(file_system) / sizeof(struct fs_ops))
50
51 struct open_file files[SOPEN_MAX];
52
open(fname,mode)53 open(fname, mode)
54 char *fname;
55 int mode;
56 {
57 register struct open_file *f;
58 register int fd, i, error;
59 char *file;
60
61 /* find a free file descriptor */
62 for (fd = 0, f = files; fd < SOPEN_MAX; fd++, f++)
63 if (f->f_flags == 0)
64 goto fnd;
65 return (-1);
66 fnd:
67 /*
68 * Try to open the device.
69 * Convert open mode (0,1,2) to F_READ, F_WRITE.
70 */
71 f->f_flags = mode + 1;
72 f->f_dev = (struct devsw *)0;
73 file = (char *)0;
74 error = devopen(f, fname, &file);
75 if (error || f->f_dev == (struct devsw *)0)
76 goto err;
77
78 /* see if we opened a raw device; otherwise, 'file' is the file name. */
79 if (file == (char *)0) {
80 f->f_flags |= F_RAW;
81 return (0);
82 }
83
84 /* pass file name to the different filesystem open routines */
85 for (i = 0; i < NFSYS; i++) {
86 /* convert mode (0,1,2) to FREAD, FWRITE. */
87 error = (file_system[i].open)(file, f);
88 if (error == 0) {
89 f->f_ops = &file_system[i];
90 return (fd);
91 }
92 }
93 if (!error)
94 error = ENOENT;
95
96 err:
97 errno = error;
98 return (-1);
99 }
100