Changeset 2b3dd78 in mainline for uspace/lib/posix/src/string.c


Ignore:
Timestamp:
2018-01-31T12:02:00Z (7 years ago)
Author:
Jenda <jenda.jzqk73@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5595841
Parents:
a0a9cc2 (diff), 14d789c (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 remote-tracking branch 'upstream/master' into forwardport

change tmon includes because of new stdlib

File:
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/src/string.c

    ra0a9cc2 r2b3dd78  
    3434 */
    3535
    36 #define LIBPOSIX_INTERNAL
    37 #define __POSIX_DEF__(x) posix_##x
    38 
    3936#include "internal/common.h"
    4037#include "posix/string.h"
     
    4845#include "posix/signal.h"
    4946
     47#include "libc/str.h"
    5048#include "libc/str_error.h"
    5149
     
    6159static char *strpbrk_null(const char *s1, const char *s2)
    6260{
    63         while (!posix_strchr(s2, *s1)) {
     61        while (!strchr(s2, *s1)) {
    6462                ++s1;
    6563        }
     
    7573 * @return Pointer to the destination buffer.
    7674 */
    77 char *posix_strcpy(char *restrict dest, const char *restrict src)
    78 {
    79         posix_stpcpy(dest, src);
     75char *strcpy(char *restrict dest, const char *restrict src)
     76{
     77        stpcpy(dest, src);
    8078        return dest;
    8179}
     
    8987 * @return Pointer to the destination buffer.
    9088 */
    91 char *posix_strncpy(char *restrict dest, const char *restrict src, size_t n)
    92 {
    93         posix_stpncpy(dest, src, n);
     89char *strncpy(char *restrict dest, const char *restrict src, size_t n)
     90{
     91        stpncpy(dest, src, n);
    9492        return dest;
    9593}
     
    102100 * @return Pointer to the nul character in the destination string.
    103101 */
    104 char *posix_stpcpy(char *restrict dest, const char *restrict src)
     102char *stpcpy(char *restrict dest, const char *restrict src)
    105103{
    106104        assert(dest != NULL);
     
    128126 * @return Pointer to the first written nul character or &dest[n].
    129127 */
    130 char *posix_stpncpy(char *restrict dest, const char *restrict src, size_t n)
     128char *stpncpy(char *restrict dest, const char *restrict src, size_t n)
    131129{
    132130        assert(dest != NULL);
     
    158156 * @return Pointer to destination buffer.
    159157 */
    160 char *posix_strcat(char *restrict dest, const char *restrict src)
     158char *strcat(char *restrict dest, const char *restrict src)
    161159{
    162160        assert(dest != NULL);
    163161        assert(src != NULL);
    164162
    165         posix_strcpy(posix_strchr(dest, '\0'), src);
     163        strcpy(strchr(dest, '\0'), src);
    166164        return dest;
    167165}
     
    175173 * @return Pointer to destination buffer.
    176174 */
    177 char *posix_strncat(char *restrict dest, const char *restrict src, size_t n)
     175char *strncat(char *restrict dest, const char *restrict src, size_t n)
    178176{
    179177        assert(dest != NULL);
    180178        assert(src != NULL);
    181179
    182         char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);
     180        char *zeroptr = strncpy(strchr(dest, '\0'), src, n);
    183181        /* strncpy doesn't append the nul terminator, so we do it here */
    184182        zeroptr[n] = '\0';
     
    195193 * @return Pointer to the first byte after c in dest if found, NULL otherwise.
    196194 */
    197 void *posix_memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
     195void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
    198196{
    199197        assert(dest != NULL);
     
    221219 * @return Newly allocated copy of the string.
    222220 */
    223 char *posix_strdup(const char *s)
    224 {
    225         return posix_strndup(s, SIZE_MAX);
     221char *strdup(const char *s)
     222{
     223        return strndup(s, SIZE_MAX);
    226224}
    227225
     
    233231 * @return Newly allocated string copy of length at most n.
    234232 */
    235 char *posix_strndup(const char *s, size_t n)
     233char *strndup(const char *s, size_t n)
    236234{
    237235        assert(s != NULL);
    238236
    239         size_t len = posix_strnlen(s, n);
     237        size_t len = strnlen(s, n);
    240238        char *dup = malloc(len + 1);
    241239        if (dup == NULL) {
     
    247245
    248246        return dup;
    249 }
    250 
    251 /**
    252  * Compare bytes in memory.
    253  *
    254  * @param mem1 First area of memory to be compared.
    255  * @param mem2 Second area of memory to be compared.
    256  * @param n Maximum number of bytes to be compared.
    257  * @return Difference of the first pair of inequal bytes,
    258  *     or 0 if areas have the same content.
    259  */
    260 int posix_memcmp(const void *mem1, const void *mem2, size_t n)
    261 {
    262         assert(mem1 != NULL);
    263         assert(mem2 != NULL);
    264 
    265         const unsigned char *s1 = mem1;
    266         const unsigned char *s2 = mem2;
    267        
    268         for (size_t i = 0; i < n; ++i) {
    269                 if (s1[i] != s2[i]) {
    270                         return s1[i] - s2[i];
    271                 }
    272         }
    273        
    274         return 0;
    275247}
    276248
     
    283255 *     or 0 if strings have the same content.
    284256 */
    285 int posix_strcmp(const char *s1, const char *s2)
     257int strcmp(const char *s1, const char *s2)
    286258{
    287259        assert(s1 != NULL);
    288260        assert(s2 != NULL);
    289261
    290         return posix_strncmp(s1, s2, STR_NO_LIMIT);
     262        return strncmp(s1, s2, STR_NO_LIMIT);
    291263}
    292264
     
    300272 *     or 0 if strings have the same content.
    301273 */
    302 int posix_strncmp(const char *s1, const char *s2, size_t n)
     274int strncmp(const char *s1, const char *s2, size_t n)
    303275{
    304276        assert(s1 != NULL);
     
    326298 *     NULL pointer otherwise.
    327299 */
    328 void *posix_memchr(const void *mem, int c, size_t n)
     300void *memchr(const void *mem, int c, size_t n)
    329301{
    330302        assert(mem != NULL);
     
    348320 *     NULL pointer otherwise.
    349321 */
    350 char *posix_strchr(const char *s, int c)
     322char *strchr(const char *s, int c)
    351323{
    352324        assert(s != NULL);
     
    364336 *     NULL pointer otherwise.
    365337 */
    366 char *posix_strrchr(const char *s, int c)
     338char *strrchr(const char *s, int c)
    367339{
    368340        assert(s != NULL);
    369341       
    370         const char *ptr = posix_strchr(s, '\0');
     342        const char *ptr = strchr(s, '\0');
    371343       
    372344        /* the same as in strchr, except it loops in reverse direction */
     
    409381 *     NULL pointer otherwise.
    410382 */
    411 char *posix_strpbrk(const char *s1, const char *s2)
     383char *strpbrk(const char *s1, const char *s2)
    412384{
    413385        assert(s1 != NULL);
     
    425397 * @return Length of the prefix.
    426398 */
    427 size_t posix_strcspn(const char *s1, const char *s2)
     399size_t strcspn(const char *s1, const char *s2)
    428400{
    429401        assert(s1 != NULL);
     
    441413 * @return Length of the prefix.
    442414 */
    443 size_t posix_strspn(const char *s1, const char *s2)
     415size_t strspn(const char *s1, const char *s2)
    444416{
    445417        assert(s1 != NULL);
     
    448420        const char *ptr;
    449421        for (ptr = s1; *ptr != '\0'; ++ptr) {
    450                 if (!posix_strchr(s2, *ptr)) {
     422                if (!strchr(s2, *ptr)) {
    451423                        break;
    452424                }
     
    463435 *     not found.
    464436 */
    465 char *posix_strstr(const char *haystack, const char *needle)
     437char *strstr(const char *haystack, const char *needle)
    466438{
    467439        assert(haystack != NULL);
     
    474446       
    475447        /* Preprocess needle. */
    476         size_t nlen = posix_strlen(needle);
     448        size_t nlen = strlen(needle);
    477449        size_t prefix_table[nlen + 1];
    478450       
     
    520492 *                      exists.
    521493 */
    522 char *posix_strtok(char *s, const char *delim)
     494char *strtok(char *s, const char *delim)
    523495{
    524496        static char *next;
    525497
    526         return posix_strtok_r(s, delim, &next);
     498        return strtok_r(s, delim, &next);
    527499}
    528500
     
    540512 *                      exists.
    541513 */
    542 char *posix_strtok_r(char *s, const char *delim, char **next)
     514char *strtok_r(char *s, const char *delim, char **next)
    543515{
    544516        char *start, *end;
     
    548520
    549521        /* Skip over leading delimiters. */
    550         while (*s && (posix_strchr(delim, *s) != NULL)) ++s;
     522        while (*s && (strchr(delim, *s) != NULL)) ++s;
    551523        start = s;
    552524
    553525        /* Skip over token characters. */
    554         while (*s && (posix_strchr(delim, *s) == NULL)) ++s;
     526        while (*s && (strchr(delim, *s) == NULL)) ++s;
    555527        end = s;
    556528        *next = (*s ? s + 1 : s);
     
    575547 *     or 0 if strings have the same content.
    576548 */
    577 int posix_strcoll(const char *s1, const char *s2)
     549int strcoll(const char *s1, const char *s2)
    578550{
    579551        assert(s1 != NULL);
    580552        assert(s2 != NULL);
    581553
    582         return posix_strcmp(s1, s2);
     554        return strcmp(s1, s2);
    583555}
    584556
     
    595567 * @return Length of the transformed string.
    596568 */
    597 size_t posix_strxfrm(char *restrict s1, const char *restrict s2, size_t n)
     569size_t strxfrm(char *restrict s1, const char *restrict s2, size_t n)
    598570{
    599571        assert(s1 != NULL || n == 0);
    600572        assert(s2 != NULL);
    601573
    602         size_t len = posix_strlen(s2);
     574        size_t len = strlen(s2);
    603575
    604576        if (n > len) {
    605                 posix_strcpy(s1, s2);
     577                strcpy(s1, s2);
    606578        }
    607579
     
    615587 * @return Error message.
    616588 */
    617 char *posix_strerror(int errnum)
     589char *strerror(int errnum)
    618590{
    619591        // FIXME: move strerror() and strerror_r() to libc.
     
    629601 * @return Zero on success, errno otherwise.
    630602 */
    631 int posix_strerror_r(int errnum, char *buf, size_t bufsz)
     603int strerror_r(int errnum, char *buf, size_t bufsz)
    632604{
    633605        assert(buf != NULL);
    634606       
    635         char *errstr = posix_strerror(errnum);
    636        
    637         if (posix_strlen(errstr) + 1 > bufsz) {
     607        char *errstr = strerror(errnum);
     608       
     609        if (strlen(errstr) + 1 > bufsz) {
    638610                return ERANGE;
    639611        } else {
    640                 posix_strcpy(buf, errstr);
     612                strcpy(buf, errstr);
    641613        }
    642614
     
    650622 * @return Length of the string.
    651623 */
    652 size_t posix_strlen(const char *s)
     624size_t strlen(const char *s)
    653625{
    654626        assert(s != NULL);
    655627       
    656         return (size_t) (posix_strchr(s, '\0') - s);
     628        return (size_t) (strchr(s, '\0') - s);
    657629}
    658630
     
    664636 * @return The lower of either string length or n limit.
    665637 */
    666 size_t posix_strnlen(const char *s, size_t n)
     638size_t strnlen(const char *s, size_t n)
    667639{
    668640        assert(s != NULL);
     
    684656 * @return Human readable signal description.
    685657 */
    686 char *posix_strsignal(int signum)
     658char *strsignal(int signum)
    687659{
    688660        static const char *const sigstrings[] = {
Note: See TracChangeset for help on using the changeset viewer.