summaryrefslogtreecommitdiff
path: root/libc/stdio/fcloseall.c
blob: 4d78b37d64019b5691bc7763c37f632c8e49ceda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
 *
 * GNU Library General Public License (LGPL) version 2 or later.
 *
 * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
 */

#include <features.h>

#ifdef __USE_GNU
#include "_stdio.h"


/* NOTE: GLIBC difference!!! -- fcloseall
 * According to the info pages, glibc actually fclose()s all open files.
 * Apparently, glibc's new version only fflush()s and unbuffers all
 * writing streams to cope with unordered destruction of c++ static
 * objects.
 */

int fcloseall (void)
{
#ifdef __STDIO_HAS_OPENLIST

	int retval = 0;
	FILE *f;

	__STDIO_OPENLIST_INC_USE;

#ifdef __UCLIBC_MJN3_ONLY__
#warning REMINDER: should probably have a get_head() operation
#endif
	__STDIO_THREADLOCK_OPENLIST_ADD;
	f = _stdio_openlist;
	__STDIO_THREADUNLOCK_OPENLIST_ADD;

	while (f) {
#ifdef __UCLIBC_MJN3_ONLY__
#warning REMINDER: should probably have a get_next() operation
#endif
		FILE *n = f->__nextopen;
		__STDIO_AUTO_THREADLOCK_VAR;

		__STDIO_AUTO_THREADLOCK(f);
		/* Only call fclose on the stream if it is not already closed. */
		if ((f->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
		    != (__FLAG_READONLY|__FLAG_WRITEONLY)
		    ) {
			if (fclose(f)) {
				retval = EOF;
			}
		}
		__STDIO_AUTO_THREADUNLOCK(f);

		f = n;
	}

	__STDIO_OPENLIST_DEC_USE;

	return retval;

#else

#warning Always fails in this configuration because no open file list.

	return EOF;

#endif
}
#endif