Changeset ea4910b in mainline for uspace/lib/c/generic/string.c
- Timestamp:
- 2018-11-30T10:04:57Z (5 years ago)
- 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/string.c
rc483fca rea4910b 33 33 */ 34 34 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> 37 39 #include <string.h> 38 #define _HELENOS_SOURCE39 40 #include <stddef.h>41 #include <str_error.h>42 40 43 41 /** Copy string. … … 504 502 } 505 503 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 */ 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 duplicate 526 * @return New string or @c NULL on failure (in which case @c errno is set 527 * 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 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 */ 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 506 571 /** @} 507 572 */
Note:
See TracChangeset
for help on using the changeset viewer.