source: trunk/libs/newlib/src/newlib/libc/string/stpncpy.c @ 577

Last change on this file since 577 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.6 KB
Line 
1/*
2FUNCTION
3        <<stpncpy>>---counted copy string returning a pointer to its end
4
5INDEX
6        stpncpy
7
8SYNOPSIS
9        #include <string.h>
10        char *stpncpy(char *restrict <[dst]>, const char *restrict <[src]>,
11                      size_t <[length]>);
12
13DESCRIPTION
14        <<stpncpy>> copies not more than <[length]> characters from the
15        the string pointed to by <[src]> (including the terminating
16        null character) to the array pointed to by <[dst]>.  If the
17        string pointed to by <[src]> is shorter than <[length]>
18        characters, null characters are appended to the destination
19        array until a total of <[length]> characters have been
20        written.
21
22RETURNS
23        This function returns a pointer to the end of the destination string,
24        thus pointing to the trailing '\0', or, if the destination string is
25        not null-terminated, pointing to dst + n.
26
27PORTABILITY
28<<stpncpy>> is a GNU extension, candidate for inclusion into POSIX/SUSv4.
29
30<<stpncpy>> requires no supporting OS subroutines.
31
32QUICKREF
33        stpncpy gnu
34*/
35
36#include <string.h>
37#include <limits.h>
38
39/*SUPPRESS 560*/
40/*SUPPRESS 530*/
41
42/* Nonzero if either X or Y is not aligned on a "long" boundary.  */
43#define UNALIGNED(X, Y) \
44  (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
45
46#if LONG_MAX == 2147483647L
47#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
48#else
49#if LONG_MAX == 9223372036854775807L
50/* Nonzero if X (a long int) contains a NULL byte. */
51#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
52#else
53#error long int is not a 32bit or 64bit type.
54#endif
55#endif
56
57#ifndef DETECTNULL
58#error long int is not a 32bit or 64bit byte
59#endif
60
61#define TOO_SMALL(LEN) ((LEN) < sizeof (long))
62
63char *
64stpncpy (char *__restrict dst,
65        const char *__restrict src,
66        size_t count)
67{
68  char *ret = NULL;
69
70#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
71  long *aligned_dst;
72  const long *aligned_src;
73
74  /* If SRC and DEST is aligned and count large enough, then copy words.  */
75  if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
76    {
77      aligned_dst = (long*)dst;
78      aligned_src = (long*)src;
79
80      /* SRC and DEST are both "long int" aligned, try to do "long int"
81         sized copies.  */
82      while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
83        {
84          count -= sizeof (long int);
85          *aligned_dst++ = *aligned_src++;
86        }
87
88      dst = (char*)aligned_dst;
89      src = (char*)aligned_src;
90    }
91#endif /* not PREFER_SIZE_OVER_SPEED */
92
93  while (count > 0)
94    {
95      --count;
96      if ((*dst++ = *src++) == '\0')
97        {
98          ret = dst - 1;
99          break;
100        }
101    }
102
103  while (count-- > 0)
104    *dst++ = '\0';
105
106  return ret ? ret : dst;
107}
Note: See TracBrowser for help on using the repository browser.