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 "stdlib.h"
|
---|
39 | #include "libc/sort.h"
|
---|
40 | #include "libc/str.h"
|
---|
41 | #include "libc/vfs/vfs.h"
|
---|
42 | #include "internal/common.h"
|
---|
43 | #include <errno.h> // FIXME: use POSIX errno
|
---|
44 |
|
---|
45 | /**
|
---|
46 | *
|
---|
47 | * @param array
|
---|
48 | * @param count
|
---|
49 | * @param size
|
---|
50 | * @param compare
|
---|
51 | */
|
---|
52 | int posix_atexit(void (*func)(void))
|
---|
53 | {
|
---|
54 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
55 | not_implemented();
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | *
|
---|
60 | * @param i Input value.
|
---|
61 | * @return Absolute value of the parameter.
|
---|
62 | */
|
---|
63 | int posix_abs(int i)
|
---|
64 | {
|
---|
65 | return i < 0 ? -i : i;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | *
|
---|
70 | * @param i Input value.
|
---|
71 | * @return Absolute value of the parameter.
|
---|
72 | */
|
---|
73 | long 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 | */
|
---|
83 | long 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 | */
|
---|
97 | static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata)
|
---|
98 | {
|
---|
99 | int (*compare)(const void *, const void *) = userdata;
|
---|
100 | return compare(elem1, elem2);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Array sorting utilizing the quicksort algorithm.
|
---|
105 | *
|
---|
106 | * @param array
|
---|
107 | * @param count
|
---|
108 | * @param size
|
---|
109 | * @param compare
|
---|
110 | */
|
---|
111 | void posix_qsort(void *array, size_t count, size_t size,
|
---|
112 | int (*compare)(const void *, const void *))
|
---|
113 | {
|
---|
114 | /* Implemented in libc with one extra argument. */
|
---|
115 | qsort(array, count, size, sort_compare_wrapper, compare);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
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.
|
---|
122 | *
|
---|
123 | * @param name
|
---|
124 | * @return Always NULL.
|
---|
125 | */
|
---|
126 | char *posix_getenv(const char *name)
|
---|
127 | {
|
---|
128 | return NULL;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | *
|
---|
133 | * @param name
|
---|
134 | * @param resolved
|
---|
135 | * @return
|
---|
136 | */
|
---|
137 | int posix_putenv(char *string)
|
---|
138 | {
|
---|
139 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
140 | not_implemented();
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | *
|
---|
145 | * @param name
|
---|
146 | * @param resolved
|
---|
147 | * @return
|
---|
148 | */
|
---|
149 | char *posix_realpath(const char *name, char *resolved)
|
---|
150 | {
|
---|
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 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Converts a string representation of a floating-point number to
|
---|
191 | * its native representation. See posix_strtold().
|
---|
192 | *
|
---|
193 | * @param nptr
|
---|
194 | * @return
|
---|
195 | */
|
---|
196 | double posix_atof(const char *nptr)
|
---|
197 | {
|
---|
198 | return posix_strtod(nptr, NULL);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Converts a string representation of a floating-point number to
|
---|
203 | * its native representation. See posix_strtold().
|
---|
204 | *
|
---|
205 | * @param nptr
|
---|
206 | * @param endptr
|
---|
207 | * @return
|
---|
208 | */
|
---|
209 | float posix_strtof(const char *restrict nptr, char **restrict endptr)
|
---|
210 | {
|
---|
211 | return (float) posix_strtold(nptr, endptr);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
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
|
---|
220 | * @return
|
---|
221 | */
|
---|
222 | double posix_strtod(const char *restrict nptr, char **restrict endptr)
|
---|
223 | {
|
---|
224 | return (double) posix_strtold(nptr, endptr);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | *
|
---|
229 | * @param size
|
---|
230 | * @return
|
---|
231 | */
|
---|
232 | void *posix_malloc(size_t size)
|
---|
233 | {
|
---|
234 | return malloc(size);
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | *
|
---|
239 | * @param nelem
|
---|
240 | * @param elsize
|
---|
241 | * @return
|
---|
242 | */
|
---|
243 | void *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 | */
|
---|
254 | void *posix_realloc(void *ptr, size_t size)
|
---|
255 | {
|
---|
256 | return realloc(ptr, size);
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | *
|
---|
261 | * @param ptr
|
---|
262 | */
|
---|
263 | void posix_free(void *ptr)
|
---|
264 | {
|
---|
265 | free(ptr);
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | *
|
---|
270 | * @param tmpl
|
---|
271 | * @return
|
---|
272 | */
|
---|
273 | char *posix_mktemp(char *tmpl)
|
---|
274 | {
|
---|
275 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
276 | not_implemented();
|
---|
277 | }
|
---|
278 |
|
---|
279 | /** @}
|
---|
280 | */
|
---|