source: mainline/uspace/lib/posix/stdlib.c@ 2b83add

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2b83add was 2b83add, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 14 years ago

Add string to int conversion functions and implement some in stdlib.h

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[2fc5072]1/*
2 * Copyright (c) 2011 Petr Koupy
[ceebf0a]3 * Copyright (c) 2011 Jiri Zarevucky
[2fc5072]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
[4d10fc8]36#define LIBPOSIX_INTERNAL
[2fc5072]37
[4d10fc8]38#include "stdlib.h"
[2b83add]39#include "libc/sort.h"
40#include "libc/str.h"
41#include "libc/vfs/vfs.h"
[9b1503e]42#include "internal/common.h"
[2b83add]43#include <errno.h> // FIXME: use POSIX errno
[2fc5072]44
[823a929]45/**
46 *
47 * @param array
48 * @param count
49 * @param size
50 * @param compare
51 */
52int posix_atexit(void (*func)(void))
53{
54 // TODO: low priority, just a compile-time dependency of binutils
55 not_implemented();
56}
57
58/**
59 *
[2b83add]60 * @param i Input value.
61 * @return Absolute value of the parameter.
[823a929]62 */
63int posix_abs(int i)
64{
[2b83add]65 return i < 0 ? -i : i;
66}
67
68/**
69 *
70 * @param i Input value.
71 * @return Absolute value of the parameter.
72 */
73long posix_labs(long i)
74{
75 return i < 0 ? -i : i;
76}
77
78/**
79 *
80 * @param i Input value.
81 * @return Absolute value of the parameter.
82 */
83long long posix_llabs(long long i)
84{
85 return i < 0 ? -i : i;
86}
87
88/**
89 * Private helper function that serves as a compare function for qsort().
90 *
91 * @param elem1 First element to compare.
92 * @param elem2 Second element to compare.
93 * @param compare Comparison function without userdata parameter.
94 *
95 * @return Relative ordering of the elements.
96 */
97static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata)
98{
99 int (*compare)(const void *, const void *) = userdata;
100 return compare(elem1, elem2);
[823a929]101}
102
[2fc5072]103/**
[2b83add]104 * Array sorting utilizing the quicksort algorithm.
[2fc5072]105 *
106 * @param array
107 * @param count
108 * @param size
109 * @param compare
110 */
[4f4b4e7]111void posix_qsort(void *array, size_t count, size_t size,
112 int (*compare)(const void *, const void *))
[2fc5072]113{
[2b83add]114 /* Implemented in libc with one extra argument. */
115 qsort(array, count, size, sort_compare_wrapper, compare);
[2fc5072]116}
117
118/**
[2b83add]119 * Retrieve a value of the given environment variable.
120 * Since HelenOS doesn't support env variables at the moment,
121 * this function always returns NULL.
[2fc5072]122 *
123 * @param name
[2b83add]124 * @return Always NULL.
[2fc5072]125 */
126char *posix_getenv(const char *name)
127{
[2b83add]128 return NULL;
[2fc5072]129}
130
[823a929]131/**
132 *
133 * @param name
134 * @param resolved
135 * @return
136 */
137int posix_putenv(char *string)
138{
139 // TODO: low priority, just a compile-time dependency of binutils
140 not_implemented();
141}
142
[2fc5072]143/**
144 *
145 * @param name
146 * @param resolved
147 * @return
148 */
149char *posix_realpath(const char *name, char *resolved)
150{
[2b83add]151 #ifndef PATH_MAX
152 assert(resolved == NULL);
153 #endif
154
155 if (name == NULL) {
156 errno = EINVAL;
157 return NULL;
158 }
159
160 // TODO: symlink resolution
161
162 /* Function absolutize is implemented in libc and declared in vfs.h.
163 * No more processing is required as HelenOS doesn't have symlinks
164 * so far (as far as I can tell), although this function will need
165 * to be updated when that support is implemented.
166 */
167 char* absolute = absolutize(name, NULL);
168
169 if (absolute == NULL) {
170 /* POSIX requires some specific errnos to be set
171 * for some cases, but there is no way to find out from
172 * absolutize().
173 */
174 errno = EINVAL;
175 return NULL;
176 }
177
178 if (resolved == NULL) {
179 return absolute;
180 } else {
181 #ifdef PATH_MAX
182 str_cpy(resolved, PATH_MAX, absolute);
183 #endif
184 free(absolute);
185 return resolved;
186 }
[ceebf0a]187}
188
189/**
[63fc519]190 * Converts a string representation of a floating-point number to
191 * its native representation. See posix_strtold().
192 *
[ceebf0a]193 * @param nptr
194 * @return
195 */
[2b83add]196double posix_atof(const char *nptr)
[ceebf0a]197{
[2b83add]198 return posix_strtod(nptr, NULL);
[ceebf0a]199}
200
201/**
[63fc519]202 * Converts a string representation of a floating-point number to
203 * its native representation. See posix_strtold().
204 *
[ceebf0a]205 * @param nptr
206 * @param endptr
207 * @return
208 */
[2b83add]209float posix_strtof(const char *restrict nptr, char **restrict endptr)
[ceebf0a]210{
[2b83add]211 return (float) posix_strtold(nptr, endptr);
[2fc5072]212}
213
[09b0b1fb]214/**
[2b83add]215 * Converts a string representation of a floating-point number to
216 * its native representation. See posix_strtold().
217 *
218 * @param nptr
219 * @param endptr
[09b0b1fb]220 * @return
221 */
[2b83add]222double posix_strtod(const char *restrict nptr, char **restrict endptr)
[09b0b1fb]223{
[2b83add]224 return (double) posix_strtold(nptr, endptr);
[09b0b1fb]225}
226
[823a929]227/**
228 *
229 * @param size
230 * @return
231 */
232void *posix_malloc(size_t size)
233{
234 return malloc(size);
235}
236
237/**
238 *
239 * @param nelem
240 * @param elsize
241 * @return
242 */
243void *posix_calloc(size_t nelem, size_t elsize)
244{
245 return calloc(nelem, elsize);
246}
247
248/**
249 *
250 * @param ptr
251 * @param size
252 * @return
253 */
254void *posix_realloc(void *ptr, size_t size)
255{
256 return realloc(ptr, size);
257}
258
259/**
260 *
261 * @param ptr
262 */
263void posix_free(void *ptr)
264{
265 free(ptr);
266}
267
268/**
269 *
270 * @param tmpl
271 * @return
272 */
273char *posix_mktemp(char *tmpl)
274{
275 // TODO: low priority, just a compile-time dependency of binutils
276 not_implemented();
277}
278
[2fc5072]279/** @}
280 */
Note: See TracBrowser for help on using the repository browser.