xref: /netbsd-src/sys/lib/libsa/open.c (revision 1c038e6834cae6806ebb97ae00a41d67e4d72ae1)
1*1c038e68Sisaki /*	$NetBSD: open.c,v 1.26 2007/11/24 13:20:56 isaki Exp $	*/
26668f51cScgd 
336b52a82Sbrezak /*-
436b52a82Sbrezak  * Copyright (c) 1993
536b52a82Sbrezak  *	The Regents of the University of California.  All rights reserved.
636b52a82Sbrezak  *
736b52a82Sbrezak  * This code is derived from software contributed to Berkeley by
836b52a82Sbrezak  * The Mach Operating System project at Carnegie-Mellon University.
936b52a82Sbrezak  *
1036b52a82Sbrezak  * Redistribution and use in source and binary forms, with or without
1136b52a82Sbrezak  * modification, are permitted provided that the following conditions
1236b52a82Sbrezak  * are met:
1336b52a82Sbrezak  * 1. Redistributions of source code must retain the above copyright
1436b52a82Sbrezak  *    notice, this list of conditions and the following disclaimer.
1536b52a82Sbrezak  * 2. Redistributions in binary form must reproduce the above copyright
1636b52a82Sbrezak  *    notice, this list of conditions and the following disclaimer in the
1736b52a82Sbrezak  *    documentation and/or other materials provided with the distribution.
18aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
1936b52a82Sbrezak  *    may be used to endorse or promote products derived from this software
2036b52a82Sbrezak  *    without specific prior written permission.
2136b52a82Sbrezak  *
2236b52a82Sbrezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2336b52a82Sbrezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2436b52a82Sbrezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2536b52a82Sbrezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2636b52a82Sbrezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2736b52a82Sbrezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2836b52a82Sbrezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2936b52a82Sbrezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3036b52a82Sbrezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3136b52a82Sbrezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3236b52a82Sbrezak  * SUCH DAMAGE.
3336b52a82Sbrezak  *
346668f51cScgd  *	@(#)open.c	8.1 (Berkeley) 6/11/93
3536b52a82Sbrezak  *
3636b52a82Sbrezak  *
3736b52a82Sbrezak  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
3836b52a82Sbrezak  * All Rights Reserved.
3936b52a82Sbrezak  *
4036b52a82Sbrezak  * Author: Alessandro Forin
4136b52a82Sbrezak  *
4236b52a82Sbrezak  * Permission to use, copy, modify and distribute this software and its
4336b52a82Sbrezak  * documentation is hereby granted, provided that both the copyright
4436b52a82Sbrezak  * notice and this permission notice appear in all copies of the
4536b52a82Sbrezak  * software, derivative works or modified versions, and any portions
4636b52a82Sbrezak  * thereof, and that both notices appear in supporting documentation.
4736b52a82Sbrezak  *
4836b52a82Sbrezak  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
4936b52a82Sbrezak  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
5036b52a82Sbrezak  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
5136b52a82Sbrezak  *
5236b52a82Sbrezak  * Carnegie Mellon requests users of this software to return to
5336b52a82Sbrezak  *
5436b52a82Sbrezak  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
5536b52a82Sbrezak  *  School of Computer Science
5636b52a82Sbrezak  *  Carnegie Mellon University
5736b52a82Sbrezak  *  Pittsburgh PA 15213-3890
5836b52a82Sbrezak  *
5936b52a82Sbrezak  * any improvements or extensions that they make and grant Carnegie the
6036b52a82Sbrezak  * rights to redistribute these changes.
6136b52a82Sbrezak  */
6236b52a82Sbrezak 
6336b52a82Sbrezak #include "stand.h"
64b7b97847Sleo 
6536b52a82Sbrezak /*
6636b52a82Sbrezak  *	File primitives proper
6736b52a82Sbrezak  */
6836b52a82Sbrezak 
6905330db7Sross #ifdef HELLO_CTAGS
oopen()7005330db7Sross oopen(){}
7105330db7Sross #endif
7205330db7Sross 
73e03842d0Sbrezak int
7421979e8dScgd #ifndef __INTERNAL_LIBSA_CREAD
open(const char * fname,int mode)75*1c038e68Sisaki open(const char *fname, int mode)
7621979e8dScgd #else
77*1c038e68Sisaki oopen(const char *fname, int mode)
7821979e8dScgd #endif
7936b52a82Sbrezak {
801279e67bSaugustss 	struct open_file *f;
811279e67bSaugustss 	int fd, error;
8230921347Scgd #if !defined(LIBSA_SINGLE_FILESYSTEM)
831279e67bSaugustss 	int i, besterror;
8430921347Scgd #endif
8536b52a82Sbrezak 	char *file;
8636b52a82Sbrezak 
8736b52a82Sbrezak 	/* find a free file descriptor */
8836b52a82Sbrezak 	for (fd = 0, f = files; fd < SOPEN_MAX; fd++, f++)
8936b52a82Sbrezak 		if (f->f_flags == 0)
9036b52a82Sbrezak 			goto fnd;
91381f4cb1Spk 	errno = EMFILE;
92*1c038e68Sisaki 	return -1;
9336b52a82Sbrezak fnd:
9436b52a82Sbrezak 	/*
9536b52a82Sbrezak 	 * Try to open the device.
9636b52a82Sbrezak 	 * Convert open mode (0,1,2) to F_READ, F_WRITE.
9736b52a82Sbrezak 	 */
9836b52a82Sbrezak 	f->f_flags = mode + 1;
9930921347Scgd #if !defined(LIBSA_SINGLE_DEVICE)
10036b52a82Sbrezak 	f->f_dev = (struct devsw *)0;
10130921347Scgd #endif
10230921347Scgd #if !defined(LIBSA_SINGLE_FILESYSTEM)
10307f70f97Spk 	f->f_ops = (struct fs_ops *)0;
10430921347Scgd #endif
10530921347Scgd #if !defined(LIBSA_NO_RAW_ACCESS)
10601bd8a8cSpk 	f->f_offset = 0;
10730921347Scgd #endif
10836b52a82Sbrezak 	file = (char *)0;
10936b52a82Sbrezak 	error = devopen(f, fname, &file);
11030921347Scgd 	if (error
11130921347Scgd #if !defined(LIBSA_SINGLE_DEVICE)
11230921347Scgd 	    || (((f->f_flags & F_NODEV) == 0) &&
11330921347Scgd 		f->f_dev == (struct devsw *)0)
11430921347Scgd #endif
11530921347Scgd 	    )
11636b52a82Sbrezak 		goto err;
11736b52a82Sbrezak 
11830921347Scgd #if !defined(LIBSA_NO_RAW_ACCESS)
11936b52a82Sbrezak 	/* see if we opened a raw device; otherwise, 'file' is the file name. */
120c9a13fa0Sthorpej 	if (file == (char *)0 || *file == '\0') {
12136b52a82Sbrezak 		f->f_flags |= F_RAW;
122*1c038e68Sisaki 		return fd;
12336b52a82Sbrezak 	}
12430921347Scgd #endif
12536b52a82Sbrezak 
12636b52a82Sbrezak 	/* pass file name to the different filesystem open routines */
12730921347Scgd #if !defined(LIBSA_SINGLE_FILESYSTEM)
128981d4969Scgd 	besterror = ENOENT;
129e03842d0Sbrezak 	for (i = 0; i < nfsys; i++) {
13030921347Scgd 		error = FS_OPEN(&file_system[i])(file, f);
13136b52a82Sbrezak 		if (error == 0) {
13236b52a82Sbrezak 			f->f_ops = &file_system[i];
133*1c038e68Sisaki 			return fd;
13436b52a82Sbrezak 		}
135981d4969Scgd 		if (error != EINVAL)
136981d4969Scgd 			besterror = error;
13736b52a82Sbrezak 	}
138981d4969Scgd 	error = besterror;
13930921347Scgd #else
14030921347Scgd 	error = FS_OPEN(&file_system[i])(file, f);
14130921347Scgd 	if (error == 0)
142*1c038e68Sisaki 		return fd;
143912fab6eSdsl 	if (error == EINVAL)
14430921347Scgd 		error = ENOENT;
14530921347Scgd #endif
14636b52a82Sbrezak 
147912fab6eSdsl 	if ((f->f_flags & F_NODEV) == 0) {
14830921347Scgd #if !defined(LIBSA_SINGLE_DEVICE)
14930921347Scgd 		if (DEV_CLOSE(f->f_dev) != NULL)
15030921347Scgd #endif
15130921347Scgd 			(void)DEV_CLOSE(f->f_dev)(f);
152912fab6eSdsl 	}
15336b52a82Sbrezak err:
1548e2c9805Spk 	f->f_flags = 0;
15536b52a82Sbrezak 	errno = error;
156*1c038e68Sisaki 	return -1;
15736b52a82Sbrezak }
158