1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /* 01/02/2003 Port to LTP avenkat@us.ibm.com */
21 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
22
23 /*
24 * NAME
25 * memcpy -- test memcpy
26 *
27 * CALLS
28 * memcpy1(3)
29 *
30 * ALGORITHM
31 * There are 4 cases for copies: S = Source, D = Destination
32 *
33 * 1 - S < D no overlap
34 * 2 - D < S no overlap
35 * 3 - S < D with overlap
36 * 4 - D < S with overlap
37 *
38 * We try all four cases. Check buffer boundaries.
39 *
40 * RESTRICTIONS
41 */
42
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <errno.h>
48
49 /***** LTP Port *****/
50 #include "test.h"
51
52 char *TCID = "memcpy1";
53 /***** ** ** *****/
54 #undef BSIZE
55 #define BSIZE 4096
56 #define LEN 100
57 #define FAILED 0
58 #define PASSED 1
59
60 /***** LTP Port *****/
61 int local_flag = PASSED;
62 int block_number;
63 FILE *temp;
64 int TST_TOTAL = 1;
65 /***** ** ** *****/
66 char buf[BSIZE];
67
68 /***** LTP Port *****/
69
70 int anyfail();
71 int blenter();
72 int blexit();
73
74 void setup();
75 /***** ** ** *****/
76 void clearit();
77 void fill(char *str);
78 int checkit(char *str);
79
80 /*--------------------------------------------------------------*/
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 char *p, *q;
84
85 setup(); /* temp file is now open */
86 /*--------------------------------------------------------------*/
87 blenter();
88
89 clearit();
90
91 p = &buf[100];
92
93 fill(p);
94 q = &buf[800];
95 memcpy(q, p, LEN);
96
97 if (checkit(q)) {
98 fprintf(temp, "\tcopy failed - missed data\n");
99 local_flag = FAILED;
100 }
101
102 if (p[-1] || p[LEN]) {
103 fprintf(temp, "\tcopy failed - 'to' bounds\n");
104 local_flag = FAILED;
105 }
106
107 if (q[-1] || q[LEN]) {
108 fprintf(temp, "\tcopy failed - 'from' bounds\n");
109 local_flag = FAILED;
110 }
111
112 blexit();
113 /*--------------------------------------------------------------*/
114 blenter();
115
116 clearit();
117
118 p = &buf[800];
119
120 fill(p);
121 q = &buf[100];
122 memcpy(q, p, LEN);
123
124 if (checkit(q)) {
125 fprintf(temp, "\tcopy failed - missed data\n");
126 local_flag = FAILED;
127 }
128
129 if (p[-1] || p[LEN]) {
130 fprintf(temp, "\tcopy failed - 'to' bounds\n");
131 local_flag = FAILED;
132 }
133
134 if (q[-1] || q[LEN]) {
135 fprintf(temp, "\tcopy failed - 'from' bounds\n");
136 local_flag = FAILED;
137 }
138
139 blexit();
140 /*--------------------------------------------------------------*/
141 blenter();
142
143 clearit();
144
145 p = &buf[800];
146
147 fill(p);
148 q = &buf[850];
149 memcpy(q, p, LEN);
150
151 if (checkit(q)) {
152 fprintf(temp, "\tcopy failed - missed data\n");
153 local_flag = FAILED;
154 }
155
156 if (p[-1]) {
157 fprintf(temp, "\tcopy failed - 'to' bounds\n");
158 local_flag = FAILED;
159 }
160
161 if (q[LEN]) {
162 fprintf(temp, "\tcopy failed - 'from' bounds\n");
163 local_flag = FAILED;
164 }
165
166 blexit();
167 /*--------------------------------------------------------------*/
168 blenter();
169
170 clearit();
171
172 p = &buf[850];
173
174 fill(p);
175 q = &buf[800];
176 memcpy(q, p, LEN);
177
178 if (checkit(q)) {
179 fprintf(temp, "\tcopy failed - missed data\n");
180 local_flag = FAILED;
181 }
182
183 if (p[LEN]) {
184 fprintf(temp, "\tcopy failed - 'to' bounds\n");
185 local_flag = FAILED;
186 }
187
188 if (q[-1]) {
189 fprintf(temp, "\tcopy failed - 'from' bounds\n");
190 local_flag = FAILED;
191 }
192
193 blexit();
194 /*--------------------------------------------------------------*/
195 /* Clean up any files created by test before call to anyfail. */
196
197 anyfail(); /* THIS CALL DOES NOT RETURN - EXITS!! */
198 tst_exit();
199 }
200
201 /*--------------------------------------------------------------*/
202 /* FUNCTIONS GO HERE */
203
clearit(void)204 void clearit(void)
205 {
206 register int i;
207
208 for (i = 0; i < BSIZE; i++)
209 buf[i] = 0;
210 }
211
fill(char * str)212 void fill(char *str)
213 {
214 register int i;
215 for (i = 0; i < LEN; i++)
216 *str++ = 'a';
217 }
218
checkit(char * str)219 int checkit(char *str)
220 {
221 register int i;
222 for (i = 0; i < LEN; i++)
223 if (*str++ != 'a')
224 return (-1);
225
226 return (0);
227 }
228
anyfail(void)229 int anyfail(void)
230 {
231 (local_flag == FAILED) ? tst_resm(TFAIL,
232 "Test failed") : tst_resm(TPASS,
233 "Test passed");
234 tst_exit();
235 }
236
setup(void)237 void setup(void)
238 {
239 temp = stderr;
240 }
241
blenter(void)242 int blenter(void)
243 {
244 local_flag = PASSED;
245 return 0;
246 }
247
blexit(void)248 int blexit(void)
249 {
250 (local_flag == FAILED) ? tst_resm(TFAIL,
251 "Test failed") : tst_resm(TPASS,
252 "Test passed");
253 return 0;
254 }
255