Changeset cc3652db in mainline


Ignore:
Timestamp:
2011-06-25T13:59:44Z (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:
be64a84
Parents:
2b83add
Message:

Add a few functions to stdlib.h

Location:
uspace/lib/posix
Files:
2 edited

Legend:

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

    r2b83add rcc3652db  
    8686}
    8787
     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
    88103/**
    89104 * Private helper function that serves as a compare function for qsort().
     
    117132
    118133/**
     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/**
    119170 * Retrieve a value of the given environment variable.
    120171 * Since HelenOS doesn't support env variables at the moment,
     
    139190        // TODO: low priority, just a compile-time dependency of binutils
    140191        not_implemented();
     192}
     193
     194/**
     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;
    141202}
    142203
  • uspace/lib/posix/stdlib.h

    r2b83add rcc3652db  
    5656extern long long posix_llabs(long long i);
    5757
    58 /* Array Sort Function */
     58/* Integer division */
     59
     60typedef struct {
     61        int quot, rem;
     62} posix_div_t;
     63
     64typedef struct {
     65        long quot, rem;
     66} posix_ldiv_t;
     67
     68typedef struct {
     69        long long quot, rem;
     70} posix_lldiv_t;
     71
     72extern posix_div_t posix_div(int numer, int denom);
     73extern posix_ldiv_t posix_ldiv(long numer, long denom);
     74extern posix_lldiv_t posix_lldiv(long long numer, long long denom);
     75
     76/* Array Functions */
    5977extern void posix_qsort(void *array, size_t count, size_t size,
    6078    int (*compare)(const void *, const void *));
     79extern void *posix_bsearch(const void *key, const void *base,
     80    size_t nmemb, size_t size, int (*compar)(const void *, const void *));
     81
    6182
    6283/* Environment Access */
    6384extern char *posix_getenv(const char *name);
    6485extern int posix_putenv(char *string);
     86
     87extern int posix_system(const char *string);
     88
    6589
    6690/* Symbolic Links */
     
    104128        #define llabs posix_llabs
    105129
     130        #define div_t posix_div_t
     131        #define ldiv_t posix_ldiv_t
     132        #define lldiv_t posix_lldiv_t
     133        #define div posix_div
     134        #define ldiv posix_ldiv
     135        #define lldiv posix_lldiv
     136
    106137        #define qsort posix_qsort
     138        #define bsearch posix_bsearch
    107139
    108140        #define getenv posix_getenv
    109141        #define putenv posix_putenv
     142        #define system posix_system
    110143
    111144        #define realpath posix_realpath
Note: See TracChangeset for help on using the changeset viewer.