• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "stdio_impl.h"
2 #include <stdlib.h>
3 #include <errno.h>
4 
dummy(FILE * f)5 static void dummy(FILE *f) { }
6 weak_alias(dummy, __unlist_locked_file);
7 
fclose(FILE * f)8 int fclose(FILE *f)
9 {
10 	int r;
11 
12 	FLOCK(f);
13 	if (!f || f->fd < 0) {
14 		errno = EBADF;
15 		FUNLOCK(f);
16 		return -EBADF;
17 	}
18 
19 	r = fflush(f);
20 	r |= f->close(f);
21 	FUNLOCK(f);
22 
23 	/* Past this point, f is closed and any further explict access
24 	 * to it is undefined. However, it still exists as an entry in
25 	 * the open file list and possibly in the thread's locked files
26 	 * list, if it was closed while explicitly locked. Functions
27 	 * which process these lists must tolerate dead FILE objects
28 	 * (which necessarily have inactive buffer pointers) without
29 	 * producing any side effects. */
30 
31 	if (f->flags & F_PERM) return r;
32 
33 	__unlist_locked_file(f);
34 
35 	free(f->getln_buf);
36 	/* release base instead of buf which may be modified by setvbuf
37 	 * or iniitalize by local variable */
38 	free(f->base);
39 
40 	/* set file to invalid descriptor */
41 	f->fd = -EBADF;
42 
43 	__ofl_free(f);
44 
45 	return r;
46 }
47