[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 |
|
---|
[ec18957a] | 41 | #include "errno.h"
|
---|
| 42 |
|
---|
[2b83add] | 43 | #include "libc/sort.h"
|
---|
| 44 | #include "libc/str.h"
|
---|
| 45 | #include "libc/vfs/vfs.h"
|
---|
[2fc5072] | 46 |
|
---|
[823a929] | 47 | /**
|
---|
| 48 | *
|
---|
| 49 | * @param array
|
---|
| 50 | * @param count
|
---|
| 51 | * @param size
|
---|
| 52 | * @param compare
|
---|
| 53 | */
|
---|
| 54 | int posix_atexit(void (*func)(void))
|
---|
| 55 | {
|
---|
| 56 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 57 | not_implemented();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | *
|
---|
[2b83add] | 62 | * @param i Input value.
|
---|
| 63 | * @return Absolute value of the parameter.
|
---|
[823a929] | 64 | */
|
---|
| 65 | int posix_abs(int i)
|
---|
| 66 | {
|
---|
[2b83add] | 67 | return i < 0 ? -i : i;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | *
|
---|
| 72 | * @param i Input value.
|
---|
| 73 | * @return Absolute value of the parameter.
|
---|
| 74 | */
|
---|
| 75 | long 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 | */
|
---|
| 85 | long long posix_llabs(long long i)
|
---|
| 86 | {
|
---|
| 87 | return i < 0 ? -i : i;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[cc3652db] | 90 | posix_div_t posix_div(int numer, int denom)
|
---|
| 91 | {
|
---|
| 92 | return (posix_div_t) { .quot = numer / denom, .rem = numer % denom };
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | posix_ldiv_t posix_ldiv(long numer, long denom)
|
---|
| 96 | {
|
---|
| 97 | return (posix_ldiv_t) { .quot = numer / denom, .rem = numer % denom };
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | posix_lldiv_t posix_lldiv(long long numer, long long denom)
|
---|
| 101 | {
|
---|
| 102 | return (posix_lldiv_t) { .quot = numer / denom, .rem = numer % denom };
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[2b83add] | 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 | */
|
---|
| 114 | static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata)
|
---|
| 115 | {
|
---|
| 116 | int (*compare)(const void *, const void *) = userdata;
|
---|
| 117 | return compare(elem1, elem2);
|
---|
[823a929] | 118 | }
|
---|
| 119 |
|
---|
[2fc5072] | 120 | /**
|
---|
[2b83add] | 121 | * Array sorting utilizing the quicksort algorithm.
|
---|
[2fc5072] | 122 | *
|
---|
| 123 | * @param array
|
---|
| 124 | * @param count
|
---|
| 125 | * @param size
|
---|
| 126 | * @param compare
|
---|
| 127 | */
|
---|
[4f4b4e7] | 128 | void posix_qsort(void *array, size_t count, size_t size,
|
---|
| 129 | int (*compare)(const void *, const void *))
|
---|
[2fc5072] | 130 | {
|
---|
[2b83add] | 131 | /* Implemented in libc with one extra argument. */
|
---|
| 132 | qsort(array, count, size, sort_compare_wrapper, compare);
|
---|
[2fc5072] | 133 | }
|
---|
| 134 |
|
---|
[cc3652db] | 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 | */
|
---|
| 145 | void *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 |
|
---|
[2fc5072] | 171 | /**
|
---|
[2b83add] | 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.
|
---|
[2fc5072] | 175 | *
|
---|
| 176 | * @param name
|
---|
[2b83add] | 177 | * @return Always NULL.
|
---|
[2fc5072] | 178 | */
|
---|
| 179 | char *posix_getenv(const char *name)
|
---|
| 180 | {
|
---|
[2b83add] | 181 | return NULL;
|
---|
[2fc5072] | 182 | }
|
---|
| 183 |
|
---|
[823a929] | 184 | /**
|
---|
| 185 | *
|
---|
| 186 | * @param name
|
---|
| 187 | * @param resolved
|
---|
| 188 | * @return
|
---|
| 189 | */
|
---|
| 190 | int posix_putenv(char *string)
|
---|
| 191 | {
|
---|
| 192 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 193 | not_implemented();
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[cc3652db] | 196 | /**
|
---|
| 197 | *
|
---|
| 198 | * @param string String to be passed to a command interpreter.
|
---|
| 199 | * @return
|
---|
| 200 | */
|
---|
| 201 | int posix_system(const char *string) {
|
---|
| 202 | // TODO: does nothing at the moment
|
---|
| 203 | return 0;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[2fc5072] | 206 | /**
|
---|
| 207 | *
|
---|
| 208 | * @param name
|
---|
| 209 | * @param resolved
|
---|
| 210 | * @return
|
---|
| 211 | */
|
---|
| 212 | char *posix_realpath(const char *name, char *resolved)
|
---|
| 213 | {
|
---|
[2b83add] | 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 | }
|
---|
[ceebf0a] | 250 | }
|
---|
| 251 |
|
---|
| 252 | /**
|
---|
[63fc519] | 253 | * Converts a string representation of a floating-point number to
|
---|
| 254 | * its native representation. See posix_strtold().
|
---|
| 255 | *
|
---|
[ceebf0a] | 256 | * @param nptr
|
---|
| 257 | * @return
|
---|
| 258 | */
|
---|
[2b83add] | 259 | double posix_atof(const char *nptr)
|
---|
[ceebf0a] | 260 | {
|
---|
[2b83add] | 261 | return posix_strtod(nptr, NULL);
|
---|
[ceebf0a] | 262 | }
|
---|
| 263 |
|
---|
| 264 | /**
|
---|
[63fc519] | 265 | * Converts a string representation of a floating-point number to
|
---|
| 266 | * its native representation. See posix_strtold().
|
---|
| 267 | *
|
---|
[ceebf0a] | 268 | * @param nptr
|
---|
| 269 | * @param endptr
|
---|
| 270 | * @return
|
---|
| 271 | */
|
---|
[2b83add] | 272 | float posix_strtof(const char *restrict nptr, char **restrict endptr)
|
---|
[ceebf0a] | 273 | {
|
---|
[2b83add] | 274 | return (float) posix_strtold(nptr, endptr);
|
---|
[2fc5072] | 275 | }
|
---|
| 276 |
|
---|
[09b0b1fb] | 277 | /**
|
---|
[2b83add] | 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
|
---|
[09b0b1fb] | 283 | * @return
|
---|
| 284 | */
|
---|
[2b83add] | 285 | double posix_strtod(const char *restrict nptr, char **restrict endptr)
|
---|
[09b0b1fb] | 286 | {
|
---|
[2b83add] | 287 | return (double) posix_strtold(nptr, endptr);
|
---|
[09b0b1fb] | 288 | }
|
---|
| 289 |
|
---|
[823a929] | 290 | /**
|
---|
| 291 | *
|
---|
| 292 | * @param size
|
---|
| 293 | * @return
|
---|
| 294 | */
|
---|
| 295 | void *posix_malloc(size_t size)
|
---|
| 296 | {
|
---|
| 297 | return malloc(size);
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | /**
|
---|
| 301 | *
|
---|
| 302 | * @param nelem
|
---|
| 303 | * @param elsize
|
---|
| 304 | * @return
|
---|
| 305 | */
|
---|
| 306 | void *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 | */
|
---|
| 317 | void *posix_realloc(void *ptr, size_t size)
|
---|
| 318 | {
|
---|
| 319 | return realloc(ptr, size);
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | /**
|
---|
| 323 | *
|
---|
| 324 | * @param ptr
|
---|
| 325 | */
|
---|
| 326 | void posix_free(void *ptr)
|
---|
| 327 | {
|
---|
| 328 | free(ptr);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /**
|
---|
| 332 | *
|
---|
| 333 | * @param tmpl
|
---|
| 334 | * @return
|
---|
| 335 | */
|
---|
| 336 | char *posix_mktemp(char *tmpl)
|
---|
| 337 | {
|
---|
| 338 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 339 | not_implemented();
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[3acff69] | 342 | /**
|
---|
| 343 | * Should read system load statistics. Not supported. Always returns -1.
|
---|
| 344 | *
|
---|
| 345 | * @param loadavg
|
---|
| 346 | * @param nelem
|
---|
| 347 | * @return
|
---|
| 348 | */
|
---|
| 349 | int bsd_getloadavg(double loadavg[], int nelem)
|
---|
| 350 | {
|
---|
| 351 | return -1;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[2fc5072] | 354 | /** @}
|
---|
| 355 | */
|
---|