Changeset 6eb2e96 in mainline for uspace/lib/libc/generic/string.c


Ignore:
Timestamp:
2009-04-10T07:53:54Z (15 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3cc6a52
Parents:
f4b1535
Message:

str_cpy() and str_ncpy() in userspace. Nuke strcpy() and strncpy().

File:
1 edited

Legend:

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

    rf4b1535 r6eb2e96  
    463463}
    464464
    465 /** Copy NULL-terminated string.
    466  *
    467  * Copy source string @a src to destination buffer @a dst.
    468  * No more than @a size bytes are written. NULL-terminator is always
    469  * written after the last succesfully copied character (i.e. if the
    470  * destination buffer is has at least 1 byte, it will be always
    471  * NULL-terminated).
    472  *
    473  * @param src   Source string.
     465/** Copy string.
     466 *
     467 * Copy source string @a src to destination buffer @a dest.
     468 * No more than @a size bytes are written. If the size of the output buffer
     469 * is at least one byte, the output string will always be well-formed, i.e.
     470 * null-terminated and containing only complete characters.
     471 *
    474472 * @param dst   Destination buffer.
    475473 * @param count Size of the destination buffer.
    476  *
    477  */
    478 void str_ncpy(char *dst, const char *src, size_t size)
    479 {
    480         /* No space for the NULL-terminator in the buffer */
     474 * @param src   Source string.
     475 */
     476void str_cpy(char *dest, size_t size, const char *src)
     477{
     478        wchar_t ch;
     479        size_t src_off;
     480        size_t dest_off;
     481
     482        /* No space for the NULL-terminator in the buffer. */
    481483        if (size == 0)
    482484                return;
    483485       
     486        src_off = 0;
     487        dest_off = 0;
     488
     489        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
     490                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     491                        break;
     492        }
     493
     494        dest[dest_off] = '\0';
     495}
     496
     497/** Copy size-limited substring.
     498 *
     499 * Copy source string @a src to destination buffer @a dest.
     500 * No more than @a size bytes are written. If the size of the output buffer
     501 * is at least one byte, the output string will always be well-formed, i.e.
     502 * null-terminated and containing only complete characters.
     503 *
     504 * No more than @a n bytes are read from the input string, so it does not
     505 * have to be null-terminated.
     506 *
     507 * @param dst   Destination buffer.
     508 * @param count Size of the destination buffer.
     509 * @param src   Source string.
     510 */
     511void str_ncpy(char *dest, size_t size, const char *src, size_t n)
     512{
    484513        wchar_t ch;
    485         size_t str_off = 0;
    486         size_t dst_off = 0;
    487        
    488         while ((ch = str_decode(src, &str_off, STR_NO_LIMIT)) != 0) {
    489                 if (chr_encode(ch, dst, &dst_off, size) != EOK)
     514        size_t src_off;
     515        size_t dest_off;
     516
     517        /* No space for the null terminator in the buffer. */
     518        if (size == 0)
     519                return;
     520       
     521        src_off = 0;
     522        dest_off = 0;
     523
     524        while ((ch = str_decode(src, &src_off, n)) != 0) {
     525                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    490526                        break;
    491527        }
    492        
    493         if (dst_off >= size)
    494                 dst[size - 1] = 0;
    495         else
    496                 dst[dst_off] = 0;
     528
     529        dest[dest_off] = '\0';
    497530}
    498531
     
    799832}
    800833
    801 char *strcpy(char *dest, const char *src)
    802 {
    803         char *orig = dest;
    804        
    805         while ((*(dest++) = *(src++)))
    806                 ;
    807         return orig;
    808 }
    809 
    810834char *strcat(char *dest, const char *src)
    811835{
Note: See TracChangeset for help on using the changeset viewer.