1 | /*
|
---|
2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
3 | * Copyright (c) 2018 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <adt/list.h>
|
---|
37 | #include <fibril_synch.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include "private/libc.h"
|
---|
40 | #include "private/stdlib.h"
|
---|
41 |
|
---|
42 | static int glbl_seed = 1;
|
---|
43 |
|
---|
44 | static LIST_INITIALIZE(exit_handlers);
|
---|
45 | static FIBRIL_MUTEX_INITIALIZE(exit_handlers_lock);
|
---|
46 |
|
---|
47 | static LIST_INITIALIZE(quick_exit_handlers);
|
---|
48 | static FIBRIL_MUTEX_INITIALIZE(quick_exit_handlers_lock);
|
---|
49 |
|
---|
50 |
|
---|
51 | int rand(void)
|
---|
52 | {
|
---|
53 | return glbl_seed = ((1366 * glbl_seed + 150889) % RAND_MAX);
|
---|
54 | }
|
---|
55 |
|
---|
56 | void srand(unsigned int seed)
|
---|
57 | {
|
---|
58 | glbl_seed = seed % RAND_MAX;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** Register exit handler.
|
---|
62 | *
|
---|
63 | * @param func Function to be called during program terimnation
|
---|
64 | * @return Zero on success, nonzero on failure
|
---|
65 | */
|
---|
66 | int atexit(void (*func)(void))
|
---|
67 | {
|
---|
68 | __exit_handler_t *entry;
|
---|
69 |
|
---|
70 | entry = malloc(sizeof(__exit_handler_t));
|
---|
71 | if (entry == NULL)
|
---|
72 | return -1;
|
---|
73 |
|
---|
74 | entry->func = func;
|
---|
75 |
|
---|
76 | fibril_mutex_lock(&exit_handlers_lock);
|
---|
77 | list_prepend(&entry->llist, &exit_handlers);
|
---|
78 | fibril_mutex_unlock(&exit_handlers_lock);
|
---|
79 | return 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Terminate program with exit status.
|
---|
83 | *
|
---|
84 | * @param status Exit status
|
---|
85 | */
|
---|
86 | void exit(int status)
|
---|
87 | {
|
---|
88 | link_t *link;
|
---|
89 | __exit_handler_t *eh;
|
---|
90 |
|
---|
91 | /* Call exit handlers */
|
---|
92 | fibril_mutex_lock(&exit_handlers_lock);
|
---|
93 | while (!list_empty(&exit_handlers)) {
|
---|
94 | link = list_first(&exit_handlers);
|
---|
95 | list_remove(link);
|
---|
96 | fibril_mutex_unlock(&exit_handlers_lock);
|
---|
97 |
|
---|
98 | eh = list_get_instance(link, __exit_handler_t, llist);
|
---|
99 | eh->func();
|
---|
100 | free(eh);
|
---|
101 | fibril_mutex_lock(&exit_handlers_lock);
|
---|
102 | }
|
---|
103 |
|
---|
104 | fibril_mutex_unlock(&exit_handlers_lock);
|
---|
105 |
|
---|
106 | _Exit(status);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Register quick exit handler.
|
---|
110 | *
|
---|
111 | * @param func Function to be called during quick program terimnation
|
---|
112 | * @return Zero on success, nonzero on failure
|
---|
113 | */
|
---|
114 | int at_quick_exit(void (*func)(void))
|
---|
115 | {
|
---|
116 | __exit_handler_t *entry;
|
---|
117 |
|
---|
118 | entry = malloc(sizeof(__exit_handler_t));
|
---|
119 | if (entry == NULL)
|
---|
120 | return -1;
|
---|
121 |
|
---|
122 | entry->func = func;
|
---|
123 |
|
---|
124 | fibril_mutex_lock(&exit_handlers_lock);
|
---|
125 | list_prepend(&entry->llist, &exit_handlers);
|
---|
126 | fibril_mutex_unlock(&exit_handlers_lock);
|
---|
127 | return 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** Quickly terminate program with exit status.
|
---|
131 | *
|
---|
132 | * @param status Exit status
|
---|
133 | */
|
---|
134 | void quick_exit(int status)
|
---|
135 | {
|
---|
136 | link_t *link;
|
---|
137 | __exit_handler_t *eh;
|
---|
138 |
|
---|
139 | /* Call quick exit handlers */
|
---|
140 | fibril_mutex_lock(&quick_exit_handlers_lock);
|
---|
141 | while (!list_empty(&quick_exit_handlers)) {
|
---|
142 | link = list_first(&quick_exit_handlers);
|
---|
143 | list_remove(link);
|
---|
144 | fibril_mutex_unlock(&quick_exit_handlers_lock);
|
---|
145 |
|
---|
146 | eh = list_get_instance(link, __exit_handler_t, llist);
|
---|
147 | eh->func();
|
---|
148 | free(eh);
|
---|
149 | fibril_mutex_lock(&quick_exit_handlers_lock);
|
---|
150 | }
|
---|
151 |
|
---|
152 | fibril_mutex_unlock(&quick_exit_handlers_lock);
|
---|
153 |
|
---|
154 | _Exit(status);
|
---|
155 | }
|
---|
156 |
|
---|
157 | void _Exit(int status)
|
---|
158 | {
|
---|
159 | __libc_exit(status);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Abnormal program termination */
|
---|
163 | void abort(void)
|
---|
164 | {
|
---|
165 | __libc_abort();
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Get environment list entry.
|
---|
169 | *
|
---|
170 | * Note that this function is not reentrant. The returned string is only
|
---|
171 | * guaranteed to be valid until the next call to @c getenv.
|
---|
172 | *
|
---|
173 | * @param name Entry name
|
---|
174 | * @return Pointer to string or @c NULL if not found
|
---|
175 | */
|
---|
176 | char *getenv(const char *name)
|
---|
177 | {
|
---|
178 | (void) name;
|
---|
179 | return NULL;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Execute command.
|
---|
183 | *
|
---|
184 | * @param string Command to execute or @c NULL
|
---|
185 | *
|
---|
186 | * @return If @a string is @c NULL, return zero (no command processor
|
---|
187 | * available). If @a string is not @c NULL, return 1 (failure).
|
---|
188 | */
|
---|
189 | int system(const char *string)
|
---|
190 | {
|
---|
191 | if (string == NULL)
|
---|
192 | return 0;
|
---|
193 |
|
---|
194 | return 1;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** Compute quotient and remainder of int division.
|
---|
198 | *
|
---|
199 | * @param numer Numerator
|
---|
200 | * @param denom Denominator
|
---|
201 | * @return Structure containing quotient and remainder
|
---|
202 | */
|
---|
203 | div_t div(int numer, int denom)
|
---|
204 | {
|
---|
205 | div_t d;
|
---|
206 |
|
---|
207 | d.quot = numer / denom;
|
---|
208 | d.rem = numer % denom;
|
---|
209 |
|
---|
210 | return d;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Compute quotient and remainder of long division.
|
---|
214 | *
|
---|
215 | * @param numer Numerator
|
---|
216 | * @param denom Denominator
|
---|
217 | * @return Structure containing quotient and remainder
|
---|
218 | */
|
---|
219 | ldiv_t ldiv(long numer, long denom)
|
---|
220 | {
|
---|
221 | ldiv_t d;
|
---|
222 |
|
---|
223 | d.quot = numer / denom;
|
---|
224 | d.rem = numer % denom;
|
---|
225 |
|
---|
226 | return d;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Compute quotient and remainder of long long division.
|
---|
230 | *
|
---|
231 | * @param numer Numerator
|
---|
232 | * @param denom Denominator
|
---|
233 | * @return Structure containing quotient and remainder
|
---|
234 | */
|
---|
235 | lldiv_t lldiv(long long numer, long long denom)
|
---|
236 | {
|
---|
237 | lldiv_t d;
|
---|
238 |
|
---|
239 | d.quot = numer / denom;
|
---|
240 | d.rem = numer % denom;
|
---|
241 |
|
---|
242 | return d;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /** @}
|
---|
246 | */
|
---|