-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathatexit.c
More file actions
175 lines (153 loc) · 3.56 KB
/
atexit.c
File metadata and controls
175 lines (153 loc) · 3.56 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* atexit.c
* This file has no copyright assigned and is placed in the Public Domain.
* This file is a part of the mingw-runtime package.
* No warranty is given; refer to the file DISCLAIMER within the package.
*
* Initialization code for DLLs.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#include <windows.h>
//#define DEBUG
#ifdef DEBUG
# define TRACE(FMT, ...) \
printf ("trace: %s:%d : " FMT, __FILE__, __LINE__, ##__VA_ARGS__)
# define FIXME(FMT, ...) \
printf ("fixme: %s:%d : " FMT, __FILE__, __LINE__, ##__VA_ARGS__)
#else
# define TRACE(FMT, ...) do; while (0)
# define FIXME(FMT, ...) do; while (0)
#endif
typedef void (* p_atexit_fn )(void);
static p_atexit_fn* first_atexit;
static p_atexit_fn* next_atexit;
/* This is based on the function in the Wine project's exit.c */
p_atexit_fn __dllonexit (p_atexit_fn, p_atexit_fn**, p_atexit_fn**);
int
atexit (p_atexit_fn pfn)
{
#ifdef DEBUG
printf ("%s: registering exit function 0x%x at 0x%x\n",
__FUNCTION__, (unsigned)pfn, (unsigned)next_atexit);
#endif
return (__dllonexit (pfn, &first_atexit, &next_atexit)
== NULL ? -1 : 0 );
}
_onexit_t
_onexit (_onexit_t pfn)
{
#ifdef DEBUG
printf ("%s: registering exit function 0x%x at 0x%x\n",
__FUNCTION__, (unsigned)pfn, (unsigned)next_atexit);
#endif
return ((_onexit_t) __dllonexit ((p_atexit_fn)pfn, &first_atexit, &next_atexit));
}
p_atexit_fn* __atexit_first(void)
{
return first_atexit;
}
BOOL __atexit_init(void)
{
/* Initialize atexit table.
32 is min size required by ANSI */
first_atexit = (p_atexit_fn*) malloc (32 * sizeof (p_atexit_fn));
if (first_atexit == NULL ) /* can't allocate memory */
{
#if defined(__MINGW32CE__) && !defined(__COREDLL__)
errno=ENOMEM;
#endif
return FALSE;
}
*first_atexit = NULL;
next_atexit = first_atexit;
return TRUE;
}
void __dll_exit(void)
/* Run LIFO terminators registered in private atexit table */
{
if ( first_atexit )
{
p_atexit_fn* __last = next_atexit - 1;
while ( __last >= first_atexit )
{
if ( *__last != NULL )
{
#ifdef DEBUG
printf ("%s: Calling exit function 0x%x from 0x%x\n",
__FUNCTION__, (unsigned)(*__last),(unsigned)__last);
#endif
(**__last) ();
}
__last--;
}
free ( first_atexit ) ;
first_atexit = NULL ;
}
/*
Make sure output buffers opened by DllMain or
atexit-registered functions are flushed before detaching,
otherwise we can have problems with redirected output.
*/
fflush (NULL);
}
p_atexit_fn
__dllonexit(p_atexit_fn func, p_atexit_fn **start, p_atexit_fn **end)
{
p_atexit_fn *tmp;
int len;
TRACE("(%p,%p,%p)\n", func, start, end);
if (!start || !*start || !end || !*end)
{
FIXME("bad table\n");
return NULL;
}
len = (*end - *start);
TRACE("table start %p-%p, %d entries\n", *start, *end, len);
if (++len <= 0)
return NULL;
tmp = (p_atexit_fn *)realloc(*start, len * sizeof(tmp));
if (!tmp)
return NULL;
*start = tmp;
*end = tmp + len;
tmp[len - 1] = func;
TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
return func;
}
static void
closeall_streams (void)
{
/* Closes all except stdin/stdout/stderr. */
_fcloseall ();
#if 0
fclose (stdin);
fclose (stdout); /* closing stdout hangs the process? */
fclose (stderr);
#endif
}
void
_cexit (void)
{
__dll_exit ();
closeall_streams ();
}
void
exit (int code)
{
_cexit ();
ExitProcess (code);
}
void
_c_exit (void)
{
closeall_streams ();
}
void
_exit (int code)
{
closeall_streams ();
ExitProcess (code);
}