xref: /illumos-gate/usr/src/boot/libsa/close.c (revision 22028508)
1*22028508SToomas Soome /*	$NetBSD: close.c,v 1.7 1997/01/22 00:38:09 cgd Exp $	*/
2*22028508SToomas Soome 
3*22028508SToomas Soome /*
4*22028508SToomas Soome  * Copyright (c) 1993
5*22028508SToomas Soome  *	The Regents of the University of California.  All rights reserved.
6*22028508SToomas Soome  *
7*22028508SToomas Soome  * This code is derived from software contributed to Berkeley by
8*22028508SToomas Soome  * The Mach Operating System project at Carnegie-Mellon University.
9*22028508SToomas Soome  *
10*22028508SToomas Soome  * Redistribution and use in source and binary forms, with or without
11*22028508SToomas Soome  * modification, are permitted provided that the following conditions
12*22028508SToomas Soome  * are met:
13*22028508SToomas Soome  * 1. Redistributions of source code must retain the above copyright
14*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer.
15*22028508SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17*22028508SToomas Soome  *    documentation and/or other materials provided with the distribution.
18*22028508SToomas Soome  * 3. Neither the name of the University nor the names of its contributors
19*22028508SToomas Soome  *    may be used to endorse or promote products derived from this software
20*22028508SToomas Soome  *    without specific prior written permission.
21*22028508SToomas Soome  *
22*22028508SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*22028508SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*22028508SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*22028508SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*22028508SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*22028508SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*22028508SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*22028508SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*22028508SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*22028508SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*22028508SToomas Soome  * SUCH DAMAGE.
33*22028508SToomas Soome  *
34*22028508SToomas Soome  *	@(#)close.c	8.1 (Berkeley) 6/11/93
35*22028508SToomas Soome  *
36*22028508SToomas Soome  *
37*22028508SToomas Soome  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
38*22028508SToomas Soome  * All Rights Reserved.
39*22028508SToomas Soome  *
40*22028508SToomas Soome  * Author: Alessandro Forin
41*22028508SToomas Soome  *
42*22028508SToomas Soome  * Permission to use, copy, modify and distribute this software and its
43*22028508SToomas Soome  * documentation is hereby granted, provided that both the copyright
44*22028508SToomas Soome  * notice and this permission notice appear in all copies of the
45*22028508SToomas Soome  * software, derivative works or modified versions, and any portions
46*22028508SToomas Soome  * thereof, and that both notices appear in supporting documentation.
47*22028508SToomas Soome  *
48*22028508SToomas Soome  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49*22028508SToomas Soome  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50*22028508SToomas Soome  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51*22028508SToomas Soome  *
52*22028508SToomas Soome  * Carnegie Mellon requests users of this software to return to
53*22028508SToomas Soome  *
54*22028508SToomas Soome  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55*22028508SToomas Soome  *  School of Computer Science
56*22028508SToomas Soome  *  Carnegie Mellon University
57*22028508SToomas Soome  *  Pittsburgh PA 15213-3890
58*22028508SToomas Soome  *
59*22028508SToomas Soome  * any improvements or extensions that they make and grant Carnegie the
60*22028508SToomas Soome  * rights to redistribute these changes.
61*22028508SToomas Soome  */
62*22028508SToomas Soome 
63*22028508SToomas Soome #include <sys/cdefs.h>
64*22028508SToomas Soome 
65*22028508SToomas Soome #include "stand.h"
66*22028508SToomas Soome 
67*22028508SToomas Soome int
close(int fd)68*22028508SToomas Soome close(int fd)
69*22028508SToomas Soome {
70*22028508SToomas Soome 	struct open_file *f, *last;
71*22028508SToomas Soome 	int err1 = 0, err2 = 0;
72*22028508SToomas Soome 
73*22028508SToomas Soome 	f = fd2open_file(fd);
74*22028508SToomas Soome 	if (f == NULL) {
75*22028508SToomas Soome 		errno = EBADF;
76*22028508SToomas Soome 		return (-1);
77*22028508SToomas Soome 	}
78*22028508SToomas Soome 	free(f->f_rabuf);
79*22028508SToomas Soome 	f->f_rabuf = NULL;
80*22028508SToomas Soome 
81*22028508SToomas Soome 	if (f->f_flags != 0) {
82*22028508SToomas Soome 		if (!(f->f_flags & F_RAW) && f->f_ops)
83*22028508SToomas Soome 			err1 = (f->f_ops->fo_close)(f);
84*22028508SToomas Soome 		if (!(f->f_flags & F_NODEV) && f->f_dev)
85*22028508SToomas Soome 			err2 = (f->f_dev->dv_close)(f);
86*22028508SToomas Soome 		if (f->f_devdata != NULL)
87*22028508SToomas Soome 			devclose(f);
88*22028508SToomas Soome 		f->f_flags = 0;
89*22028508SToomas Soome 	} else {
90*22028508SToomas Soome 		/* Attempt to close already closed file. */
91*22028508SToomas Soome 		err1 = EBADF;
92*22028508SToomas Soome 	}
93*22028508SToomas Soome 
94*22028508SToomas Soome 	/* free unused entries from tail. */
95*22028508SToomas Soome 	TAILQ_FOREACH_REVERSE_SAFE(last, &files, file_list, f_link, f) {
96*22028508SToomas Soome 		if (last->f_flags != 0)
97*22028508SToomas Soome 			break;
98*22028508SToomas Soome 		TAILQ_REMOVE(&files, last, f_link);
99*22028508SToomas Soome 		free(last);
100*22028508SToomas Soome 	}
101*22028508SToomas Soome 
102*22028508SToomas Soome 	if (err1) {
103*22028508SToomas Soome 		errno = err1;
104*22028508SToomas Soome 		return (-1);
105*22028508SToomas Soome 	}
106*22028508SToomas Soome 	if (err2) {
107*22028508SToomas Soome 		errno = err2;
108*22028508SToomas Soome 		return (-1);
109*22028508SToomas Soome 	}
110*22028508SToomas Soome 	return (0);
111*22028508SToomas Soome }
112