185909Simp/*
285909Simp * ----------------------------------------------------------------------------
3122116Sbde * "THE BEER-WARE LICENSE" (Revision 42):
4122116Sbde * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5122116Sbde * can do whatever you want with this stuff. If we meet some day, and you think
6180012Sru * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7160440Sobrien * ----------------------------------------------------------------------------
8210151Simp */
9210151Simp
10210151Simp#include <sys/cdefs.h>
1185909Simp__FBSDID("$FreeBSD$");
1285909Simp
1385909Simp#include <limits.h>
1485909Simp#include <stdio.h>
15175888Simp#include <stdlib.h>
16175888Simp#include <unistd.h>
1785909Simp
18218538Simpstatic void
1985909Simpusage(void)
2091512Sobrien{
21116341Smarkm
2285909Simp	fprintf(stderr, "usage: %s [-sx] [-n count] [prefix [suffix]]\n",
2385909Simp	    getprogname());
2485909Simp	exit(1);
2585909Simp}
26220863Sdim
27140606Sobrienint
28187103Sgnnmain(int argc, char *argv[])
29220863Sdim{
30140606Sobrien	int c, count, linepos, maxcount, pretty, radix;
31220863Sdim
32220863Sdim	maxcount = 0;
33127204Sobrien	pretty = 0;
34220863Sdim	radix = 10;
35140606Sobrien	while ((c = getopt(argc, argv, "n:sx")) != -1) {
36220863Sdim		switch (c) {
37220863Sdim		case 'n':	/* Max. number of bytes per line. */
38124834Sru			maxcount = strtol(optarg, NULL, 10);
39124834Sru			break;
4085909Simp		case 's':	/* Be more style(9) comliant. */
4185909Simp			pretty = 1;
4285909Simp			break;
43160043Sobrien		case 'x':	/* Print hexadecimal numbers. */
44126890Strhodes			radix = 16;
4585909Simp			break;
46192901Sthompsa		case '?':
47126890Strhodes		default:
48151605Sobrien			usage();
49151605Sobrien		}
50130416Smlaier	}
51130416Smlaier	argc -= optind;
52149978Sobrien	argv += optind;
53149978Sobrien
54149978Sobrien	if (argc > 0)
55149978Sobrien		printf("%s\n", argv[0]);
56149978Sobrien	count = linepos = 0;
57149978Sobrien	while((c = getchar()) != EOF) {
58149978Sobrien		if (count) {
59149978Sobrien			putchar(',');
60185522Ssam			linepos++;
61185522Ssam		}
62149978Sobrien		if ((maxcount == 0 && linepos > 70) ||
63149978Sobrien		    (maxcount > 0 && count >= maxcount)) {
64149978Sobrien			putchar('\n');
65149978Sobrien			count = linepos = 0;
66149978Sobrien		}
67149978Sobrien		if (pretty) {
68149978Sobrien			if (count) {
69153325Srodrigc				putchar(' ');
70153325Srodrigc				linepos++;
71153325Srodrigc			} else {
72218792Snp				putchar('\t');
73218792Snp				linepos += 8;
74183292Skmacy			}
75149978Sobrien		}
76149978Sobrien		switch (radix) {
77160043Sobrien		case 10:
78150966Sglebius			linepos += printf("%d", c);
79211437Srpaulo			break;
80124834Sru		case 16:
81210311Sjmallett			linepos += printf("0x%02x", c);
82132766Skan			break;
83132766Skan		default:
84210311Sjmallett			abort();
85210311Sjmallett		}
86215988Sjmallett		count++;
87210311Sjmallett	}
88215988Sjmallett	putchar('\n');
89210311Sjmallett	if (argc > 1)
90210394Srpaulo		printf("%s\n", argv[1]);
91171239Speter	return (0);
9285909Simp}
9385909Simp