Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/stdlib.c

    rec18957a r102a729  
    3131 * @{
    3232 */
    33 /** @file
     33/** @file Standard library definitions.
    3434 */
    3535
     
    4040
    4141#include "errno.h"
     42#include "limits.h"
    4243
    4344#include "libc/sort.h"
     
    5960
    6061/**
     62 * Integer absolute value.
    6163 *
    6264 * @param i Input value.
     
    6971
    7072/**
     73 * Long integer absolute value.
    7174 *
    7275 * @param i Input value.
     
    7982
    8083/**
     84 * Long long integer absolute value.
    8185 *
    8286 * @param i Input value.
     
    8892}
    8993
     94/**
     95 * Compute the quotient and remainder of an integer division.
     96 *
     97 * @param numer Numerator.
     98 * @param denom Denominator.
     99 * @return Quotient and remainder packed into structure.
     100 */
    90101posix_div_t posix_div(int numer, int denom)
    91102{
     
    93104}
    94105
     106/**
     107 * Compute the quotient and remainder of a long integer division.
     108 *
     109 * @param numer Numerator.
     110 * @param denom Denominator.
     111 * @return Quotient and remainder packed into structure.
     112 */
    95113posix_ldiv_t posix_ldiv(long numer, long denom)
    96114{
     
    98116}
    99117
     118/**
     119 * Compute the quotient and remainder of a long long integer division.
     120 *
     121 * @param numer Numerator.
     122 * @param denom Denominator.
     123 * @return Quotient and remainder packed into structure.
     124 */
    100125posix_lldiv_t posix_lldiv(long long numer, long long denom)
    101126{
     
    109134 * @param elem2 Second element to compare.
    110135 * @param compare Comparison function without userdata parameter.
    111  *
    112136 * @return Relative ordering of the elements.
    113137 */
     
    115139{
    116140        int (*compare)(const void *, const void *) = userdata;
    117         return compare(elem1, elem2);
     141        int ret = compare(elem1, elem2);
     142       
     143        /* Native qsort internals expect this. */
     144        if (ret < 0) {
     145                return -1;
     146        } else if (ret > 0) {
     147                return 1;
     148        } else {
     149                return 0;
     150        }
    118151}
    119152
     
    121154 * Array sorting utilizing the quicksort algorithm.
    122155 *
    123  * @param array
    124  * @param count
    125  * @param size
    126  * @param compare
     156 * @param array Array of elements to sort.
     157 * @param count Number of elements in the array.
     158 * @param size Width of each element.
     159 * @param compare Decides relative ordering of two elements.
    127160 */
    128161void posix_qsort(void *array, size_t count, size_t size,
     
    171204/**
    172205 * Retrieve a value of the given environment variable.
     206 *
    173207 * Since HelenOS doesn't support env variables at the moment,
    174208 * this function always returns NULL.
    175209 *
    176  * @param name
    177  * @return Always NULL.
     210 * @param name Name of the variable.
     211 * @return Value of the variable or NULL if such variable does not exist.
    178212 */
    179213char *posix_getenv(const char *name)
     
    195229
    196230/**
    197  *
    198  * @param string String to be passed to a command interpreter.
    199  * @return
     231 * Issue a command.
     232 *
     233 * @param string String to be passed to a command interpreter or NULL.
     234 * @return Termination status of the command if the command is not NULL,
     235 *     otherwise indicate whether there is a command interpreter (non-zero)
     236 *     or not (zero).
    200237 */
    201238int posix_system(const char *string) {
     
    205242
    206243/**
    207  *
    208  * @param name
    209  * @param resolved
    210  * @return
    211  */
    212 char *posix_realpath(const char *name, char *resolved)
     244 * Resolve absolute pathname.
     245 *
     246 * @param name Pathname to be resolved.
     247 * @param resolved Either buffer for the resolved absolute pathname or NULL.
     248 * @return On success, either resolved (if it was not NULL) or pointer to the
     249 *     newly allocated buffer containing the absolute pathname (if resolved was
     250 *     NULL). Otherwise NULL.
     251 *
     252 */
     253char *posix_realpath(const char *restrict name, char *restrict resolved)
    213254{
    214255        #ifndef PATH_MAX
     
    254295 * its native representation. See posix_strtold().
    255296 *
    256  * @param nptr
    257  * @return
     297 * @param nptr String representation of a floating-point number.
     298 * @return Double-precision number resulting from the string conversion.
    258299 */
    259300double posix_atof(const char *nptr)
     
    266307 * its native representation. See posix_strtold().
    267308 *
    268  * @param nptr
    269  * @param endptr
    270  * @return
     309 * @param nptr String representation of a floating-point number.
     310 * @param endptr Pointer to the final part of the string which
     311 *     was not used for conversion.
     312 * @return Single-precision number resulting from the string conversion.
    271313 */
    272314float posix_strtof(const char *restrict nptr, char **restrict endptr)
     
    279321 * its native representation. See posix_strtold().
    280322 *
    281  * @param nptr
    282  * @param endptr
    283  * @return
     323 * @param nptr String representation of a floating-point number.
     324 * @param endptr Pointer to the final part of the string which
     325 *     was not used for conversion.
     326 * @return Double-precision number resulting from the string conversion.
    284327 */
    285328double posix_strtod(const char *restrict nptr, char **restrict endptr)
     
    289332
    290333/**
    291  *
    292  * @param size
    293  * @return
     334 * Allocate memory chunk.
     335 *
     336 * @param size Size of the chunk to allocate.
     337 * @return Either pointer to the allocated chunk or NULL if not possible.
    294338 */
    295339void *posix_malloc(size_t size)
     
    299343
    300344/**
    301  *
    302  * @param nelem
    303  * @param elsize
    304  * @return
     345 * Allocate memory for an array of elements.
     346 *
     347 * @param nelem Number of elements in the array.
     348 * @param elsize Size of each element.
     349 * @return Either pointer to the allocated array or NULL if not possible.
    305350 */
    306351void *posix_calloc(size_t nelem, size_t elsize)
     
    310355
    311356/**
    312  *
    313  * @param ptr
    314  * @param size
    315  * @return
     357 * Reallocate memory chunk to a new size.
     358 *
     359 * @param ptr Memory chunk to reallocate. Might be NULL.
     360 * @param size Size of the reallocated chunk. Might be zero.
     361 * @return Either NULL or the pointer to the newly reallocated chunk.
    316362 */
    317363void *posix_realloc(void *ptr, size_t size)
    318364{
    319         return realloc(ptr, size);
    320 }
    321 
    322 /**
    323  *
    324  * @param ptr
     365        if (ptr != NULL && size == 0) {
     366                /* Native realloc does not handle this special case. */
     367                free(ptr);
     368                return NULL;
     369        } else {
     370                return realloc(ptr, size);
     371        }
     372}
     373
     374/**
     375 * Free allocated memory chunk.
     376 *
     377 * @param ptr Memory chunk to be freed.
    325378 */
    326379void posix_free(void *ptr)
    327380{
    328         free(ptr);
     381        if (ptr) {
     382                free(ptr);
     383        }
    329384}
    330385
     
    341396
    342397/**
    343  * Should read system load statistics. Not supported. Always returns -1.
    344  *
    345  * @param loadavg
    346  * @param nelem
    347  * @return
     398 * Get system load average statistics.
     399 *
     400 * Not supported. Always returns -1.
     401 *
     402 * @param loadavg Array where the load averages shall be placed.
     403 * @param nelem Maximum number of elements to be placed into the array.
     404 * @return Number of elements placed into the array on success, -1 otherwise.
    348405 */
    349406int bsd_getloadavg(double loadavg[], int nelem)
Note: See TracChangeset for help on using the changeset viewer.