• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 2 cases for copies:  S = Source, D = Destination
32  *
33  *	  1 - S < D no overlap
34  *	  2 - D < S no overlap
35  *
36  *	We try both cases.  Check buffer boundaries.
37  *
38  * RESTRICTIONS
39  */
40 
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <errno.h>
46 
47 #include "test.h"
48 
49 char *TCID = "memcpy1";
50 
51 #undef  BSIZE
52 #define BSIZE	4096
53 #define LEN	100
54 #define FAILED 0
55 #define PASSED 1
56 
57 int local_flag = PASSED;
58 int block_number;
59 FILE *temp;
60 int TST_TOTAL = 1;
61 char buf[BSIZE];
62 
63 
64 int anyfail();
65 int blenter();
66 int blexit();
67 
68 void setup();
69 void clearit();
70 void fill(char *str);
71 int checkit(char *str);
72 
main(int argc,char * argv[])73 int main(int argc, char *argv[])
74 {
75 	char *p, *q;
76 
77 	tst_parse_opts(argc, argv, NULL, NULL);
78 
79 	setup();		/* temp file is now open        */
80 /*--------------------------------------------------------------*/
81 	blenter();
82 
83 	clearit();
84 
85 	p = &buf[100];
86 
87 	fill(p);
88 	q = &buf[800];
89 	memcpy(q, p, LEN);
90 
91 	if (checkit(q)) {
92 		fprintf(temp, "\tcopy failed - missed data\n");
93 		local_flag = FAILED;
94 	}
95 
96 	if (p[-1] || p[LEN]) {
97 		fprintf(temp, "\tcopy failed - 'to' bounds\n");
98 		local_flag = FAILED;
99 	}
100 
101 	if (q[-1] || q[LEN]) {
102 		fprintf(temp, "\tcopy failed - 'from' bounds\n");
103 		local_flag = FAILED;
104 	}
105 
106 	blexit();
107 /*--------------------------------------------------------------*/
108 	blenter();
109 
110 	clearit();
111 
112 	p = &buf[800];
113 
114 	fill(p);
115 	q = &buf[100];
116 	memcpy(q, p, LEN);
117 
118 	if (checkit(q)) {
119 		fprintf(temp, "\tcopy failed - missed data\n");
120 		local_flag = FAILED;
121 	}
122 
123 	if (p[-1] || p[LEN]) {
124 		fprintf(temp, "\tcopy failed - 'to' bounds\n");
125 		local_flag = FAILED;
126 	}
127 
128 	if (q[-1] || q[LEN]) {
129 		fprintf(temp, "\tcopy failed - 'from' bounds\n");
130 		local_flag = FAILED;
131 	}
132 
133 	blexit();
134 
135 	anyfail();
136 	tst_exit();
137 }
138 
clearit(void)139 void clearit(void)
140 {
141 	register int i;
142 
143 	for (i = 0; i < BSIZE; i++)
144 		buf[i] = 0;
145 }
146 
fill(char * str)147 void fill(char *str)
148 {
149 	register int i;
150 	for (i = 0; i < LEN; i++)
151 		*str++ = 'a';
152 }
153 
checkit(char * str)154 int checkit(char *str)
155 {
156 	register int i;
157 	for (i = 0; i < LEN; i++)
158 		if (*str++ != 'a')
159 			return (-1);
160 
161 	return (0);
162 }
163 
anyfail(void)164 int anyfail(void)
165 {
166 	(local_flag == FAILED) ? tst_resm(TFAIL,
167 					  "Test failed") : tst_resm(TPASS,
168 								    "Test passed");
169 	tst_exit();
170 }
171 
setup(void)172 void setup(void)
173 {
174 	temp = stderr;
175 }
176 
blenter(void)177 int blenter(void)
178 {
179 	local_flag = PASSED;
180 	return 0;
181 }
182 
blexit(void)183 int blexit(void)
184 {
185 	(local_flag == FAILED) ? tst_resm(TFAIL,
186 					  "Test failed") : tst_resm(TPASS,
187 								    "Test passed");
188 	return 0;
189 }
190