1 /* 2 * ---------------------------------------------------------------------------- 3 * "THE BEER-WARE LICENSE" (Revision 42): 4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you 5 * can do whatever you want with this stuff. If we meet some day, and you think 6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 7 * ---------------------------------------------------------------------------- 8 */ 9 10 #include <sys/cdefs.h> 11 __FBSDID("$FreeBSD$"); 12 13 #include <limits.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <unistd.h> 17 18 static void 19 usage(void) 20 { 21 22 fprintf(stderr, "usage: %s [-n count] [-x] [prefix [suffix]]\n", 23 getprogname()); 24 exit(1); 25 } 26 27 int 28 main(int argc, char *argv[]) 29 { 30 int c, count, linepos, maxcount, radix; 31 32 maxcount = 0; 33 radix = 10; 34 while ((c = getopt(argc, argv, "n:x")) != -1) { 35 switch (c) { 36 case 'n': /* Max. number of bytes per line. */ 37 maxcount = strtol(optarg, NULL, 10); 38 break; 39 case 'x': /* Print hexadecimal numbers. */ 40 radix = 16; 41 break; 42 case '?': 43 default: 44 usage(); 45 } 46 } 47 argc -= optind; 48 argv += optind; 49 50 if (argc > 0) 51 printf("%s\n", argv[0]); 52 count = linepos = 0; 53 while((c = getchar()) != EOF) { 54 if (count) { 55 putchar(','); 56 linepos++; 57 } 58 if ((maxcount == 0 && linepos > 70) || 59 (maxcount > 0 && count >= maxcount)) { 60 putchar('\n'); 61 count = linepos = 0; 62 } 63 switch (radix) { 64 case 10: 65 linepos += printf("%d", c); 66 break; 67 case 16: 68 linepos += printf("0x%02x", c); 69 break; 70 default: 71 abort(); 72 } 73 count++; 74 } 75 putchar('\n'); 76 if (argc > 1) 77 printf("%s\n", argv[1]); 78 return (0); 79 } 80