Changeset 75c430e3 in mainline


Ignore:
Timestamp:
2018-06-19T11:48:03Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
47be512
Parents:
6c440362
git-author:
Jiri Svoboda <jiri@…> (2018-06-18 17:47:07)
git-committer:
Jiri Svoboda <jiri@…> (2018-06-19 11:48:03)
Message:

Bsearch, getenv, system need to go to libc. (although we might eventually want different implementation for POSIX mode.)

Location:
uspace/lib
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/Makefile

    r6c440362 r75c430e3  
    163163        generic/stats.c \
    164164        generic/assert.c \
     165        generic/bsearch.c \
    165166        generic/pio_trace.c \
    166167        generic/qsort.c \
  • uspace/lib/c/generic/stdlib.c

    r6c440362 r75c430e3  
    166166}
    167167
     168/** Get environment list entry.
     169 *
     170 * Note that this function is not reentrant. The returned string is only
     171 * guaranteed to be valid until the next call to @c getenv.
     172 *
     173 * @param name Entry name
     174 * @return Pointer to string or @c NULL if not found
     175 */
     176char *getenv(const char *name)
     177{
     178        (void) name;
     179        return NULL;
     180}
     181
     182/** Execute command.
     183 *
     184 * @param string Command to execute or @c NULL
     185 *
     186 * @return If @a string is @c NULL, return zero (no command processor
     187 *         available). If @a string is not @c NULL, return 1 (failure).
     188 */
     189int system(const char *string)
     190{
     191        if (string == NULL)
     192                return 0;
     193
     194        return 1;
     195}
    168196
    169197/** Compute quotient and remainder of int division.
  • uspace/lib/c/include/stdlib.h

    r6c440362 r75c430e3  
    3838#include <_bits/size_t.h>
    3939#include <_bits/wchar_t.h>
     40#include <bsearch.h>
    4041#include <malloc.h>
    4142#include <qsort.h>
     
    7475
    7576extern int rand(void);
    76 extern void srand(unsigned int seed);
     77extern void srand(unsigned int);
    7778
    7879extern void abort(void) __attribute__((noreturn));
     
    8283extern int at_quick_exit(void (*)(void));
    8384extern void quick_exit(int);
     85
     86extern char *getenv(const char *);
     87extern int system(const char *);
    8488
    8589extern int atoi(const char *);
  • uspace/lib/c/test/stdlib.c

    r6c440362 r75c430e3  
    282282}
    283283
     284/** getenv function */
     285PCUT_TEST(getenv)
     286{
     287        char *s;
     288
     289        s = getenv("FOO");
     290        PCUT_ASSERT_NULL(s);
     291}
     292
     293/** Test availability of command processor */
     294PCUT_TEST(system_null)
     295{
     296        int rc;
     297
     298        rc = system(NULL);
     299        PCUT_ASSERT_INT_EQUALS(0, rc);
     300}
     301
     302/** Test running a command */
     303PCUT_TEST(system_cmd)
     304{
     305        int rc;
     306
     307        /* This should fail as system is just a stub */
     308        rc = system("/app/bdsh");
     309        PCUT_ASSERT_INT_EQUALS(1, rc);
     310}
     311
     312/** Comparison function for bsearch test */
     313static int test_compar(const void *a, const void *b)
     314{
     315        const int *ia, *ib;
     316
     317        ia = (const int *)a;
     318        ib = (const int *)b;
     319
     320        return *ia - *ib;
     321}
     322
     323PCUT_TEST(bsearch)
     324{
     325        int numbers[] = { 1, 2, 6, 7, 7, 10, 100, 120 };
     326        int k;
     327        void *r;
     328
     329        k = 0;
     330        r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
     331            test_compar);
     332        PCUT_ASSERT_NULL(r);
     333
     334        k = 1;
     335        r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
     336            test_compar);
     337        PCUT_ASSERT_NOT_NULL(r);
     338        PCUT_ASSERT_INT_EQUALS(1, *(int *)r);
     339
     340        k = 3;
     341        r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
     342            test_compar);
     343        PCUT_ASSERT_NULL(r);
     344
     345        k = 6;
     346        r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
     347            test_compar);
     348        PCUT_ASSERT_NOT_NULL(r);
     349        PCUT_ASSERT_INT_EQUALS(6, *(int *)r);
     350
     351        k = 7;
     352        r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
     353            test_compar);
     354        PCUT_ASSERT_NOT_NULL(r);
     355        PCUT_ASSERT_INT_EQUALS(7, *(int *)r);
     356
     357        k = 200;
     358        r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
     359            test_compar);
     360        PCUT_ASSERT_NULL(r);
     361}
     362
    284363/** Integer division */
    285364PCUT_TEST(div_func)
  • uspace/lib/posix/include/posix/stdlib.h

    r6c440362 r75c430e3  
    4747extern long long llabs(long long i);
    4848
    49 /* Array Functions */
    50 extern void *bsearch(const void *key, const void *base,
    51     size_t nmemb, size_t size, int (*compar)(const void *, const void *));
    52 
    5349/* Environment Access */
    54 extern char *getenv(const char *name);
    5550extern int putenv(char *string);
    56 extern int system(const char *string);
    5751
    5852/* Symbolic Links */
  • uspace/lib/posix/src/stdlib.c

    r6c440362 r75c430e3  
    8484
    8585/**
    86  * Binary search in a sorted array.
    87  *
    88  * @param key Object to search for.
    89  * @param base Pointer to the first element of the array.
    90  * @param nmemb Number of elements in the array.
    91  * @param size Size of each array element.
    92  * @param compar Comparison function.
    93  * @return Pointer to a matching element, or NULL if none can be found.
    94  */
    95 void *bsearch(const void *key, const void *base,
    96     size_t nmemb, size_t size, int (*compar)(const void *, const void *))
    97 {
    98         while (nmemb > 0) {
    99                 const void *middle = base + (nmemb / 2) * size;
    100                 int cmp = compar(key, middle);
    101                 if (cmp == 0) {
    102                         return (void *) middle;
    103                 }
    104                 if (middle == base) {
    105                         /*
    106                          * There is just one member left to check and it
    107                          * didn't match the key. Avoid infinite loop.
    108                          */
    109                         break;
    110                 }
    111                 if (cmp < 0) {
    112                         nmemb = nmemb / 2;
    113                 } else if (cmp > 0) {
    114                         nmemb = nmemb - (nmemb / 2);
    115                         base = middle;
    116                 }
    117         }
    118 
    119         return NULL;
    120 }
    121 
    122 /**
    123  * Retrieve a value of the given environment variable.
    124  *
    125  * Since HelenOS doesn't support env variables at the moment,
    126  * this function always returns NULL.
    127  *
    128  * @param name Name of the variable.
    129  * @return Value of the variable or NULL if such variable does not exist.
    130  */
    131 char *getenv(const char *name)
    132 {
    133         return NULL;
    134 }
    135 
    136 /**
    13786 *
    13887 * @param name
     
    14392{
    14493        // TODO: low priority, just a compile-time dependency of binutils
    145         not_implemented();
    146         return 0;
    147 }
    148 
    149 /**
    150  * Issue a command.
    151  *
    152  * @param string String to be passed to a command interpreter or NULL.
    153  * @return Termination status of the command if the command is not NULL,
    154  *     otherwise indicate whether there is a command interpreter (non-zero)
    155  *     or not (zero).
    156  */
    157 int system(const char *string)
    158 {
    159         // TODO: does nothing at the moment
    16094        not_implemented();
    16195        return 0;
Note: See TracChangeset for help on using the changeset viewer.