15996Sphk/*
25996Sphk * ----------------------------------------------------------------------------
35996Sphk * "THE BEER-WARE LICENSE" (Revision 42):
493150Sphk * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
55996Sphk * can do whatever you want with this stuff. If we meet some day, and you think
65996Sphk * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
75996Sphk * ----------------------------------------------------------------------------
85996Sphk */
95996Sphk
1099112Sobrien#include <sys/cdefs.h>
1199112Sobrien__FBSDID("$FreeBSD$");
1299112Sobrien
13200462Sdelphij#include <limits.h>
145996Sphk#include <stdio.h>
15146231Smarcel#include <stdlib.h>
16146231Smarcel#include <unistd.h>
175996Sphk
18146231Smarcelstatic void
19146231Smarcelusage(void)
20146231Smarcel{
21146231Smarcel
22173197Sru	fprintf(stderr, "usage: %s [-sx] [-n count] [prefix [suffix]]\n",
23146231Smarcel	    getprogname());
24146231Smarcel	exit(1);
25146231Smarcel}
26146231Smarcel
275996Sphkint
28146225Smarcelmain(int argc, char *argv[])
295996Sphk{
30167823Sjkim	int c, count, linepos, maxcount, pretty, radix;
315996Sphk
32146231Smarcel	maxcount = 0;
33167823Sjkim	pretty = 0;
34146231Smarcel	radix = 10;
35167823Sjkim	while ((c = getopt(argc, argv, "n:sx")) != -1) {
36146231Smarcel		switch (c) {
37146231Smarcel		case 'n':	/* Max. number of bytes per line. */
38146231Smarcel			maxcount = strtol(optarg, NULL, 10);
39146231Smarcel			break;
40167823Sjkim		case 's':	/* Be more style(9) comliant. */
41167823Sjkim			pretty = 1;
42167823Sjkim			break;
43146231Smarcel		case 'x':	/* Print hexadecimal numbers. */
44146231Smarcel			radix = 16;
45146231Smarcel			break;
46146231Smarcel		case '?':
47146231Smarcel		default:
48146231Smarcel			usage();
49146231Smarcel		}
50146231Smarcel	}
51146231Smarcel	argc -= optind;
52146231Smarcel	argv += optind;
53146231Smarcel
54146231Smarcel	if (argc > 0)
55146231Smarcel		printf("%s\n", argv[0]);
56146231Smarcel	count = linepos = 0;
57146231Smarcel	while((c = getchar()) != EOF) {
58146231Smarcel		if (count) {
59146225Smarcel			putchar(',');
60146231Smarcel			linepos++;
61146225Smarcel		}
62146231Smarcel		if ((maxcount == 0 && linepos > 70) ||
63146231Smarcel		    (maxcount > 0 && count >= maxcount)) {
64146225Smarcel			putchar('\n');
65146231Smarcel			count = linepos = 0;
66146225Smarcel		}
67167823Sjkim		if (pretty) {
68167823Sjkim			if (count) {
69167823Sjkim				putchar(' ');
70167823Sjkim				linepos++;
71167823Sjkim			} else {
72167823Sjkim				putchar('\t');
73167823Sjkim				linepos += 8;
74167823Sjkim			}
75167823Sjkim		}
76146231Smarcel		switch (radix) {
77146231Smarcel		case 10:
78146231Smarcel			linepos += printf("%d", c);
79146231Smarcel			break;
80146231Smarcel		case 16:
81146231Smarcel			linepos += printf("0x%02x", c);
82146231Smarcel			break;
83146231Smarcel		default:
84146231Smarcel			abort();
85146231Smarcel		}
86146231Smarcel		count++;
875996Sphk	}
88146225Smarcel	putchar('\n');
89146231Smarcel	if (argc > 1)
90146231Smarcel		printf("%s\n", argv[1]);
91146225Smarcel	return (0);
925996Sphk}
93