Changeset e4f8c77 in mainline for uspace/lib/posix/stdlib.c


Ignore:
Timestamp:
2011-07-13T22:39:18Z (13 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e6910c8
Parents:
5974661 (diff), 8ecef91 (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.

File:
1 edited

Legend:

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

    r5974661 re4f8c77  
    3636#define LIBPOSIX_INTERNAL
    3737
     38#include "internal/common.h"
    3839#include "stdlib.h"
    39 #include "internal/common.h"
    40 
    41 /**
    42  *
     40
     41#include "errno.h"
     42
     43#include "libc/sort.h"
     44#include "libc/str.h"
     45#include "libc/vfs/vfs.h"
     46
     47/**
     48 *
    4349 * @param array
    4450 * @param count
     
    4652 * @param compare
    4753 */
     54int posix_atexit(void (*func)(void))
     55{
     56        // TODO: low priority, just a compile-time dependency of binutils
     57        not_implemented();
     58}
     59
     60/**
     61 *
     62 * @param i Input value.
     63 * @return Absolute value of the parameter.
     64 */
     65int posix_abs(int i)
     66{
     67        return i < 0 ? -i : i;
     68}
     69
     70/**
     71 *
     72 * @param i Input value.
     73 * @return Absolute value of the parameter.
     74 */
     75long 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 */
     85long long posix_llabs(long long i)
     86{
     87        return i < 0 ? -i : i;
     88}
     89
     90posix_div_t posix_div(int numer, int denom)
     91{
     92        return (posix_div_t) { .quot = numer / denom, .rem = numer % denom };
     93}
     94
     95posix_ldiv_t posix_ldiv(long numer, long denom)
     96{
     97        return (posix_ldiv_t) { .quot = numer / denom, .rem = numer % denom };
     98}
     99
     100posix_lldiv_t posix_lldiv(long long numer, long long denom)
     101{
     102        return (posix_lldiv_t) { .quot = numer / denom, .rem = numer % denom };
     103}
     104
     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 */
     114static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata)
     115{
     116        int (*compare)(const void *, const void *) = userdata;
     117        return compare(elem1, elem2);
     118}
     119
     120/**
     121 * Array sorting utilizing the quicksort algorithm.
     122 *
     123 * @param array
     124 * @param count
     125 * @param size
     126 * @param compare
     127 */
    48128void posix_qsort(void *array, size_t count, size_t size,
    49129    int (*compare)(const void *, const void *))
    50130{
    51         // TODO
    52         not_implemented();
    53 }
    54 
    55 /**
     131        /* Implemented in libc with one extra argument. */
     132        qsort(array, count, size, sort_compare_wrapper, compare);
     133}
     134
     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 */
     145void *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
     171/**
     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.
    56175 *
    57176 * @param name
    58  * @return
     177 * @return Always NULL.
    59178 */
    60179char *posix_getenv(const char *name)
    61180{
    62         // TODO
    63         not_implemented();
     181        return NULL;
    64182}
    65183
     
    70188 * @return
    71189 */
     190int posix_putenv(char *string)
     191{
     192        // TODO: low priority, just a compile-time dependency of binutils
     193        not_implemented();
     194}
     195
     196/**
     197 *
     198 * @param string String to be passed to a command interpreter.
     199 * @return
     200 */
     201int posix_system(const char *string) {
     202        // TODO: does nothing at the moment
     203        return 0;
     204}
     205
     206/**
     207 *
     208 * @param name
     209 * @param resolved
     210 * @return
     211 */
    72212char *posix_realpath(const char *name, char *resolved)
    73213{
    74         // TODO
    75         not_implemented();
     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        }
    76250}
    77251
     
    81255 *
    82256 * @param nptr
    83  * @param endptr
    84  * @return
    85  */
    86 float posix_strtof(const char *restrict nptr, char **restrict endptr)
    87 {
    88         return (float) posix_strtold(nptr, endptr);
     257 * @return
     258 */
     259double posix_atof(const char *nptr)
     260{
     261        return posix_strtod(nptr, NULL);
    89262}
    90263
     
    97270 * @return
    98271 */
     272float posix_strtof(const char *restrict nptr, char **restrict endptr)
     273{
     274        return (float) posix_strtold(nptr, endptr);
     275}
     276
     277/**
     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
     283 * @return
     284 */
    99285double posix_strtod(const char *restrict nptr, char **restrict endptr)
    100286{
     
    103289
    104290/**
    105  *
    106  * @param str
    107  * @return
    108  */
    109 int posix_atoi(const char *str)
    110 {
    111         // TODO
     291 *
     292 * @param size
     293 * @return
     294 */
     295void *posix_malloc(size_t size)
     296{
     297        return malloc(size);
     298}
     299
     300/**
     301 *
     302 * @param nelem
     303 * @param elsize
     304 * @return
     305 */
     306void *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 */
     317void *posix_realloc(void *ptr, size_t size)
     318{
     319        return realloc(ptr, size);
     320}
     321
     322/**
     323 *
     324 * @param ptr
     325 */
     326void posix_free(void *ptr)
     327{
     328        free(ptr);
     329}
     330
     331/**
     332 *
     333 * @param tmpl
     334 * @return
     335 */
     336char *posix_mktemp(char *tmpl)
     337{
     338        // TODO: low priority, just a compile-time dependency of binutils
    112339        not_implemented();
    113340}
    114341
     342/**
     343 * Should read system load statistics. Not supported. Always returns -1.
     344 *
     345 * @param loadavg
     346 * @param nelem
     347 * @return
     348 */
     349int bsd_getloadavg(double loadavg[], int nelem)
     350{
     351        return -1;
     352}
     353
    115354/** @}
    116355 */
Note: See TracChangeset for help on using the changeset viewer.