Changeset 2b83add in mainline


Ignore:
Timestamp:
2011-06-25T12:41:40Z (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:
cc3652db
Parents:
458d356
Message:

Add string to int conversion functions and implement some in stdlib.h

Location:
uspace/lib/posix
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/Makefile

    r458d356 r2b83add  
    4444        stdio.c \
    4545        stdlib.c \
     46        stdlib/strtol.c \
    4647        stdlib/strtold.c \
    4748        string.c \
  • uspace/lib/posix/stdlib.c

    r458d356 r2b83add  
    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
     88/**
     89 * Private helper function that serves as a compare function for qsort().
     90 *
     91 * @param elem1 First element to compare.
     92 * @param elem2 Second element to compare.
     93 * @param compare Comparison function without userdata parameter.
     94 *
     95 * @return Relative ordering of the elements.
     96 */
     97static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata)
     98{
     99        int (*compare)(const void *, const void *) = userdata;
     100        return compare(elem1, elem2);
     101}
     102
     103/**
     104 * Array sorting utilizing the quicksort algorithm.
     105 *
    56106 * @param array
    57107 * @param count
     
    59109 * @param compare
    60110 */
    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  */
    74111void posix_qsort(void *array, size_t count, size_t size,
    75112    int (*compare)(const void *, const void *))
    76113{
    77         // TODO
    78         not_implemented();
    79 }
    80 
    81 /**
     114        /* Implemented in libc with one extra argument. */
     115        qsort(array, count, size, sort_compare_wrapper, compare);
     116}
     117
     118/**
     119 * Retrieve a value of the given environment variable.
     120 * Since HelenOS doesn't support env variables at the moment,
     121 * this function always returns NULL.
    82122 *
    83123 * @param name
    84  * @return
     124 * @return Always NULL.
    85125 */
    86126char *posix_getenv(const char *name)
    87127{
    88         // TODO
    89         not_implemented();
     128        return NULL;
    90129}
    91130
     
    110149char *posix_realpath(const char *name, char *resolved)
    111150{
    112         // TODO
    113         not_implemented();
     151        #ifndef PATH_MAX
     152                assert(resolved == NULL);
     153        #endif
     154       
     155        if (name == NULL) {
     156                errno = EINVAL;
     157                return NULL;
     158        }
     159       
     160        // TODO: symlink resolution
     161       
     162        /* Function absolutize is implemented in libc and declared in vfs.h.
     163         * No more processing is required as HelenOS doesn't have symlinks
     164         * so far (as far as I can tell), although this function will need
     165         * to be updated when that support is implemented.
     166         */
     167        char* absolute = absolutize(name, NULL);
     168       
     169        if (absolute == NULL) {
     170                /* POSIX requires some specific errnos to be set
     171                 * for some cases, but there is no way to find out from
     172                 * absolutize().
     173                 */
     174                errno = EINVAL;
     175                return NULL;
     176        }
     177       
     178        if (resolved == NULL) {
     179                return absolute;
     180        } else {
     181                #ifdef PATH_MAX
     182                        str_cpy(resolved, PATH_MAX, absolute);
     183                #endif
     184                free(absolute);
     185                return resolved;
     186        }
    114187}
    115188
     
    119192 *
    120193 * @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);
     194 * @return
     195 */
     196double posix_atof(const char *nptr)
     197{
     198        return posix_strtod(nptr, NULL);
    127199}
    128200
     
    135207 * @return
    136208 */
     209float posix_strtof(const char *restrict nptr, char **restrict endptr)
     210{
     211        return (float) posix_strtold(nptr, endptr);
     212}
     213
     214/**
     215 * Converts a string representation of a floating-point number to
     216 * its native representation. See posix_strtold().
     217 *
     218 * @param nptr
     219 * @param endptr
     220 * @return
     221 */
    137222double posix_strtod(const char *restrict nptr, char **restrict endptr)
    138223{
    139224        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();
    151225}
    152226
  • uspace/lib/posix/stdlib.h

    r458d356 r2b83add  
    5353/* Absolute Value */
    5454extern int posix_abs(int i);
     55extern long posix_labs(long i);
     56extern long long posix_llabs(long long i);
    5557
    5658/* Array Sort Function */
     
    6668
    6769/* Floating Point Conversion */
     70extern double posix_atof(const char *nptr);
    6871extern float posix_strtof(const char *restrict nptr, char **restrict endptr);
    6972extern double posix_strtod(const char *restrict nptr, char **restrict endptr);
     
    7174
    7275/* Integer Conversion */
    73 extern int posix_atoi(const char *str);
     76extern int posix_atoi(const char *nptr);
     77extern long int posix_atol(const char *nptr);
     78extern long long int posix_atoll(const char *nptr);
     79
     80extern long int posix_strtol(const char *restrict nptr,
     81    char **restrict endptr, int base);
     82extern long long int posix_strtoll(const char *restrict nptr,
     83    char **restrict endptr, int base);
     84extern unsigned long int posix_strtoul(const char *restrict nptr,
     85    char **restrict endptr, int base);
     86extern unsigned long long int posix_strtoull(
     87    const char *restrict nptr, char **restrict endptr, int base);
     88
    7489
    7590/* Memory Allocation */
     
    86101
    87102        #define abs posix_abs
     103        #define labs posix_labs
     104        #define llabs posix_llabs
    88105
    89106        #define qsort posix_qsort
    90107
    91108        #define getenv posix_getenv
     109        #define putenv posix_putenv
    92110
    93111        #define realpath posix_realpath
    94112       
     113        #define atof posix_atof
    95114        #define strtof posix_strtof
    96115        #define strtod posix_strtod
     
    98117       
    99118        #define atoi posix_atoi
     119        #define atol posix_atol
     120        #define atoll posix_atoll
     121        #define strtol posix_strtol
     122        #define strtoll posix_strtoll
     123        #define strtoul posix_strtoul
     124        #define strtoull posix_strtoull
    100125
    101126        #define malloc posix_malloc
Note: See TracChangeset for help on using the changeset viewer.