blob: 7fc96f287c27a0c0110a2f9bea0c45e51f5947b3 (
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
71
72
|
#include "__stdio.h" // for __FILE
#include "stddef.h" // for NULL
#include <libc.h> // for weak_reference
#include <stdio.h> // for fflush, stdout, FILE, stderr, stdin
#include <stdlib.h> // for free, exit
#include <unistd.h> // for _exit, close, write, STDERR_FILENO
void (*__dummy_atexit_fvec)(void);
weak_reference(__dummy_atexit_fvec, __atexit_fvec);
void (*__dummy_stdio_cleanup)(void);
weak_reference(__dummy_stdio_cleanup, __stdio_cleanup);
static void __fclose(FILE *stream)
{
if (stream == NULL) {
return;
}
if (__FILE(stream)->buf_len > 0) {
fflush(stream);
}
if (__FILE(stream)->fd > STDERR_FILENO) {
close(__FILE(stream)->fd);
}
if (__FILE(stream)->buf) {
free(__FILE(stream)->buf);
}
if (stream != stdout && stream != stderr && stream != stdin) {
free(stream);
}
}
static void __stdio_cleanup_impl(void)
{
write(1, "HELLO\n", 6);
fflush(stdout);
if (stdout->next != NULL) {
FILE *cur = stdout->next;
while (cur) {
struct __FILE *next = cur->next;
__fclose(cur);
cur = next;
}
}
}
void exit(int status)
{
void (*fptr)(void);
/* Only do stdio cleanup if it was referenced (meaning stdio was used)
*/
if (__stdio_cleanup) {
__stdio_cleanup_impl();
}
if (__atexit_fvec) {
fptr = __atexit_fvec;
while (fptr) {
fptr();
fptr++;
}
}
_exit(status);
}
|