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