@@ -15,7 +15,7 @@
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
-/* This is almost copied from strncpy.c, written by Torbjorn Granlund. */
+#include <stddef.h>
#ifdef HAVE_CONFIG_H
# include <config.h>
@@ -41,59 +41,12 @@ weak_alias (__stpncpy, stpncpy)
char *
STPNCPY (char *dest, const char *src, size_t n)
{
- char c;
- char *s = dest;
-
- if (n >= 4)
- {
- size_t n4 = n >> 2;
-
- for (;;)
- {
- c = *src++;
- *dest++ = c;
- if (c == '\0')
- break;
- c = *src++;
- *dest++ = c;
- if (c == '\0')
- break;
- c = *src++;
- *dest++ = c;
- if (c == '\0')
- break;
- c = *src++;
- *dest++ = c;
- if (c == '\0')
- break;
- if (--n4 == 0)
- goto last_chars;
- }
- n -= dest - s;
- goto zero_fill;
- }
-
- last_chars:
- n &= 3;
- if (n == 0)
+ size_t size = __strnlen (src, n);
+ memcpy (dest, src, size);
+ dest += size;
+ if (size == n)
return dest;
-
- for (;;)
- {
- c = *src++;
- --n;
- *dest++ = c;
- if (c == '\0')
- break;
- if (n == 0)
- return dest;
- }
-
- zero_fill:
- while (n-- > 0)
- dest[n] = '\0';
-
- return dest - 1;
+ return memset (dest, '\0', n - size);
}
#ifdef weak_alias
libc_hidden_def (__stpncpy)