1 /* Copyright (C) 2000 Free Software Foundation.
2
3 Ensure all expected transformations of builtin strncpy occur and
4 perform correctly.
5
6 Written by Kaveh R. Ghazi, 11/25/2000. */
7
8 extern void abort (void);
9 typedef __SIZE_TYPE__ size_t;
10 extern char *strncpy (char *, const char *, size_t);
11 extern int strcmp (const char *, const char *);
12 extern int strncmp (const char *, const char *, size_t);
13 extern void *memset (void *, int, size_t);
14
main()15 int main ()
16 {
17 const char *const src = "hello world";
18 const char *src2;
19 char dst[64], *dst2;
20
21 memset (dst, 0, sizeof (dst));
22 if (strncpy (dst, src, 4) != dst || strncmp (dst, src, 4))
23 abort();
24
25 memset (dst, 0, sizeof (dst));
26 if (strncpy (dst+16, src, 4) != dst+16 || strncmp (dst+16, src, 4))
27 abort();
28
29 memset (dst, 0, sizeof (dst));
30 if (strncpy (dst+32, src+5, 4) != dst+32 || strncmp (dst+32, src+5, 4))
31 abort();
32
33 memset (dst, 0, sizeof (dst));
34 dst2 = dst;
35 if (strncpy (++dst2, src+5, 4) != dst+1 || strncmp (dst2, src+5, 4)
36 || dst2 != dst+1)
37 abort();
38
39 memset (dst, 0, sizeof (dst));
40 if (strncpy (dst, src, 0) != dst || strcmp (dst, ""))
41 abort();
42
43 memset (dst, 0, sizeof (dst));
44 dst2 = dst; src2 = src;
45 if (strncpy (++dst2, ++src2, 0) != dst+1 || strcmp (dst2, "")
46 || dst2 != dst+1 || src2 != src+1)
47 abort();
48
49 memset (dst, 0, sizeof (dst));
50 dst2 = dst; src2 = src;
51 if (strncpy (++dst2+5, ++src2+5, 0) != dst+6 || strcmp (dst2+5, "")
52 || dst2 != dst+1 || src2 != src+1)
53 abort();
54
55 memset (dst, 0, sizeof (dst));
56 if (strncpy (dst, src, 12) != dst || strcmp (dst, src))
57 abort();
58
59 /* Test at least one instance of the __builtin_ style. We do this
60 to ensure that it works and that the prototype is correct. */
61 memset (dst, 0, sizeof (dst));
62 if (__builtin_strncpy (dst, src, 4) != dst || strncmp (dst, src, 4))
63 abort();
64
65 return 0;
66 }
67
68 #ifdef __OPTIMIZE__
69 /* When optimizing, all the above cases should be transformed into
70 something else. So any remaining calls to the original function
71 should abort. */
72 static char *
strncpy(char * s1,const char * s2,size_t n)73 strncpy(char *s1, const char *s2, size_t n)
74 {
75 abort();
76 }
77 #endif
78