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