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