xref: /csrg-svn/sys/stand/open.c (revision 63370)
163276Smckusick /*-
2*63370Sbostic  * Copyright (c) 1993
3*63370Sbostic  *	The Regents of the University of California.  All rights reserved.
463276Smckusick  *
563276Smckusick  * This code is derived from software contributed to Berkeley by
663276Smckusick  * The Mach Operating System project at Carnegie-Mellon University.
763276Smckusick  *
863276Smckusick  * %sccs.include.redist.c%
963276Smckusick  *
10*63370Sbostic  *	@(#)open.c	8.1 (Berkeley) 06/11/93
1163276Smckusick  *
1263276Smckusick  *
1363276Smckusick  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
1463276Smckusick  * All Rights Reserved.
1563276Smckusick  *
1663276Smckusick  * Author: Alessandro Forin
1763276Smckusick  *
1863276Smckusick  * Permission to use, copy, modify and distribute this software and its
1963276Smckusick  * documentation is hereby granted, provided that both the copyright
2063276Smckusick  * notice and this permission notice appear in all copies of the
2163276Smckusick  * software, derivative works or modified versions, and any portions
2263276Smckusick  * thereof, and that both notices appear in supporting documentation.
2363276Smckusick  *
2463276Smckusick  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
2563276Smckusick  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
2663276Smckusick  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
2763276Smckusick  *
2863276Smckusick  * Carnegie Mellon requests users of this software to return to
2963276Smckusick  *
3063276Smckusick  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
3163276Smckusick  *  School of Computer Science
3263276Smckusick  *  Carnegie Mellon University
3363276Smckusick  *  Pittsburgh PA 15213-3890
3463276Smckusick  *
3563276Smckusick  * any improvements or extensions that they make and grant Carnegie the
3663276Smckusick  * rights to redistribute these changes.
3763276Smckusick  */
3863276Smckusick 
3963276Smckusick #include <stand/stand.h>
4063276Smckusick #include <stand/ufs.h>
4163276Smckusick 
4263276Smckusick /*
4363276Smckusick  *	File primitives proper
4463276Smckusick  */
4563276Smckusick 
4663276Smckusick struct fs_ops file_system[] = {
4763276Smckusick 	{ ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat }
4863276Smckusick };
4963276Smckusick #define	NFSYS	(sizeof(file_system) / sizeof(struct fs_ops))
5063276Smckusick 
5163276Smckusick struct open_file files[SOPEN_MAX];
5263276Smckusick 
open(fname,mode)5363276Smckusick open(fname, mode)
5463276Smckusick 	char *fname;
5563276Smckusick 	int mode;
5663276Smckusick {
5763276Smckusick 	register struct open_file *f;
5863276Smckusick 	register int fd, i, error;
5963276Smckusick 	char *file;
6063276Smckusick 
6163276Smckusick 	/* find a free file descriptor */
6263276Smckusick 	for (fd = 0, f = files; fd < SOPEN_MAX; fd++, f++)
6363276Smckusick 		if (f->f_flags == 0)
6463276Smckusick 			goto fnd;
6563276Smckusick 	return (-1);
6663276Smckusick fnd:
6763276Smckusick 	/*
6863276Smckusick 	 * Try to open the device.
6963276Smckusick 	 * Convert open mode (0,1,2) to F_READ, F_WRITE.
7063276Smckusick 	 */
7163276Smckusick 	f->f_flags = mode + 1;
7263276Smckusick 	f->f_dev = (struct devsw *)0;
7363276Smckusick 	file = (char *)0;
7463276Smckusick 	error = devopen(f, fname, &file);
7563276Smckusick 	if (error || f->f_dev == (struct devsw *)0)
7663276Smckusick 		goto err;
7763276Smckusick 
7863276Smckusick 	/* see if we opened a raw device; otherwise, 'file' is the file name. */
7963276Smckusick 	if (file == (char *)0) {
8063276Smckusick 		f->f_flags |= F_RAW;
8163276Smckusick 		return (0);
8263276Smckusick 	}
8363276Smckusick 
8463276Smckusick 	/* pass file name to the different filesystem open routines */
8563276Smckusick 	for (i = 0; i < NFSYS; i++) {
8663276Smckusick 		/* convert mode (0,1,2) to FREAD, FWRITE. */
8763276Smckusick 		error = (file_system[i].open)(file, f);
8863276Smckusick 		if (error == 0) {
8963276Smckusick 			f->f_ops = &file_system[i];
9063276Smckusick 			return (fd);
9163276Smckusick 		}
9263276Smckusick 	}
9363276Smckusick 	if (!error)
9463276Smckusick 		error = ENOENT;
9563276Smckusick 
9663276Smckusick err:
9763276Smckusick 	errno = error;
9863276Smckusick 	return (-1);
9963276Smckusick }
100