Changeset ea4910b in mainline for uspace/lib/c/generic/string.c


Ignore:
Timestamp:
2018-11-30T10:04:57Z (5 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
26f5bdf, 4aba581
Parents:
c483fca
git-author:
Jiri Svoboda <jiri@…> (2018-11-29 18:02:24)
git-committer:
Jiri Svoboda <jiri@…> (2018-11-30 10:04:57)
Message:

strdup(), strndup(), strnlen() are commonly used extensions so move them to libc (native ports can use these).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/string.c

    rc483fca rea4910b  
    3333 */
    3434
    35 /* Prevent an error from being generated */
    36 #undef _HELENOS_SOURCE
     35#include <errno.h>
     36#include <stddef.h>
     37#include <stdlib.h>
     38#include <str_error.h>
    3739#include <string.h>
    38 #define _HELENOS_SOURCE
    39 
    40 #include <stddef.h>
    41 #include <str_error.h>
    4240
    4341/** Copy string.
     
    504502}
    505503
     504/** Return number of characters in string with length limit.
     505 *
     506 * @param s String
     507 * @param maxlen Maximum number of characters to read
     508 * @return Number of characters preceding the null character, at most @a maxlen.
     509 */
     510size_t strnlen(const char *s, size_t maxlen)
     511{
     512        size_t n;
     513
     514        n = 0;
     515        while (n < maxlen && *s != '\0') {
     516                ++s;
     517                ++n;
     518        }
     519
     520        return n;
     521}
     522
     523/** Allocate a new duplicate of string.
     524 *
     525 * @param s String to duplicate
     526 * @return New string or @c NULL on failure (in which case @c errno is set
     527 *         to ENOMEM).
     528 */
     529char *strdup(const char *s)
     530{
     531        size_t sz;
     532        char *dup;
     533
     534        sz = strlen(s);
     535        dup = malloc(sz + 1);
     536        if (dup == NULL) {
     537                errno = ENOMEM;
     538                return NULL;
     539        }
     540
     541        strcpy(dup, s);
     542        return dup;
     543}
     544
     545/** Allocate a new duplicate of string with length limit.
     546 *
     547 * Creates a new duplicate of @a s. If @a s is longer than @a n characters,
     548 * only @a n characters are copied and a null character is appended.
     549 *
     550 * @param s String to duplicate
     551 * @param n Maximum number of characters to copy
     552 * @return New string or @c NULL on failure (in which case @c errno is set
     553 *         to ENOMEM).
     554 */
     555char *strndup(const char *s, size_t n)
     556{
     557        size_t sz;
     558        char *dup;
     559
     560        sz = strnlen(s, n);
     561        dup = malloc(sz + 1);
     562        if (dup == NULL) {
     563                errno = ENOMEM;
     564                return NULL;
     565        }
     566
     567        strcpy(dup, s);
     568        return dup;
     569}
     570
    506571/** @}
    507572 */
Note: See TracChangeset for help on using the changeset viewer.