File: printbuf.c

package info (click to toggle)
lowpan-tools 0.2.2-2.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,032 kB
  • sloc: sh: 13,195; ansic: 2,800; python: 262; yacc: 162; makefile: 89; lex: 83
file content (58 lines) | stat: -rw-r--r-- 1,711 bytes parent folder | download | duplicates (2)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Linux IEEE 802.15.4 userspace tools
 *
 * Copyright (C) 2008, 2009 Sismens AG
 *
 * Written-by: Dmitry Eremin-Solenikov
 * Written-by: Sergey Lapin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>

#include <libcommon.h>

void printbuf(const unsigned char *buf, int len) {
	char outbuf[1024], outbuf2[1024];
	int olen = 0, olen2 = 0;
	int i;
	for (i = 0; i < len; ) {
		if (i % 8 == 0) {
			olen = olen2 = 0;
			outbuf[olen] = outbuf2[olen2] = '\0';
			olen += snprintf(outbuf + olen, sizeof(outbuf) - olen, "%03x: ", i);
			if (i > len - 8) {
				int j;
				for (j = len % 8; j < 8; j++)
					olen2 += snprintf(outbuf2 + olen2, sizeof(outbuf2) - olen2, "   ");
			}
			olen2 += snprintf(outbuf2 + olen2, sizeof(outbuf2) - olen2, "| ");
		}

		outbuf2[olen2++] = (buf[i] > ' ' && buf[i] < 0x7f) ? buf[i] : '.';
		olen += snprintf(outbuf + olen, sizeof(outbuf) - olen, "%02x ", buf[i++]);

		if ((i % 8 == 0) || (i == len)) {
			outbuf2[olen2] = '\0';
			printf("%s%s\n", outbuf, outbuf2);
		}
	}
}