Changeset 2b3dd78 in mainline for uspace/lib/posix/src/string.c
- Timestamp:
- 2018-01-31T12:02:00Z (7 years ago)
- 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. - File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/src/string.c
ra0a9cc2 r2b3dd78 34 34 */ 35 35 36 #define LIBPOSIX_INTERNAL37 #define __POSIX_DEF__(x) posix_##x38 39 36 #include "internal/common.h" 40 37 #include "posix/string.h" … … 48 45 #include "posix/signal.h" 49 46 47 #include "libc/str.h" 50 48 #include "libc/str_error.h" 51 49 … … 61 59 static char *strpbrk_null(const char *s1, const char *s2) 62 60 { 63 while (! posix_strchr(s2, *s1)) {61 while (!strchr(s2, *s1)) { 64 62 ++s1; 65 63 } … … 75 73 * @return Pointer to the destination buffer. 76 74 */ 77 char * posix_strcpy(char *restrict dest, const char *restrict src)78 { 79 posix_stpcpy(dest, src);75 char *strcpy(char *restrict dest, const char *restrict src) 76 { 77 stpcpy(dest, src); 80 78 return dest; 81 79 } … … 89 87 * @return Pointer to the destination buffer. 90 88 */ 91 char * posix_strncpy(char *restrict dest, const char *restrict src, size_t n)92 { 93 posix_stpncpy(dest, src, n);89 char *strncpy(char *restrict dest, const char *restrict src, size_t n) 90 { 91 stpncpy(dest, src, n); 94 92 return dest; 95 93 } … … 102 100 * @return Pointer to the nul character in the destination string. 103 101 */ 104 char * posix_stpcpy(char *restrict dest, const char *restrict src)102 char *stpcpy(char *restrict dest, const char *restrict src) 105 103 { 106 104 assert(dest != NULL); … … 128 126 * @return Pointer to the first written nul character or &dest[n]. 129 127 */ 130 char * posix_stpncpy(char *restrict dest, const char *restrict src, size_t n)128 char *stpncpy(char *restrict dest, const char *restrict src, size_t n) 131 129 { 132 130 assert(dest != NULL); … … 158 156 * @return Pointer to destination buffer. 159 157 */ 160 char * posix_strcat(char *restrict dest, const char *restrict src)158 char *strcat(char *restrict dest, const char *restrict src) 161 159 { 162 160 assert(dest != NULL); 163 161 assert(src != NULL); 164 162 165 posix_strcpy(posix_strchr(dest, '\0'), src);163 strcpy(strchr(dest, '\0'), src); 166 164 return dest; 167 165 } … … 175 173 * @return Pointer to destination buffer. 176 174 */ 177 char * posix_strncat(char *restrict dest, const char *restrict src, size_t n)175 char *strncat(char *restrict dest, const char *restrict src, size_t n) 178 176 { 179 177 assert(dest != NULL); 180 178 assert(src != NULL); 181 179 182 char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);180 char *zeroptr = strncpy(strchr(dest, '\0'), src, n); 183 181 /* strncpy doesn't append the nul terminator, so we do it here */ 184 182 zeroptr[n] = '\0'; … … 195 193 * @return Pointer to the first byte after c in dest if found, NULL otherwise. 196 194 */ 197 void * posix_memccpy(void *restrict dest, const void *restrict src, int c, size_t n)195 void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n) 198 196 { 199 197 assert(dest != NULL); … … 221 219 * @return Newly allocated copy of the string. 222 220 */ 223 char * posix_strdup(const char *s)224 { 225 return posix_strndup(s, SIZE_MAX);221 char *strdup(const char *s) 222 { 223 return strndup(s, SIZE_MAX); 226 224 } 227 225 … … 233 231 * @return Newly allocated string copy of length at most n. 234 232 */ 235 char * posix_strndup(const char *s, size_t n)233 char *strndup(const char *s, size_t n) 236 234 { 237 235 assert(s != NULL); 238 236 239 size_t len = posix_strnlen(s, n);237 size_t len = strnlen(s, n); 240 238 char *dup = malloc(len + 1); 241 239 if (dup == NULL) { … … 247 245 248 246 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;275 247 } 276 248 … … 283 255 * or 0 if strings have the same content. 284 256 */ 285 int posix_strcmp(const char *s1, const char *s2)257 int strcmp(const char *s1, const char *s2) 286 258 { 287 259 assert(s1 != NULL); 288 260 assert(s2 != NULL); 289 261 290 return posix_strncmp(s1, s2, STR_NO_LIMIT);262 return strncmp(s1, s2, STR_NO_LIMIT); 291 263 } 292 264 … … 300 272 * or 0 if strings have the same content. 301 273 */ 302 int posix_strncmp(const char *s1, const char *s2, size_t n)274 int strncmp(const char *s1, const char *s2, size_t n) 303 275 { 304 276 assert(s1 != NULL); … … 326 298 * NULL pointer otherwise. 327 299 */ 328 void * posix_memchr(const void *mem, int c, size_t n)300 void *memchr(const void *mem, int c, size_t n) 329 301 { 330 302 assert(mem != NULL); … … 348 320 * NULL pointer otherwise. 349 321 */ 350 char * posix_strchr(const char *s, int c)322 char *strchr(const char *s, int c) 351 323 { 352 324 assert(s != NULL); … … 364 336 * NULL pointer otherwise. 365 337 */ 366 char * posix_strrchr(const char *s, int c)338 char *strrchr(const char *s, int c) 367 339 { 368 340 assert(s != NULL); 369 341 370 const char *ptr = posix_strchr(s, '\0');342 const char *ptr = strchr(s, '\0'); 371 343 372 344 /* the same as in strchr, except it loops in reverse direction */ … … 409 381 * NULL pointer otherwise. 410 382 */ 411 char * posix_strpbrk(const char *s1, const char *s2)383 char *strpbrk(const char *s1, const char *s2) 412 384 { 413 385 assert(s1 != NULL); … … 425 397 * @return Length of the prefix. 426 398 */ 427 size_t posix_strcspn(const char *s1, const char *s2)399 size_t strcspn(const char *s1, const char *s2) 428 400 { 429 401 assert(s1 != NULL); … … 441 413 * @return Length of the prefix. 442 414 */ 443 size_t posix_strspn(const char *s1, const char *s2)415 size_t strspn(const char *s1, const char *s2) 444 416 { 445 417 assert(s1 != NULL); … … 448 420 const char *ptr; 449 421 for (ptr = s1; *ptr != '\0'; ++ptr) { 450 if (! posix_strchr(s2, *ptr)) {422 if (!strchr(s2, *ptr)) { 451 423 break; 452 424 } … … 463 435 * not found. 464 436 */ 465 char * posix_strstr(const char *haystack, const char *needle)437 char *strstr(const char *haystack, const char *needle) 466 438 { 467 439 assert(haystack != NULL); … … 474 446 475 447 /* Preprocess needle. */ 476 size_t nlen = posix_strlen(needle);448 size_t nlen = strlen(needle); 477 449 size_t prefix_table[nlen + 1]; 478 450 … … 520 492 * exists. 521 493 */ 522 char * posix_strtok(char *s, const char *delim)494 char *strtok(char *s, const char *delim) 523 495 { 524 496 static char *next; 525 497 526 return posix_strtok_r(s, delim, &next);498 return strtok_r(s, delim, &next); 527 499 } 528 500 … … 540 512 * exists. 541 513 */ 542 char * posix_strtok_r(char *s, const char *delim, char **next)514 char *strtok_r(char *s, const char *delim, char **next) 543 515 { 544 516 char *start, *end; … … 548 520 549 521 /* Skip over leading delimiters. */ 550 while (*s && ( posix_strchr(delim, *s) != NULL)) ++s;522 while (*s && (strchr(delim, *s) != NULL)) ++s; 551 523 start = s; 552 524 553 525 /* Skip over token characters. */ 554 while (*s && ( posix_strchr(delim, *s) == NULL)) ++s;526 while (*s && (strchr(delim, *s) == NULL)) ++s; 555 527 end = s; 556 528 *next = (*s ? s + 1 : s); … … 575 547 * or 0 if strings have the same content. 576 548 */ 577 int posix_strcoll(const char *s1, const char *s2)549 int strcoll(const char *s1, const char *s2) 578 550 { 579 551 assert(s1 != NULL); 580 552 assert(s2 != NULL); 581 553 582 return posix_strcmp(s1, s2);554 return strcmp(s1, s2); 583 555 } 584 556 … … 595 567 * @return Length of the transformed string. 596 568 */ 597 size_t posix_strxfrm(char *restrict s1, const char *restrict s2, size_t n)569 size_t strxfrm(char *restrict s1, const char *restrict s2, size_t n) 598 570 { 599 571 assert(s1 != NULL || n == 0); 600 572 assert(s2 != NULL); 601 573 602 size_t len = posix_strlen(s2);574 size_t len = strlen(s2); 603 575 604 576 if (n > len) { 605 posix_strcpy(s1, s2);577 strcpy(s1, s2); 606 578 } 607 579 … … 615 587 * @return Error message. 616 588 */ 617 char * posix_strerror(int errnum)589 char *strerror(int errnum) 618 590 { 619 591 // FIXME: move strerror() and strerror_r() to libc. … … 629 601 * @return Zero on success, errno otherwise. 630 602 */ 631 int posix_strerror_r(int errnum, char *buf, size_t bufsz)603 int strerror_r(int errnum, char *buf, size_t bufsz) 632 604 { 633 605 assert(buf != NULL); 634 606 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) { 638 610 return ERANGE; 639 611 } else { 640 posix_strcpy(buf, errstr);612 strcpy(buf, errstr); 641 613 } 642 614 … … 650 622 * @return Length of the string. 651 623 */ 652 size_t posix_strlen(const char *s)624 size_t strlen(const char *s) 653 625 { 654 626 assert(s != NULL); 655 627 656 return (size_t) ( posix_strchr(s, '\0') - s);628 return (size_t) (strchr(s, '\0') - s); 657 629 } 658 630 … … 664 636 * @return The lower of either string length or n limit. 665 637 */ 666 size_t posix_strnlen(const char *s, size_t n)638 size_t strnlen(const char *s, size_t n) 667 639 { 668 640 assert(s != NULL); … … 684 656 * @return Human readable signal description. 685 657 */ 686 char * posix_strsignal(int signum)658 char *strsignal(int signum) 687 659 { 688 660 static const char *const sigstrings[] = {
Note:
See TracChangeset
for help on using the changeset viewer.