Ignore:
File:
1 edited

Legend:

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

    rec18957a r63fc519  
    3636#define LIBPOSIX_INTERNAL
    3737
     38#include "stdlib.h"
    3839#include "internal/common.h"
    39 #include "stdlib.h"
    40 
    41 #include "errno.h"
    42 
    43 #include "libc/sort.h"
    44 #include "libc/str.h"
    45 #include "libc/vfs/vfs.h"
    4640
    4741/**
    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  *
    62  * @param i Input value.
    63  * @return Absolute value of the parameter.
    64  */
    65 int 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  */
    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 
    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 
    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);
    118 }
    119 
    120 /**
    121  * Array sorting utilizing the quicksort algorithm.
    12242 *
    12343 * @param array
     
    12949    int (*compare)(const void *, const void *))
    13050{
    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  */
    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 
    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.
    175  *
    176  * @param name
    177  * @return Always NULL.
    178  */
    179 char *posix_getenv(const char *name)
    180 {
    181         return NULL;
    182 }
    183 
    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
     51        // TODO
    19352        not_implemented();
    19453}
     
    19655/**
    19756 *
    198  * @param string String to be passed to a command interpreter.
     57 * @param name
    19958 * @return
    20059 */
    201 int posix_system(const char *string) {
    202         // TODO: does nothing at the moment
    203         return 0;
     60char *posix_getenv(const char *name)
     61{
     62        // TODO
     63        not_implemented();
    20464}
    20565
     
    21272char *posix_realpath(const char *name, char *resolved)
    21373{
    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         }
    250 }
    251 
    252 /**
    253  * Converts a string representation of a floating-point number to
    254  * its native representation. See posix_strtold().
    255  *
    256  * @param nptr
    257  * @return
    258  */
    259 double posix_atof(const char *nptr)
    260 {
    261         return posix_strtod(nptr, NULL);
     74        // TODO
     75        not_implemented();
    26276}
    26377
     
    289103
    290104/**
    291  *
    292  * @param size
     105 * 
     106 * @param str
    293107 * @return
    294108 */
    295 void *posix_malloc(size_t size)
     109int posix_atoi(const char *str)
    296110{
    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
     111        // TODO
    339112        not_implemented();
    340 }
    341 
    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;
    352113}
    353114
Note: See TracChangeset for help on using the changeset viewer.