150472Speter/*
233975Sjdp * ----------------------------------------------------------------------------
3275373Semaste * "THE BEER-WARE LICENSE" (Revision 42):
4275373Semaste * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5276651Sbapt * can do whatever you want with this stuff. If we meet some day, and you think
6276651Sbapt * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7275193Semaste * ----------------------------------------------------------------------------
8275193Semaste */
9275193Semaste
10275193Semaste#include <sys/cdefs.h>
11275193Semaste#include <limits.h>
12286030Semaste#include <stdio.h>
13275193Semaste#include <stdlib.h>
14276551Sbapt#include <unistd.h>
15286730Semaste
16286030Semastestatic void
17286030Semasteusage(void)
1833975Sjdp{
19287262Simp
20287262Simp	fprintf(stderr, "usage: %s [-sx] [-n count] [prefix [suffix]]\n",
21287262Simp	    getprogname());
22287262Simp	exit(1);
23287262Simp}
24287150Simp
25287150Simpint
26287150Simpmain(int argc, char *argv[])
27287150Simp{
28287150Simp	int c, count, linepos, maxcount, pretty, radix;
2933975Sjdp
30	maxcount = 0;
31	pretty = 0;
32	radix = 10;
33	while ((c = getopt(argc, argv, "n:sx")) != -1) {
34		switch (c) {
35		case 'n':	/* Max. number of bytes per line. */
36			maxcount = strtol(optarg, NULL, 10);
37			break;
38		case 's':	/* Be more style(9) comliant. */
39			pretty = 1;
40			break;
41		case 'x':	/* Print hexadecimal numbers. */
42			radix = 16;
43			break;
44		case '?':
45		default:
46			usage();
47		}
48	}
49	argc -= optind;
50	argv += optind;
51
52	if (argc > 0)
53		printf("%s\n", argv[0]);
54	count = linepos = 0;
55	while((c = getchar()) != EOF) {
56		if (count) {
57			putchar(',');
58			linepos++;
59		}
60		if ((maxcount == 0 && linepos > 70) ||
61		    (maxcount > 0 && count >= maxcount)) {
62			putchar('\n');
63			count = linepos = 0;
64		}
65		if (pretty) {
66			if (count) {
67				putchar(' ');
68				linepos++;
69			} else {
70				putchar('\t');
71				linepos += 8;
72			}
73		}
74		switch (radix) {
75		case 10:
76			linepos += printf("%d", c);
77			break;
78		case 16:
79			linepos += printf("0x%02x", c);
80			break;
81		default:
82			abort();
83		}
84		count++;
85	}
86	putchar('\n');
87	if (argc > 1)
88		printf("%s\n", argv[1]);
89	return (0);
90}
91