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