source: mainline/uspace/lib/posix/stdlib.c@ 5273eb6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5273eb6 was ca1f1ec, checked in by Petr Koupy <petr.koupy@…>, 14 years ago

Edited posix_free() wrapper to catch NULL argument (native free() assumes non-zero pointer).

  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * Copyright (c) 2011 Petr Koupy
3 * Copyright (c) 2011 Jiri Zarevucky
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 libposix
31 * @{
32 */
33/** @file
34 */
35
36#define LIBPOSIX_INTERNAL
37
38#include "internal/common.h"
39#include "stdlib.h"
40
41#include "errno.h"
42
43#include "libc/sort.h"
44#include "libc/str.h"
45#include "libc/vfs/vfs.h"
46
47/**
48 *
49 * @param array
50 * @param count
51 * @param size
52 * @param compare
53 */
54int posix_atexit(void (*func)(void))
55{
56 // TODO: low priority, just a compile-time dependency of binutils
57 not_implemented();
58}
59
60/**
61 *
62 * @param i Input value.
63 * @return Absolute value of the parameter.
64 */
65int posix_abs(int i)
66{
67 return i < 0 ? -i : i;
68}
69
70/**
71 *
72 * @param i Input value.
73 * @return Absolute value of the parameter.
74 */
75long posix_labs(long i)
76{
77 return i < 0 ? -i : i;
78}
79
80/**
81 *
82 * @param i Input value.
83 * @return Absolute value of the parameter.
84 */
85long long posix_llabs(long long i)
86{
87 return i < 0 ? -i : i;
88}
89
90posix_div_t posix_div(int numer, int denom)
91{
92 return (posix_div_t) { .quot = numer / denom, .rem = numer % denom };
93}
94
95posix_ldiv_t posix_ldiv(long numer, long denom)
96{
97 return (posix_ldiv_t) { .quot = numer / denom, .rem = numer % denom };
98}
99
100posix_lldiv_t posix_lldiv(long long numer, long long denom)
101{
102 return (posix_lldiv_t) { .quot = numer / denom, .rem = numer % denom };
103}
104
105/**
106 * Private helper function that serves as a compare function for qsort().
107 *
108 * @param elem1 First element to compare.
109 * @param elem2 Second element to compare.
110 * @param compare Comparison function without userdata parameter.
111 *
112 * @return Relative ordering of the elements.
113 */
114static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata)
115{
116 int (*compare)(const void *, const void *) = userdata;
117 return compare(elem1, elem2);
118}
119
120/**
121 * Array sorting utilizing the quicksort algorithm.
122 *
123 * @param array
124 * @param count
125 * @param size
126 * @param compare
127 */
128void posix_qsort(void *array, size_t count, size_t size,
129 int (*compare)(const void *, const void *))
130{
131 /* Implemented in libc with one extra argument. */
132 qsort(array, count, size, sort_compare_wrapper, compare);
133}
134
135/**
136 * Binary search in a sorted array.
137 *
138 * @param key Object to search for.
139 * @param base Pointer to the first element of the array.
140 * @param nmemb Number of elements in the array.
141 * @param size Size of each array element.
142 * @param compar Comparison function.
143 * @return Pointer to a matching element, or NULL if none can be found.
144 */
145void *posix_bsearch(const void *key, const void *base,
146 size_t nmemb, size_t size, int (*compar)(const void *, const void *))
147{
148 while (nmemb > 0) {
149 const void *middle = base + (nmemb / 2) * size;
150 int cmp = compar(key, middle);
151 if (cmp == 0) {
152 return (void *) middle;
153 }
154 if (middle == base) {
155 /* There is just one member left to check and it
156 * didn't match the key. Avoid infinite loop.
157 */
158 break;
159 }
160 if (cmp < 0) {
161 nmemb = nmemb / 2;
162 } else if (cmp > 0) {
163 nmemb = nmemb - (nmemb / 2);
164 base = middle;
165 }
166 }
167
168 return NULL;
169}
170
171/**
172 * Retrieve a value of the given environment variable.
173 * Since HelenOS doesn't support env variables at the moment,
174 * this function always returns NULL.
175 *
176 * @param name
177 * @return Always NULL.
178 */
179char *posix_getenv(const char *name)
180{
181 return NULL;
182}
183
184/**
185 *
186 * @param name
187 * @param resolved
188 * @return
189 */
190int posix_putenv(char *string)
191{
192 // TODO: low priority, just a compile-time dependency of binutils
193 not_implemented();
194}
195
196/**
197 *
198 * @param string String to be passed to a command interpreter.
199 * @return
200 */
201int posix_system(const char *string) {
202 // TODO: does nothing at the moment
203 return 0;
204}
205
206/**
207 *
208 * @param name
209 * @param resolved
210 * @return
211 */
212char *posix_realpath(const char *name, char *resolved)
213{
214 #ifndef PATH_MAX
215 assert(resolved == NULL);
216 #endif
217
218 if (name == NULL) {
219 errno = EINVAL;
220 return NULL;
221 }
222
223 // TODO: symlink resolution
224
225 /* Function absolutize is implemented in libc and declared in vfs.h.
226 * No more processing is required as HelenOS doesn't have symlinks
227 * so far (as far as I can tell), although this function will need
228 * to be updated when that support is implemented.
229 */
230 char* absolute = absolutize(name, NULL);
231
232 if (absolute == NULL) {
233 /* POSIX requires some specific errnos to be set
234 * for some cases, but there is no way to find out from
235 * absolutize().
236 */
237 errno = EINVAL;
238 return NULL;
239 }
240
241 if (resolved == NULL) {
242 return absolute;
243 } else {
244 #ifdef PATH_MAX
245 str_cpy(resolved, PATH_MAX, absolute);
246 #endif
247 free(absolute);
248 return resolved;
249 }
250}
251
252/**
253 * Converts a string representation of a floating-point number to
254 * its native representation. See posix_strtold().
255 *
256 * @param nptr
257 * @return
258 */
259double posix_atof(const char *nptr)
260{
261 return posix_strtod(nptr, NULL);
262}
263
264/**
265 * Converts a string representation of a floating-point number to
266 * its native representation. See posix_strtold().
267 *
268 * @param nptr
269 * @param endptr
270 * @return
271 */
272float posix_strtof(const char *restrict nptr, char **restrict endptr)
273{
274 return (float) posix_strtold(nptr, endptr);
275}
276
277/**
278 * Converts a string representation of a floating-point number to
279 * its native representation. See posix_strtold().
280 *
281 * @param nptr
282 * @param endptr
283 * @return
284 */
285double posix_strtod(const char *restrict nptr, char **restrict endptr)
286{
287 return (double) posix_strtold(nptr, endptr);
288}
289
290/**
291 *
292 * @param size
293 * @return
294 */
295void *posix_malloc(size_t size)
296{
297 return malloc(size);
298}
299
300/**
301 *
302 * @param nelem
303 * @param elsize
304 * @return
305 */
306void *posix_calloc(size_t nelem, size_t elsize)
307{
308 return calloc(nelem, elsize);
309}
310
311/**
312 *
313 * @param ptr
314 * @param size
315 * @return
316 */
317void *posix_realloc(void *ptr, size_t size)
318{
319 return realloc(ptr, size);
320}
321
322/**
323 *
324 * @param ptr
325 */
326void posix_free(void *ptr)
327{
328 if (ptr) {
329 free(ptr);
330 }
331}
332
333/**
334 *
335 * @param tmpl
336 * @return
337 */
338char *posix_mktemp(char *tmpl)
339{
340 // TODO: low priority, just a compile-time dependency of binutils
341 not_implemented();
342}
343
344/**
345 * Should read system load statistics. Not supported. Always returns -1.
346 *
347 * @param loadavg
348 * @param nelem
349 * @return
350 */
351int bsd_getloadavg(double loadavg[], int nelem)
352{
353 return -1;
354}
355
356/** @}
357 */
Note: See TracBrowser for help on using the repository browser.