1199767f8SToomas Soome /* $NetBSD: close.c,v 1.7 1997/01/22 00:38:09 cgd Exp $ */
2199767f8SToomas Soome
3692bf522SToomas Soome /*
4199767f8SToomas Soome * Copyright (c) 1993
5199767f8SToomas Soome * The Regents of the University of California. All rights reserved.
6199767f8SToomas Soome *
7199767f8SToomas Soome * This code is derived from software contributed to Berkeley by
8199767f8SToomas Soome * The Mach Operating System project at Carnegie-Mellon University.
9199767f8SToomas Soome *
10199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
11199767f8SToomas Soome * modification, are permitted provided that the following conditions
12199767f8SToomas Soome * are met:
13199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
14199767f8SToomas Soome * notice, this list of conditions and the following disclaimer.
15199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
16199767f8SToomas Soome * notice, this list of conditions and the following disclaimer in the
17199767f8SToomas Soome * documentation and/or other materials provided with the distribution.
18692bf522SToomas Soome * 3. Neither the name of the University nor the names of its contributors
19199767f8SToomas Soome * may be used to endorse or promote products derived from this software
20199767f8SToomas Soome * without specific prior written permission.
21199767f8SToomas Soome *
22199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23199767f8SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24199767f8SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25199767f8SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26199767f8SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27199767f8SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28199767f8SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29199767f8SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30199767f8SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32199767f8SToomas Soome * SUCH DAMAGE.
33199767f8SToomas Soome *
34199767f8SToomas Soome * @(#)close.c 8.1 (Berkeley) 6/11/93
35692bf522SToomas Soome *
36199767f8SToomas Soome *
37199767f8SToomas Soome * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
38199767f8SToomas Soome * All Rights Reserved.
39199767f8SToomas Soome *
40199767f8SToomas Soome * Author: Alessandro Forin
41692bf522SToomas Soome *
42199767f8SToomas Soome * Permission to use, copy, modify and distribute this software and its
43199767f8SToomas Soome * documentation is hereby granted, provided that both the copyright
44199767f8SToomas Soome * notice and this permission notice appear in all copies of the
45199767f8SToomas Soome * software, derivative works or modified versions, and any portions
46199767f8SToomas Soome * thereof, and that both notices appear in supporting documentation.
47692bf522SToomas Soome *
48199767f8SToomas Soome * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49199767f8SToomas Soome * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50199767f8SToomas Soome * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51692bf522SToomas Soome *
52199767f8SToomas Soome * Carnegie Mellon requests users of this software to return to
53692bf522SToomas Soome *
54199767f8SToomas Soome * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55199767f8SToomas Soome * School of Computer Science
56199767f8SToomas Soome * Carnegie Mellon University
57199767f8SToomas Soome * Pittsburgh PA 15213-3890
58692bf522SToomas Soome *
59199767f8SToomas Soome * any improvements or extensions that they make and grant Carnegie the
60199767f8SToomas Soome * rights to redistribute these changes.
61199767f8SToomas Soome */
62199767f8SToomas Soome
63199767f8SToomas Soome #include <sys/cdefs.h>
64199767f8SToomas Soome
65199767f8SToomas Soome #include "stand.h"
66199767f8SToomas Soome
67199767f8SToomas Soome int
close(int fd)68199767f8SToomas Soome close(int fd)
69199767f8SToomas Soome {
70a2027c5dSToomas Soome struct open_file *f, *last;
71692bf522SToomas Soome int err1 = 0, err2 = 0;
72199767f8SToomas Soome
73a2027c5dSToomas Soome f = fd2open_file(fd);
74a2027c5dSToomas Soome if (f == NULL) {
75692bf522SToomas Soome errno = EBADF;
76692bf522SToomas Soome return (-1);
77692bf522SToomas Soome }
78199767f8SToomas Soome free(f->f_rabuf);
79199767f8SToomas Soome f->f_rabuf = NULL;
80692bf522SToomas Soome
81a2027c5dSToomas Soome if (f->f_flags != 0) {
82a2027c5dSToomas Soome if (!(f->f_flags & F_RAW) && f->f_ops)
83a2027c5dSToomas Soome err1 = (f->f_ops->fo_close)(f);
84a2027c5dSToomas Soome if (!(f->f_flags & F_NODEV) && f->f_dev)
85a2027c5dSToomas Soome err2 = (f->f_dev->dv_close)(f);
86a2027c5dSToomas Soome if (f->f_devdata != NULL)
87a2027c5dSToomas Soome devclose(f);
88a2027c5dSToomas Soome f->f_flags = 0;
89a2027c5dSToomas Soome } else {
90a2027c5dSToomas Soome /* Attempt to close already closed file. */
91a2027c5dSToomas Soome err1 = EBADF;
92a2027c5dSToomas Soome }
93a2027c5dSToomas Soome
94a2027c5dSToomas Soome /* free unused entries from tail. */
95a2027c5dSToomas Soome TAILQ_FOREACH_REVERSE_SAFE(last, &files, file_list, f_link, f) {
96a2027c5dSToomas Soome if (last->f_flags != 0)
97a2027c5dSToomas Soome break;
98a2027c5dSToomas Soome TAILQ_REMOVE(&files, last, f_link);
99a2027c5dSToomas Soome free(last);
100a2027c5dSToomas Soome }
101a2027c5dSToomas Soome
102692bf522SToomas Soome if (err1) {
103692bf522SToomas Soome errno = err1;
104692bf522SToomas Soome return (-1);
105692bf522SToomas Soome }
106692bf522SToomas Soome if (err2) {
107692bf522SToomas Soome errno = err2;
108692bf522SToomas Soome return (-1);
109692bf522SToomas Soome }
110692bf522SToomas Soome return (0);
111199767f8SToomas Soome }
112