1*14bf419fSkrw /* $OpenBSD: open.c,v 1.2 2016/03/14 23:08:05 krw Exp $ */
28641b11fStom /* $NetBSD: open.c,v 1.12 1996/09/30 16:01:21 ws Exp $ */
38641b11fStom
48641b11fStom /*-
58641b11fStom * Copyright (c) 1993
68641b11fStom * The Regents of the University of California. All rights reserved.
78641b11fStom *
88641b11fStom * This code is derived from software contributed to Berkeley by
98641b11fStom * The Mach Operating System project at Carnegie-Mellon University.
108641b11fStom *
118641b11fStom * Redistribution and use in source and binary forms, with or without
128641b11fStom * modification, are permitted provided that the following conditions
138641b11fStom * are met:
148641b11fStom * 1. Redistributions of source code must retain the above copyright
158641b11fStom * notice, this list of conditions and the following disclaimer.
168641b11fStom * 2. Redistributions in binary form must reproduce the above copyright
178641b11fStom * notice, this list of conditions and the following disclaimer in the
188641b11fStom * documentation and/or other materials provided with the distribution.
198641b11fStom * 3. Neither the name of the University nor the names of its contributors
208641b11fStom * may be used to endorse or promote products derived from this software
218641b11fStom * without specific prior written permission.
228641b11fStom *
238641b11fStom * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
248641b11fStom * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
258641b11fStom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
268641b11fStom * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
278641b11fStom * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
288641b11fStom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
298641b11fStom * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
308641b11fStom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
318641b11fStom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
328641b11fStom * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
338641b11fStom * SUCH DAMAGE.
348641b11fStom *
358641b11fStom * @(#)open.c 8.1 (Berkeley) 6/11/93
368641b11fStom *
378641b11fStom *
388641b11fStom * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
398641b11fStom * All Rights Reserved.
408641b11fStom *
418641b11fStom * Author: Alessandro Forin
428641b11fStom *
438641b11fStom * Permission to use, copy, modify and distribute this software and its
448641b11fStom * documentation is hereby granted, provided that both the copyright
458641b11fStom * notice and this permission notice appear in all copies of the
468641b11fStom * software, derivative works or modified versions, and any portions
478641b11fStom * thereof, and that both notices appear in supporting documentation.
488641b11fStom *
498641b11fStom * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
508641b11fStom * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
518641b11fStom * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
528641b11fStom *
538641b11fStom * Carnegie Mellon requests users of this software to return to
548641b11fStom *
558641b11fStom * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
568641b11fStom * School of Computer Science
578641b11fStom * Carnegie Mellon University
588641b11fStom * Pittsburgh PA 15213-3890
598641b11fStom *
608641b11fStom * any improvements or extensions that they make and grant Carnegie the
618641b11fStom * rights to redistribute these changes.
628641b11fStom */
638641b11fStom
648641b11fStom #include <lib/libsa/stand.h>
658641b11fStom
668641b11fStom struct open_file files[SOPEN_MAX];
678641b11fStom
688641b11fStom /*
698641b11fStom * File primitives proper
708641b11fStom */
718641b11fStom
728641b11fStom int
738641b11fStom #ifndef __INTERNAL_LIBSA_CREAD
open(const char * fname,int mode)748641b11fStom open(const char *fname, int mode)
758641b11fStom #else
768641b11fStom oopen(const char *fname, int mode)
778641b11fStom #endif
788641b11fStom {
798641b11fStom struct open_file *f;
808641b11fStom int fd, i, error;
818641b11fStom char *file;
828641b11fStom
838641b11fStom /* find a free file descriptor */
848641b11fStom for (fd = 0, f = files; fd < SOPEN_MAX; fd++, f++)
858641b11fStom if (f->f_flags == 0)
868641b11fStom goto fnd;
878641b11fStom errno = EMFILE;
888641b11fStom return -1;
898641b11fStom fnd:
908641b11fStom /*
918641b11fStom * Try to open the device.
928641b11fStom * Convert open mode (0,1,2) to F_READ, F_WRITE.
938641b11fStom */
948641b11fStom f->f_flags = mode + 1;
95*14bf419fSkrw f->f_dev = NULL;
96*14bf419fSkrw f->f_ops = NULL;
97*14bf419fSkrw file = NULL;
988641b11fStom error = devopen(f, fname, &file);
998641b11fStom if (error ||
100*14bf419fSkrw (((f->f_flags & F_NODEV) == 0) && f->f_dev == NULL))
1018641b11fStom goto err;
1028641b11fStom
1038641b11fStom /* see if we opened a raw device; otherwise, 'file' is the file name. */
104*14bf419fSkrw if (file == NULL || *file == '\0') {
1058641b11fStom f->f_flags |= F_RAW;
1068641b11fStom return fd;
1078641b11fStom }
1088641b11fStom
1098641b11fStom /* Allow f->f_ops to be set by devopen routine. */
1108641b11fStom if (f->f_ops != NULL) {
1118641b11fStom error = f->f_ops->open(file, f);
1128641b11fStom if (error == 0)
1138641b11fStom return fd;
1148641b11fStom }
1158641b11fStom else {
1168641b11fStom /* pass file name to the different filesystem open routines */
1178641b11fStom for (i = 0; i < nfsys; i++) {
1188641b11fStom /* convert mode (0,1,2) to FREAD, FWRITE. */
1198641b11fStom error = (file_system[i].open)(file, f);
1208641b11fStom if (error == 0) {
1218641b11fStom f->f_ops = &file_system[i];
1228641b11fStom return (fd);
1238641b11fStom }
1248641b11fStom if (error == ENOENT || error == ENOTDIR)
1258641b11fStom break;
1268641b11fStom }
1278641b11fStom }
1288641b11fStom if (error == 0)
1298641b11fStom error = ENOENT;
1308641b11fStom
1318641b11fStom f->f_dev->dv_close(f);
1328641b11fStom err:
1338641b11fStom f->f_flags = 0;
1348641b11fStom errno = error;
1358641b11fStom return -1;
1368641b11fStom }
137