[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 | */
|
---|
[087c8798] | 33 | /** @file Standard library definitions.
|
---|
[2fc5072] | 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 | /**
|
---|
[087c8798] | 61 | * Integer absolute value.
|
---|
[823a929] | 62 | *
|
---|
[2b83add] | 63 | * @param i Input value.
|
---|
| 64 | * @return Absolute value of the parameter.
|
---|
[823a929] | 65 | */
|
---|
| 66 | int posix_abs(int i)
|
---|
| 67 | {
|
---|
[2b83add] | 68 | return i < 0 ? -i : i;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
[087c8798] | 72 | * Long integer absolute value.
|
---|
[2b83add] | 73 | *
|
---|
| 74 | * @param i Input value.
|
---|
| 75 | * @return Absolute value of the parameter.
|
---|
| 76 | */
|
---|
| 77 | long posix_labs(long i)
|
---|
| 78 | {
|
---|
| 79 | return i < 0 ? -i : i;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
[087c8798] | 83 | * Long long integer absolute value.
|
---|
[2b83add] | 84 | *
|
---|
| 85 | * @param i Input value.
|
---|
| 86 | * @return Absolute value of the parameter.
|
---|
| 87 | */
|
---|
| 88 | long long posix_llabs(long long i)
|
---|
| 89 | {
|
---|
| 90 | return i < 0 ? -i : i;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[087c8798] | 93 | /**
|
---|
| 94 | * Compute the quotient and remainder of an integer division.
|
---|
| 95 | *
|
---|
| 96 | * @param numer Numerator.
|
---|
| 97 | * @param denom Denominator.
|
---|
| 98 | * @return Quotient and remainder packed into structure.
|
---|
| 99 | */
|
---|
[cc3652db] | 100 | posix_div_t posix_div(int numer, int denom)
|
---|
| 101 | {
|
---|
| 102 | return (posix_div_t) { .quot = numer / denom, .rem = numer % denom };
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[087c8798] | 105 | /**
|
---|
| 106 | * Compute the quotient and remainder of a long integer division.
|
---|
| 107 | *
|
---|
| 108 | * @param numer Numerator.
|
---|
| 109 | * @param denom Denominator.
|
---|
| 110 | * @return Quotient and remainder packed into structure.
|
---|
| 111 | */
|
---|
[cc3652db] | 112 | posix_ldiv_t posix_ldiv(long numer, long denom)
|
---|
| 113 | {
|
---|
| 114 | return (posix_ldiv_t) { .quot = numer / denom, .rem = numer % denom };
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[087c8798] | 117 | /**
|
---|
| 118 | * Compute the quotient and remainder of a long long integer division.
|
---|
| 119 | *
|
---|
| 120 | * @param numer Numerator.
|
---|
| 121 | * @param denom Denominator.
|
---|
| 122 | * @return Quotient and remainder packed into structure.
|
---|
| 123 | */
|
---|
[cc3652db] | 124 | posix_lldiv_t posix_lldiv(long long numer, long long denom)
|
---|
| 125 | {
|
---|
| 126 | return (posix_lldiv_t) { .quot = numer / denom, .rem = numer % denom };
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[2b83add] | 129 | /**
|
---|
| 130 | * Private helper function that serves as a compare function for qsort().
|
---|
| 131 | *
|
---|
| 132 | * @param elem1 First element to compare.
|
---|
| 133 | * @param elem2 Second element to compare.
|
---|
| 134 | * @param compare Comparison function without userdata parameter.
|
---|
| 135 | * @return Relative ordering of the elements.
|
---|
| 136 | */
|
---|
| 137 | static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata)
|
---|
| 138 | {
|
---|
| 139 | int (*compare)(const void *, const void *) = userdata;
|
---|
| 140 | return compare(elem1, elem2);
|
---|
[823a929] | 141 | }
|
---|
| 142 |
|
---|
[2fc5072] | 143 | /**
|
---|
[2b83add] | 144 | * Array sorting utilizing the quicksort algorithm.
|
---|
[2fc5072] | 145 | *
|
---|
[087c8798] | 146 | * @param array Array of elements to sort.
|
---|
| 147 | * @param count Number of elements in the array.
|
---|
| 148 | * @param size Width of each element.
|
---|
| 149 | * @param compare Decides relative ordering of two elements.
|
---|
[2fc5072] | 150 | */
|
---|
[4f4b4e7] | 151 | void posix_qsort(void *array, size_t count, size_t size,
|
---|
| 152 | int (*compare)(const void *, const void *))
|
---|
[2fc5072] | 153 | {
|
---|
[2b83add] | 154 | /* Implemented in libc with one extra argument. */
|
---|
| 155 | qsort(array, count, size, sort_compare_wrapper, compare);
|
---|
[2fc5072] | 156 | }
|
---|
| 157 |
|
---|
[cc3652db] | 158 | /**
|
---|
| 159 | * Binary search in a sorted array.
|
---|
| 160 | *
|
---|
| 161 | * @param key Object to search for.
|
---|
| 162 | * @param base Pointer to the first element of the array.
|
---|
| 163 | * @param nmemb Number of elements in the array.
|
---|
| 164 | * @param size Size of each array element.
|
---|
| 165 | * @param compar Comparison function.
|
---|
| 166 | * @return Pointer to a matching element, or NULL if none can be found.
|
---|
| 167 | */
|
---|
| 168 | void *posix_bsearch(const void *key, const void *base,
|
---|
| 169 | size_t nmemb, size_t size, int (*compar)(const void *, const void *))
|
---|
| 170 | {
|
---|
| 171 | while (nmemb > 0) {
|
---|
| 172 | const void *middle = base + (nmemb / 2) * size;
|
---|
| 173 | int cmp = compar(key, middle);
|
---|
| 174 | if (cmp == 0) {
|
---|
| 175 | return (void *) middle;
|
---|
| 176 | }
|
---|
| 177 | if (middle == base) {
|
---|
| 178 | /* There is just one member left to check and it
|
---|
| 179 | * didn't match the key. Avoid infinite loop.
|
---|
| 180 | */
|
---|
| 181 | break;
|
---|
| 182 | }
|
---|
| 183 | if (cmp < 0) {
|
---|
| 184 | nmemb = nmemb / 2;
|
---|
| 185 | } else if (cmp > 0) {
|
---|
| 186 | nmemb = nmemb - (nmemb / 2);
|
---|
| 187 | base = middle;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | return NULL;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[2fc5072] | 194 | /**
|
---|
[2b83add] | 195 | * Retrieve a value of the given environment variable.
|
---|
[087c8798] | 196 | *
|
---|
[2b83add] | 197 | * Since HelenOS doesn't support env variables at the moment,
|
---|
| 198 | * this function always returns NULL.
|
---|
[2fc5072] | 199 | *
|
---|
[087c8798] | 200 | * @param name Name of the variable.
|
---|
| 201 | * @return Value of the variable or NULL if such variable does not exist.
|
---|
[2fc5072] | 202 | */
|
---|
| 203 | char *posix_getenv(const char *name)
|
---|
| 204 | {
|
---|
[2b83add] | 205 | return NULL;
|
---|
[2fc5072] | 206 | }
|
---|
| 207 |
|
---|
[823a929] | 208 | /**
|
---|
| 209 | *
|
---|
| 210 | * @param name
|
---|
| 211 | * @param resolved
|
---|
| 212 | * @return
|
---|
| 213 | */
|
---|
| 214 | int posix_putenv(char *string)
|
---|
| 215 | {
|
---|
| 216 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 217 | not_implemented();
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[cc3652db] | 220 | /**
|
---|
[087c8798] | 221 | * Issue a command.
|
---|
[cc3652db] | 222 | *
|
---|
[087c8798] | 223 | * @param string String to be passed to a command interpreter or NULL.
|
---|
| 224 | * @return Termination status of the command if the command is not NULL,
|
---|
| 225 | * otherwise indicate whether there is a command interpreter (non-zero)
|
---|
| 226 | * or not (zero).
|
---|
[cc3652db] | 227 | */
|
---|
| 228 | int posix_system(const char *string) {
|
---|
| 229 | // TODO: does nothing at the moment
|
---|
| 230 | return 0;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[2fc5072] | 233 | /**
|
---|
[087c8798] | 234 | * Resolve absolute pathname.
|
---|
[2fc5072] | 235 | *
|
---|
[087c8798] | 236 | * @param name Pathname to be resolved.
|
---|
| 237 | * @param resolved Either buffer for the resolved absolute pathname or NULL.
|
---|
| 238 | * @return On success, either resolved (if it was not NULL) or pointer to the
|
---|
| 239 | * newly allocated buffer containing the absolute pathname (if resolved was
|
---|
| 240 | * NULL). Otherwise NULL.
|
---|
| 241 | *
|
---|
[2fc5072] | 242 | */
|
---|
[087c8798] | 243 | char *posix_realpath(const char *restrict name, char *restrict resolved)
|
---|
[2fc5072] | 244 | {
|
---|
[2b83add] | 245 | #ifndef PATH_MAX
|
---|
| 246 | assert(resolved == NULL);
|
---|
| 247 | #endif
|
---|
| 248 |
|
---|
| 249 | if (name == NULL) {
|
---|
| 250 | errno = EINVAL;
|
---|
| 251 | return NULL;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | // TODO: symlink resolution
|
---|
| 255 |
|
---|
| 256 | /* Function absolutize is implemented in libc and declared in vfs.h.
|
---|
| 257 | * No more processing is required as HelenOS doesn't have symlinks
|
---|
| 258 | * so far (as far as I can tell), although this function will need
|
---|
| 259 | * to be updated when that support is implemented.
|
---|
| 260 | */
|
---|
| 261 | char* absolute = absolutize(name, NULL);
|
---|
| 262 |
|
---|
| 263 | if (absolute == NULL) {
|
---|
| 264 | /* POSIX requires some specific errnos to be set
|
---|
| 265 | * for some cases, but there is no way to find out from
|
---|
| 266 | * absolutize().
|
---|
| 267 | */
|
---|
| 268 | errno = EINVAL;
|
---|
| 269 | return NULL;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | if (resolved == NULL) {
|
---|
| 273 | return absolute;
|
---|
| 274 | } else {
|
---|
| 275 | #ifdef PATH_MAX
|
---|
| 276 | str_cpy(resolved, PATH_MAX, absolute);
|
---|
| 277 | #endif
|
---|
| 278 | free(absolute);
|
---|
| 279 | return resolved;
|
---|
| 280 | }
|
---|
[ceebf0a] | 281 | }
|
---|
| 282 |
|
---|
| 283 | /**
|
---|
[63fc519] | 284 | * Converts a string representation of a floating-point number to
|
---|
| 285 | * its native representation. See posix_strtold().
|
---|
| 286 | *
|
---|
[087c8798] | 287 | * @param nptr String representation of a floating-point number.
|
---|
| 288 | * @return Double-precision number resulting from the string conversion.
|
---|
[ceebf0a] | 289 | */
|
---|
[2b83add] | 290 | double posix_atof(const char *nptr)
|
---|
[ceebf0a] | 291 | {
|
---|
[2b83add] | 292 | return posix_strtod(nptr, NULL);
|
---|
[ceebf0a] | 293 | }
|
---|
| 294 |
|
---|
| 295 | /**
|
---|
[63fc519] | 296 | * Converts a string representation of a floating-point number to
|
---|
| 297 | * its native representation. See posix_strtold().
|
---|
| 298 | *
|
---|
[087c8798] | 299 | * @param nptr String representation of a floating-point number.
|
---|
| 300 | * @param endptr Pointer to the final part of the string which
|
---|
| 301 | * was not used for conversion.
|
---|
| 302 | * @return Single-precision number resulting from the string conversion.
|
---|
[ceebf0a] | 303 | */
|
---|
[2b83add] | 304 | float posix_strtof(const char *restrict nptr, char **restrict endptr)
|
---|
[ceebf0a] | 305 | {
|
---|
[2b83add] | 306 | return (float) posix_strtold(nptr, endptr);
|
---|
[2fc5072] | 307 | }
|
---|
| 308 |
|
---|
[09b0b1fb] | 309 | /**
|
---|
[2b83add] | 310 | * Converts a string representation of a floating-point number to
|
---|
| 311 | * its native representation. See posix_strtold().
|
---|
| 312 | *
|
---|
[087c8798] | 313 | * @param nptr String representation of a floating-point number.
|
---|
| 314 | * @param endptr Pointer to the final part of the string which
|
---|
| 315 | * was not used for conversion.
|
---|
| 316 | * @return Double-precision number resulting from the string conversion.
|
---|
[09b0b1fb] | 317 | */
|
---|
[2b83add] | 318 | double posix_strtod(const char *restrict nptr, char **restrict endptr)
|
---|
[09b0b1fb] | 319 | {
|
---|
[2b83add] | 320 | return (double) posix_strtold(nptr, endptr);
|
---|
[09b0b1fb] | 321 | }
|
---|
| 322 |
|
---|
[823a929] | 323 | /**
|
---|
[087c8798] | 324 | * Allocate memory chunk.
|
---|
[823a929] | 325 | *
|
---|
[087c8798] | 326 | * @param size Size of the chunk to allocate.
|
---|
| 327 | * @return Either pointer to the allocated chunk or NULL if not possible.
|
---|
[823a929] | 328 | */
|
---|
| 329 | void *posix_malloc(size_t size)
|
---|
| 330 | {
|
---|
| 331 | return malloc(size);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | /**
|
---|
[087c8798] | 335 | * Allocate memory for an array of elements.
|
---|
[823a929] | 336 | *
|
---|
[087c8798] | 337 | * @param nelem Number of elements in the array.
|
---|
| 338 | * @param elsize Size of each element.
|
---|
| 339 | * @return Either pointer to the allocated array or NULL if not possible.
|
---|
[823a929] | 340 | */
|
---|
| 341 | void *posix_calloc(size_t nelem, size_t elsize)
|
---|
| 342 | {
|
---|
| 343 | return calloc(nelem, elsize);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | /**
|
---|
[087c8798] | 347 | * Reallocate memory chunk to a new size.
|
---|
[823a929] | 348 | *
|
---|
[087c8798] | 349 | * @param ptr Memory chunk to reallocate. Might be NULL.
|
---|
| 350 | * @param size Size of the reallocated chunk. Might be zero.
|
---|
| 351 | * @return Either NULL or the pointer to the newly reallocated chunk.
|
---|
[823a929] | 352 | */
|
---|
| 353 | void *posix_realloc(void *ptr, size_t size)
|
---|
| 354 | {
|
---|
| 355 | return realloc(ptr, size);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | /**
|
---|
[087c8798] | 359 | * Free allocated memory chunk.
|
---|
[823a929] | 360 | *
|
---|
[087c8798] | 361 | * @param ptr Memory chunk to be freed.
|
---|
[823a929] | 362 | */
|
---|
| 363 | void posix_free(void *ptr)
|
---|
| 364 | {
|
---|
[ca1f1ec] | 365 | if (ptr) {
|
---|
| 366 | free(ptr);
|
---|
| 367 | }
|
---|
[823a929] | 368 | }
|
---|
| 369 |
|
---|
| 370 | /**
|
---|
| 371 | *
|
---|
| 372 | * @param tmpl
|
---|
| 373 | * @return
|
---|
| 374 | */
|
---|
| 375 | char *posix_mktemp(char *tmpl)
|
---|
| 376 | {
|
---|
| 377 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 378 | not_implemented();
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[3acff69] | 381 | /**
|
---|
[087c8798] | 382 | * Get system load average statistics.
|
---|
[3acff69] | 383 | *
|
---|
[087c8798] | 384 | * Not supported. Always returns -1.
|
---|
| 385 | *
|
---|
| 386 | * @param loadavg Array where the load averages shall be placed.
|
---|
| 387 | * @param nelem Maximum number of elements to be placed into the array.
|
---|
| 388 | * @return Number of elements placed into the array on success, -1 otherwise.
|
---|
[3acff69] | 389 | */
|
---|
| 390 | int bsd_getloadavg(double loadavg[], int nelem)
|
---|
| 391 | {
|
---|
| 392 | return -1;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[2fc5072] | 395 | /** @}
|
---|
| 396 | */
|
---|