aboutsummaryrefslogtreecommitdiff
path: root/tools/xway-swap-bytes.c
blob: 3a6d82d54a06f68753e923907688720f8d9dac5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifndef BUFSIZ
# define BUFSIZ 4096
#endif

#undef BUFSIZ
# define BUFSIZ 64
int main (void)
{
	short ibuff[BUFSIZ], obuff[BUFSIZ];
	int rc, i, len;

	while ((rc = read (0, ibuff, sizeof (ibuff))) > 0) {
		memset (obuff, 0, sizeof (obuff));
		for (i = 0; i < (rc + 1) / 2; i++) {
			obuff[i] = ibuff[i ^ 1];
		}

		len = (rc + 1) & ~1;

		if (write (1, obuff, len) != len) {
			perror ("read error");
			return (EXIT_FAILURE);
		}

		memset (ibuff, 0, sizeof (ibuff));
	}

	if (rc < 0) {
		perror ("read error");
		return (EXIT_FAILURE);
	}
	return (EXIT_SUCCESS);
}