Changeset 10fdd0e in mainline for uspace/lib/posix/stdlib.c


Ignore:
Timestamp:
2011-06-26T01:34:47Z (14 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f5b2522
Parents:
343d0da1 (diff), 4d41821 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix changes.

File:
1 edited

Legend:

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

    r343d0da1 r10fdd0e  
    3737
    3838#include "stdlib.h"
     39#include "libc/sort.h"
     40#include "libc/str.h"
     41#include "libc/vfs/vfs.h"
    3942#include "internal/common.h"
     43#include <errno.h>  // FIXME: use POSIX errno
    4044
    4145/**
     
    5458/**
    5559 *
     60 * @param i Input value.
     61 * @return Absolute value of the parameter.
     62 */
     63int posix_abs(int i)
     64{
     65        return i < 0 ? -i : i;
     66}
     67
     68/**
     69 *
     70 * @param i Input value.
     71 * @return Absolute value of the parameter.
     72 */
     73long 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 */
     83long long posix_llabs(long long i)
     84{
     85        return i < 0 ? -i : i;
     86}
     87
     88posix_div_t posix_div(int numer, int denom)
     89{
     90        return (posix_div_t) { .quot = numer / denom, .rem = numer % denom };
     91}
     92
     93posix_ldiv_t posix_ldiv(long numer, long denom)
     94{
     95        return (posix_ldiv_t) { .quot = numer / denom, .rem = numer % denom };
     96}
     97
     98posix_lldiv_t posix_lldiv(long long numer, long long denom)
     99{
     100        return (posix_lldiv_t) { .quot = numer / denom, .rem = numer % denom };
     101}
     102
     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 */
     112static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata)
     113{
     114        int (*compare)(const void *, const void *) = userdata;
     115        return compare(elem1, elem2);
     116}
     117
     118/**
     119 * Array sorting utilizing the quicksort algorithm.
     120 *
    56121 * @param array
    57122 * @param count
     
    59124 * @param compare
    60125 */
    61 int posix_abs(int i)
    62 {
    63         // TODO
    64         not_implemented();
    65 }
    66 
    67 /**
    68  *
    69  * @param array
    70  * @param count
    71  * @param size
    72  * @param compare
    73  */
    74126void posix_qsort(void *array, size_t count, size_t size,
    75127    int (*compare)(const void *, const void *))
    76128{
    77         // TODO
    78         not_implemented();
    79 }
    80 
    81 /**
     129        /* Implemented in libc with one extra argument. */
     130        qsort(array, count, size, sort_compare_wrapper, compare);
     131}
     132
     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 */
     143void *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
     169/**
     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.
    82173 *
    83174 * @param name
    84  * @return
     175 * @return Always NULL.
    85176 */
    86177char *posix_getenv(const char *name)
    87178{
    88         // TODO
    89         not_implemented();
     179        return NULL;
    90180}
    91181
     
    103193
    104194/**
     195 *
     196 * @param string String to be passed to a command interpreter.
     197 * @return
     198 */
     199int posix_system(const char *string) {
     200        // TODO: does nothing at the moment
     201        return 0;
     202}
     203
     204/**
    105205 *
    106206 * @param name
     
    110210char *posix_realpath(const char *name, char *resolved)
    111211{
    112         // TODO
    113         not_implemented();
     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        }
    114248}
    115249
     
    119253 *
    120254 * @param nptr
    121  * @param endptr
    122  * @return
    123  */
    124 float posix_strtof(const char *restrict nptr, char **restrict endptr)
    125 {
    126         return (float) posix_strtold(nptr, endptr);
     255 * @return
     256 */
     257double posix_atof(const char *nptr)
     258{
     259        return posix_strtod(nptr, NULL);
    127260}
    128261
     
    135268 * @return
    136269 */
     270float posix_strtof(const char *restrict nptr, char **restrict endptr)
     271{
     272        return (float) posix_strtold(nptr, endptr);
     273}
     274
     275/**
     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
     281 * @return
     282 */
    137283double posix_strtod(const char *restrict nptr, char **restrict endptr)
    138284{
    139285        return (double) posix_strtold(nptr, endptr);
    140 }
    141 
    142 /**
    143  *
    144  * @param str
    145  * @return
    146  */
    147 int posix_atoi(const char *str)
    148 {
    149         // TODO
    150         not_implemented();
    151286}
    152287
Note: See TracChangeset for help on using the changeset viewer.