1239310Sdim/* $OpenBSD: close.c,v 1.8 2003/08/11 06:23:09 deraadt Exp $ */ 2239310Sdim/* $NetBSD: close.c,v 1.5 1995/09/06 19:53:29 pk Exp $ */ 3239310Sdim 4239310Sdim/*- 5239310Sdim * Copyright (c) 1993 6239310Sdim * The Regents of the University of California. All rights reserved. 7239310Sdim * 8239310Sdim * This code is derived from software contributed to Berkeley by 9239310Sdim * The Mach Operating System project at Carnegie-Mellon University. 10239310Sdim * 11239310Sdim * Redistribution and use in source and binary forms, with or without 12239310Sdim * modification, are permitted provided that the following conditions 13239310Sdim * are met: 14239310Sdim * 1. Redistributions of source code must retain the above copyright 15249423Sdim * notice, this list of conditions and the following disclaimer. 16249423Sdim * 2. Redistributions in binary form must reproduce the above copyright 17249423Sdim * notice, this list of conditions and the following disclaimer in the 18239310Sdim * documentation and/or other materials provided with the distribution. 19239310Sdim * 3. Neither the name of the University nor the names of its contributors 20249423Sdim * may be used to endorse or promote products derived from this software 21239310Sdim * without specific prior written permission. 22239310Sdim * 23239310Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24239310Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25239310Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26239310Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27239310Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28249423Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29249423Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30249423Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31249423Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32239310Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33239310Sdim * SUCH DAMAGE. 34249423Sdim * 35239310Sdim * @(#)close.c 8.1 (Berkeley) 6/11/93 36249423Sdim * 37249423Sdim * 38239310Sdim * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University 39249423Sdim * All Rights Reserved. 40249423Sdim * 41249423Sdim * Author: Alessandro Forin 42249423Sdim * 43249423Sdim * Permission to use, copy, modify and distribute this software and its 44239310Sdim * documentation is hereby granted, provided that both the copyright 45251662Sdim * notice and this permission notice appear in all copies of the 46251662Sdim * software, derivative works or modified versions, and any portions 47251662Sdim * thereof, and that both notices appear in supporting documentation. 48251662Sdim * 49251662Sdim * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 50239310Sdim * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 51239310Sdim * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 52239310Sdim * 53239310Sdim * Carnegie Mellon requests users of this software to return to 54239310Sdim * 55239310Sdim * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 56239310Sdim * School of Computer Science 57239310Sdim * Carnegie Mellon University 58239310Sdim * Pittsburgh PA 15213-3890 59249423Sdim * 60249423Sdim * any improvements or extensions that they make and grant Carnegie the 61239310Sdim * rights to redistribute these changes. 62239310Sdim */ 63239310Sdim 64239310Sdim#include "stand.h" 65239310Sdim#include "saerrno.h" 66239310Sdim 67249423Sdimint 68249423Sdim#ifndef __INTERNAL_LIBSA_CREAD 69249423Sdimclose(int fd) 70249423Sdim#else 71249423Sdimoclose(int fd) 72249423Sdim#endif 73239310Sdim{ 74239310Sdim struct open_file *f = &files[fd]; 75239310Sdim int err1 = 0, err2 = 0; 76239310Sdim 77239310Sdim if ((unsigned)fd >= SOPEN_MAX || f->f_flags == 0) { 78239310Sdim errno = EBADF; 79239310Sdim return (-1); 80239310Sdim } 81239310Sdim if (!(f->f_flags & F_RAW) && f->f_ops) 82251662Sdim err1 = (f->f_ops->close)(f); 83251662Sdim if (!(f->f_flags & F_NODEV) && f->f_dev) 84239310Sdim err2 = (f->f_dev->dv_close)(f); 85239310Sdim f->f_flags = 0; 86239310Sdim if (err1) { 87239310Sdim errno = err1; 88239310Sdim return (-1); 89239310Sdim } 90239310Sdim if (err2) { 91239310Sdim errno = err2; 92249423Sdim return (-1); 93239310Sdim } 94255804Sdim return (0); 95255804Sdim} 96249423Sdim