xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/ftest/libftest.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) Cyril Hrubis [email protected] 2009
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
6*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
7*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
16*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <inttypes.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <assert.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "libftest.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker /*
27*49cdfc7eSAndroid Build Coastguard Worker  * Dump content of iov structure.
28*49cdfc7eSAndroid Build Coastguard Worker  */
ft_dumpiov(struct iovec * iov)29*49cdfc7eSAndroid Build Coastguard Worker void ft_dumpiov(struct iovec *iov)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	char *buf, val;
32*49cdfc7eSAndroid Build Coastguard Worker 	int idx, nout, i;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "\tBuf:");
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	nout = 0;
37*49cdfc7eSAndroid Build Coastguard Worker 	idx = 0;
38*49cdfc7eSAndroid Build Coastguard Worker 	buf = (char *)iov->iov_base;
39*49cdfc7eSAndroid Build Coastguard Worker 	val = *((char *)buf);
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; (unsigned int)i < iov->iov_len; i++) {
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 		if (buf[i] != val) {
44*49cdfc7eSAndroid Build Coastguard Worker 			if (i == idx + 1)
45*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TINFO, "\t%" PRIx32 "x,",
46*49cdfc7eSAndroid Build Coastguard Worker 					 buf[idx] & 0xff);
47*49cdfc7eSAndroid Build Coastguard Worker 			else
48*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TINFO, "\t%d*%" PRIx32 "x, ", i - idx,
49*49cdfc7eSAndroid Build Coastguard Worker 					 buf[idx] & 0xff);
50*49cdfc7eSAndroid Build Coastguard Worker 			idx = i;
51*49cdfc7eSAndroid Build Coastguard Worker 			++nout;
52*49cdfc7eSAndroid Build Coastguard Worker 		}
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 		if (nout > 10) {
55*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "\t ... more");
56*49cdfc7eSAndroid Build Coastguard Worker 			return;
57*49cdfc7eSAndroid Build Coastguard Worker 		}
58*49cdfc7eSAndroid Build Coastguard Worker 	}
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	if (i == idx + 1)
61*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "\t%" PRIx32 "x", buf[idx] & 0xff);
62*49cdfc7eSAndroid Build Coastguard Worker 	else
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "\t%d*%" PRIx32 "x", i - idx, buf[idx]);
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker /*
67*49cdfc7eSAndroid Build Coastguard Worker  * Dump bits string.
68*49cdfc7eSAndroid Build Coastguard Worker  */
ft_dumpbits(void * bits,size_t size)69*49cdfc7eSAndroid Build Coastguard Worker void ft_dumpbits(void *bits, size_t size)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker 	void *buf;
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "\tBits array:");
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	for (buf = bits; size > 0; --size, ++buf) {
76*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "\t%td:\t", 8 * (buf - bits));
77*49cdfc7eSAndroid Build Coastguard Worker 		if ((buf - bits) % 16 == 0) {
78*49cdfc7eSAndroid Build Coastguard Worker 			assert(0 < (buf - bits));
79*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "\t%td:\t", 8 * (buf - bits));
80*49cdfc7eSAndroid Build Coastguard Worker 		}
81*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "\t%02" PRIx32 "x ", *((char *)buf) & 0xff);
82*49cdfc7eSAndroid Build Coastguard Worker 	}
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "\t");
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker /*
88*49cdfc7eSAndroid Build Coastguard Worker  * Do logical or of hold and bits (of size)
89*49cdfc7eSAndroid Build Coastguard Worker  * fields and store result into hold field.
90*49cdfc7eSAndroid Build Coastguard Worker  */
ft_orbits(char * hold,char * bits,int size)91*49cdfc7eSAndroid Build Coastguard Worker void ft_orbits(char *hold, char *bits, int size)
92*49cdfc7eSAndroid Build Coastguard Worker {
93*49cdfc7eSAndroid Build Coastguard Worker 	while (size-- > 0)
94*49cdfc7eSAndroid Build Coastguard Worker 		*hold++ |= *bits++;
95*49cdfc7eSAndroid Build Coastguard Worker }
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker /*
98*49cdfc7eSAndroid Build Coastguard Worker  * Dumps buffer in hexadecimal format.
99*49cdfc7eSAndroid Build Coastguard Worker  */
ft_dumpbuf(char * buf,int csize)100*49cdfc7eSAndroid Build Coastguard Worker void ft_dumpbuf(char *buf, int csize)
101*49cdfc7eSAndroid Build Coastguard Worker {
102*49cdfc7eSAndroid Build Coastguard Worker 	char val;
103*49cdfc7eSAndroid Build Coastguard Worker 	int idx, nout, i;
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "\tBuf:");
106*49cdfc7eSAndroid Build Coastguard Worker 	nout = 0;
107*49cdfc7eSAndroid Build Coastguard Worker 	idx = 0;
108*49cdfc7eSAndroid Build Coastguard Worker 	val = buf[0];
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < csize; i++) {
111*49cdfc7eSAndroid Build Coastguard Worker 		if (buf[i] != val) {
112*49cdfc7eSAndroid Build Coastguard Worker 			if (i == idx + 1)
113*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TINFO, "\t%x, ", buf[idx] & 0xff);
114*49cdfc7eSAndroid Build Coastguard Worker 			else
115*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TINFO, "\t%d*%x, ", i - idx,
116*49cdfc7eSAndroid Build Coastguard Worker 					 buf[idx] & 0xff);
117*49cdfc7eSAndroid Build Coastguard Worker 			idx = i;
118*49cdfc7eSAndroid Build Coastguard Worker 			++nout;
119*49cdfc7eSAndroid Build Coastguard Worker 		}
120*49cdfc7eSAndroid Build Coastguard Worker 		if (nout > 10) {
121*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "\t ... more");
122*49cdfc7eSAndroid Build Coastguard Worker 			return;
123*49cdfc7eSAndroid Build Coastguard Worker 		}
124*49cdfc7eSAndroid Build Coastguard Worker 	}
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker 	if (i == idx + 1)
127*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "\t%x", buf[idx] & 0xff);
128*49cdfc7eSAndroid Build Coastguard Worker 	else
129*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "\t%d*%x", i - idx, buf[idx]);
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker /*
133*49cdfc7eSAndroid Build Coastguard Worker  * Creates filename from path and numbers.
134*49cdfc7eSAndroid Build Coastguard Worker  *
135*49cdfc7eSAndroid Build Coastguard Worker  * TODO: name is big enough?
136*49cdfc7eSAndroid Build Coastguard Worker  */
ft_mkname(char * name,char * dirname,int me,int idx)137*49cdfc7eSAndroid Build Coastguard Worker void ft_mkname(char *name, char *dirname, int me, int idx)
138*49cdfc7eSAndroid Build Coastguard Worker {
139*49cdfc7eSAndroid Build Coastguard Worker 	char a, b;
140*49cdfc7eSAndroid Build Coastguard Worker 
141*49cdfc7eSAndroid Build Coastguard Worker 	a = 'A' + (me % 26);
142*49cdfc7eSAndroid Build Coastguard Worker 	b = 'a' + (idx % 26);
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	if (dirname[0] != '\0')
145*49cdfc7eSAndroid Build Coastguard Worker 		snprintf(name, PATH_MAX, "%s/%c%c", dirname, a, b);
146*49cdfc7eSAndroid Build Coastguard Worker 	else
147*49cdfc7eSAndroid Build Coastguard Worker 		snprintf(name, PATH_MAX, "%c%c", a, b);
148*49cdfc7eSAndroid Build Coastguard Worker }
149