Ignore:
File:
1 edited

Legend:

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

    rea4910b r9a5ae815  
    3333 */
    3434
    35 #include <errno.h>
     35/* Prevent an error from being generated */
     36#undef _HELENOS_SOURCE
     37#include <string.h>
     38#define _HELENOS_SOURCE
     39
    3640#include <stddef.h>
    37 #include <stdlib.h>
    3841#include <str_error.h>
    39 #include <string.h>
    4042
    4143/** Copy string.
     
    502504}
    503505
    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 
    571506/** @}
    572507 */
Note: See TracChangeset for help on using the changeset viewer.