xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/doio/dataascii.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it is
13*49cdfc7eSAndroid Build Coastguard Worker  * free of the rightful claim of any third person regarding infringement
14*49cdfc7eSAndroid Build Coastguard Worker  * or the like.  Any license provided herein, whether implied or
15*49cdfc7eSAndroid Build Coastguard Worker  * otherwise, applies only to this software file.  Patent licenses, if
16*49cdfc7eSAndroid Build Coastguard Worker  * any, provided herein do not apply to combinations of this program with
17*49cdfc7eSAndroid Build Coastguard Worker  * other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
20*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
21*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24*49cdfc7eSAndroid Build Coastguard Worker  * Mountain View, CA  94043, or:
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * http://www.sgi.com
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * For further information regarding this notice, see:
29*49cdfc7eSAndroid Build Coastguard Worker  *
30*49cdfc7eSAndroid Build Coastguard Worker  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31*49cdfc7eSAndroid Build Coastguard Worker  */
32*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include "dataascii.h"
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker #define CHARS		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjiklmnopqrstuvwxyz\n"
37*49cdfc7eSAndroid Build Coastguard Worker #define CHARS_SIZE	sizeof(CHARS)
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #ifdef UNIT_TEST
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
41*49cdfc7eSAndroid Build Coastguard Worker #endif
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker static char Errmsg[80];
44*49cdfc7eSAndroid Build Coastguard Worker 
dataasciigen(char * listofchars,char * buffer,int bsize,int offset)45*49cdfc7eSAndroid Build Coastguard Worker int dataasciigen(char *listofchars, char *buffer, int bsize, int offset)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker 	int cnt;
48*49cdfc7eSAndroid Build Coastguard Worker 	int total;
49*49cdfc7eSAndroid Build Coastguard Worker 	int ind;
50*49cdfc7eSAndroid Build Coastguard Worker 	char *chr;
51*49cdfc7eSAndroid Build Coastguard Worker 	int chars_size;
52*49cdfc7eSAndroid Build Coastguard Worker 	char *charlist;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	chr = buffer;
55*49cdfc7eSAndroid Build Coastguard Worker 	total = offset + bsize;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	if (listofchars == NULL) {
58*49cdfc7eSAndroid Build Coastguard Worker 		charlist = CHARS;
59*49cdfc7eSAndroid Build Coastguard Worker 		chars_size = CHARS_SIZE;
60*49cdfc7eSAndroid Build Coastguard Worker 	} else {
61*49cdfc7eSAndroid Build Coastguard Worker 		charlist = listofchars;
62*49cdfc7eSAndroid Build Coastguard Worker 		chars_size = strlen(listofchars);
63*49cdfc7eSAndroid Build Coastguard Worker 	}
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	for (cnt = offset; cnt < total; cnt++) {
66*49cdfc7eSAndroid Build Coastguard Worker 		ind = cnt % chars_size;
67*49cdfc7eSAndroid Build Coastguard Worker 		*chr++ = charlist[ind];
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	return bsize;
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker 
dataasciichk(char * listofchars,char * buffer,int bsize,int offset,char ** errmsg)73*49cdfc7eSAndroid Build Coastguard Worker int dataasciichk(char *listofchars, char *buffer, int bsize,
74*49cdfc7eSAndroid Build Coastguard Worker 		 int offset, char **errmsg)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker 	int cnt;
77*49cdfc7eSAndroid Build Coastguard Worker 	int total;
78*49cdfc7eSAndroid Build Coastguard Worker 	int ind;
79*49cdfc7eSAndroid Build Coastguard Worker 	char *chr;
80*49cdfc7eSAndroid Build Coastguard Worker 	int chars_size;
81*49cdfc7eSAndroid Build Coastguard Worker 	char *charlist;
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	chr = buffer;
84*49cdfc7eSAndroid Build Coastguard Worker 	total = offset + bsize;
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	if (listofchars == NULL) {
87*49cdfc7eSAndroid Build Coastguard Worker 		charlist = CHARS;
88*49cdfc7eSAndroid Build Coastguard Worker 		chars_size = CHARS_SIZE;
89*49cdfc7eSAndroid Build Coastguard Worker 	} else {
90*49cdfc7eSAndroid Build Coastguard Worker 		charlist = listofchars;
91*49cdfc7eSAndroid Build Coastguard Worker 		chars_size = strlen(listofchars);
92*49cdfc7eSAndroid Build Coastguard Worker 	}
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	if (errmsg != NULL)
95*49cdfc7eSAndroid Build Coastguard Worker 		*errmsg = Errmsg;
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	for (cnt = offset; cnt < total; chr++, cnt++) {
98*49cdfc7eSAndroid Build Coastguard Worker 		ind = cnt % chars_size;
99*49cdfc7eSAndroid Build Coastguard Worker 		if (*chr != charlist[ind]) {
100*49cdfc7eSAndroid Build Coastguard Worker 			sprintf(Errmsg,
101*49cdfc7eSAndroid Build Coastguard Worker 				"data mismatch at offset %d, exp:%#o, act:%#o",
102*49cdfc7eSAndroid Build Coastguard Worker 				cnt, charlist[ind], *chr);
103*49cdfc7eSAndroid Build Coastguard Worker 			return cnt;
104*49cdfc7eSAndroid Build Coastguard Worker 		}
105*49cdfc7eSAndroid Build Coastguard Worker 	}
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
108*49cdfc7eSAndroid Build Coastguard Worker 	return -1;
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker #if UNIT_TEST
112*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** ag)113*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **ag)
114*49cdfc7eSAndroid Build Coastguard Worker {
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	int size = 1023;
117*49cdfc7eSAndroid Build Coastguard Worker 	char *buffer;
118*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
119*49cdfc7eSAndroid Build Coastguard Worker 	char *errmsg;
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 	buffer = malloc(size);
122*49cdfc7eSAndroid Build Coastguard Worker 	if (buffer == NULL) {
123*49cdfc7eSAndroid Build Coastguard Worker 		perror("malloc");
124*49cdfc7eSAndroid Build Coastguard Worker 		exit(2);
125*49cdfc7eSAndroid Build Coastguard Worker 	}
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 	dataasciigen(NULL, buffer, size, 0);
128*49cdfc7eSAndroid Build Coastguard Worker 	printf("dataasciigen(NULL, buffer, %d, 0)\n", size);
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	ret = dataasciichk(NULL, buffer, size, 0, &errmsg);
131*49cdfc7eSAndroid Build Coastguard Worker 	printf("dataasciichk(NULL, buffer, %d, 0, &errmsg) returned %d %s\n",
132*49cdfc7eSAndroid Build Coastguard Worker 	       size, ret, errmsg);
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1)
135*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tPASS return value is -1 as expected\n");
136*49cdfc7eSAndroid Build Coastguard Worker 	else
137*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tFAIL return value is %d, expected -1\n", ret);
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker 	ret = dataasciichk(NULL, &buffer[1], size - 1, 1, &errmsg);
140*49cdfc7eSAndroid Build Coastguard Worker 	printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
141*49cdfc7eSAndroid Build Coastguard Worker 		size - 1, ret, errmsg);
142*49cdfc7eSAndroid Build Coastguard Worker 
143*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1)
144*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tPASS return value is -1 as expected\n");
145*49cdfc7eSAndroid Build Coastguard Worker 	else
146*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tFAIL return value is %d, expected -1\n", ret);
147*49cdfc7eSAndroid Build Coastguard Worker 
148*49cdfc7eSAndroid Build Coastguard Worker 	buffer[25] = 0x0;
149*49cdfc7eSAndroid Build Coastguard Worker 	printf("changing char 25\n");
150*49cdfc7eSAndroid Build Coastguard Worker 
151*49cdfc7eSAndroid Build Coastguard Worker 	ret = dataasciichk(NULL, &buffer[1], size - 1, 1, &errmsg);
152*49cdfc7eSAndroid Build Coastguard Worker 	printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
153*49cdfc7eSAndroid Build Coastguard Worker 		size - 1, ret, errmsg);
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 25)
156*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tPASS return value is 25 as expected\n");
157*49cdfc7eSAndroid Build Coastguard Worker 	else
158*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tFAIL return value is %d, expected 25\n", ret);
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker 	dataasciigen("this is a test of the my string", buffer, size, 0);
161*49cdfc7eSAndroid Build Coastguard Worker 	printf("dataasciigen(\"this is a test of the my string\", buffer, %d, 0)\n",
162*49cdfc7eSAndroid Build Coastguard Worker 		size);
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 	ret = dataasciichk("this is a test of the my string",
165*49cdfc7eSAndroid Build Coastguard Worker 			   buffer, size, 0, &errmsg);
166*49cdfc7eSAndroid Build Coastguard Worker 	printf("dataasciichk(\"this is a test of the my string\", buffer, %d, 0, &errmsg) returned %d %s\n",
167*49cdfc7eSAndroid Build Coastguard Worker 		size, ret, errmsg);
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1)
170*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tPASS return value is -1 as expected\n");
171*49cdfc7eSAndroid Build Coastguard Worker 	else
172*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tFAIL return value is %d, expected -1\n", ret);
173*49cdfc7eSAndroid Build Coastguard Worker 
174*49cdfc7eSAndroid Build Coastguard Worker 	ret =
175*49cdfc7eSAndroid Build Coastguard Worker 	    dataasciichk("this is a test of the my string", &buffer[1],
176*49cdfc7eSAndroid Build Coastguard Worker 			 size - 1, 1, &errmsg);
177*49cdfc7eSAndroid Build Coastguard Worker 	printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
178*49cdfc7eSAndroid Build Coastguard Worker 		size - 1, ret, errmsg);
179*49cdfc7eSAndroid Build Coastguard Worker 
180*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1)
181*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tPASS return value is -1 as expected\n");
182*49cdfc7eSAndroid Build Coastguard Worker 	else
183*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tFAIL return value is %d, expected -1\n", ret);
184*49cdfc7eSAndroid Build Coastguard Worker 
185*49cdfc7eSAndroid Build Coastguard Worker 	buffer[25] = 0x0;
186*49cdfc7eSAndroid Build Coastguard Worker 	printf("changing char 25\n");
187*49cdfc7eSAndroid Build Coastguard Worker 
188*49cdfc7eSAndroid Build Coastguard Worker 	ret = dataasciichk("this is a test of the my string", &buffer[1],
189*49cdfc7eSAndroid Build Coastguard Worker 			   size - 1, 1, &errmsg);
190*49cdfc7eSAndroid Build Coastguard Worker 	printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
191*49cdfc7eSAndroid Build Coastguard Worker 		size - 1, ret, errmsg);
192*49cdfc7eSAndroid Build Coastguard Worker 
193*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 25)
194*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tPASS return value is 25 as expected\n");
195*49cdfc7eSAndroid Build Coastguard Worker 	else
196*49cdfc7eSAndroid Build Coastguard Worker 		printf("\tFAIL return value is %d, expected 25\n", ret);
197*49cdfc7eSAndroid Build Coastguard Worker 
198*49cdfc7eSAndroid Build Coastguard Worker 	exit(0);
199*49cdfc7eSAndroid Build Coastguard Worker }
200*49cdfc7eSAndroid Build Coastguard Worker 
201*49cdfc7eSAndroid Build Coastguard Worker #endif
202