1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*============================================================================
6**
7** Source: test1.c
8**
9** Purpose:
10** Test to see that you can copy a portion of a string into a new buffer.
11** Also check that the strncpy function doesn't overflow when it is used.
12** Finally check that if the number of characters given is greater than the
13** amount to copy, that the destination buffer is padded with NULLs.
14**
15**
16**==========================================================================*/
17
18#include <palsuite.h>
19
20
21int __cdecl main(int argc, char *argv[])
22{
23 char dest[80];
24 char *result = "foobar";
25 char *str = "foobar\0baz";
26 char *ret;
27 int i;
28
29 if (PAL_Initialize(argc, argv))
30 {
31 return FAIL;
32 }
33
34
35 for (i=0; i<80; i++)
36 {
37 dest[i] = 'x';
38 }
39
40 ret = strncpy(dest, str, 3);
41 if (ret != dest)
42 {
43 Fail("Expected strncpy to return %p, got %p!\n", dest, ret);
44 }
45
46 if (strncmp(dest, result, 3) != 0)
47 {
48 Fail("Expected strncpy to give \"%s\", got \"%s\"!\n", result, dest);
49 }
50
51 if (dest[3] != 'x')
52 {
53 Fail("strncpy overflowed!\n");
54 }
55
56 ret = strncpy(dest, str, 40);
57 if (ret != dest)
58 {
59 Fail("Expected strncpy to return %p, got %p!\n", dest, ret);
60 }
61
62 if (strcmp(dest, result) != 0)
63 {
64 Fail("Expected strncpy to give \"%s\", got \"%s\"!\n", result, dest);
65 }
66
67 for (i=strlen(str); i<40; i++)
68 {
69 if (dest[i] != 0)
70 {
71 Fail("strncpy failed to pad the destination with NULLs!\n");
72 }
73 }
74
75 if (dest[40] != 'x')
76 {
77 Fail("strncpy overflowed!\n");
78 }
79
80
81
82 PAL_Terminate();
83
84 return PASS;
85}
86