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 #include <limits.h> 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <unistd.h> 15 16 static void 17 usage(void) 18 { 19 20 fprintf(stderr, "usage: %s [-sx] [-n count] [prefix [suffix]]\n", 21 getprogname()); 22 exit(1); 23 } 24 25 int 26 main(int argc, char *argv[]) 27 { 28 int c, count, linepos, maxcount, pretty, radix; 29 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