Changes in uspace/lib/c/generic/string.c [ea4910b:9a5ae815] in mainline
- File:
-
- 1 edited
-
uspace/lib/c/generic/string.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/string.c
rea4910b r9a5ae815 33 33 */ 34 34 35 #include <errno.h> 35 /* Prevent an error from being generated */ 36 #undef _HELENOS_SOURCE 37 #include <string.h> 38 #define _HELENOS_SOURCE 39 36 40 #include <stddef.h> 37 #include <stdlib.h>38 41 #include <str_error.h> 39 #include <string.h>40 42 41 43 /** Copy string. … … 502 504 } 503 505 504 /** Return number of characters in string with length limit.505 *506 * @param s String507 * @param maxlen Maximum number of characters to read508 * @return Number of characters preceding the null character, at most @a maxlen.509 */510 size_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 duplicate526 * @return New string or @c NULL on failure (in which case @c errno is set527 * to ENOMEM).528 */529 char *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 duplicate551 * @param n Maximum number of characters to copy552 * @return New string or @c NULL on failure (in which case @c errno is set553 * to ENOMEM).554 */555 char *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 571 506 /** @} 572 507 */
Note:
See TracChangeset
for help on using the changeset viewer.
