Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. #include <reent.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *
  6. _DEFUN (_strndup_r, (reent_ptr, str, n),
  7.         struct _reent *reent_ptr  _AND
  8.         _CONST char   *str _AND
  9.         size_t n)
  10. {
  11.   _CONST char *ptr = str;
  12.   size_t len;
  13.   char *copy;
  14.  
  15.   while (n-- > 0 && *ptr)
  16.     ptr++;
  17.  
  18.   len = ptr - str;
  19.  
  20.   copy = _malloc_r (reent_ptr, len + 1);
  21.   if (copy)
  22.     {
  23.       memcpy (copy, str, len);
  24.       copy[len] = '\0';
  25.     }
  26.   return copy;
  27. }
  28.