Subversion Repositories Kolibri OS

Rev

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

  1. /* strncpy( char *, const char *, size_t )
  2.  
  3.    This file is part of the Public Domain C Library (PDCLib).
  4.    Permission is granted to use, modify, and / or redistribute at will.
  5. */
  6.  
  7. #include <string.h>
  8.  
  9. char * strncpy( char * s1, const char * s2, size_t n )
  10. {
  11.     char * rc = s1;
  12.  
  13.     while ( n && ( *s1++ = *s2++ ) )
  14.     {
  15.         /* Cannot do "n--" in the conditional as size_t is unsigned and we have
  16.            to check it again for >0 in the next loop below, so we must not risk
  17.            underflow.
  18.         */
  19.         --n;
  20.     }
  21.  
  22.     /* Checking against 1 as we missed the last --n in the loop above. */
  23.     while ( n-- > 1 )
  24.     {
  25.         *s1++ = '\0';
  26.     }
  27.  
  28.     return rc;
  29. }