1/* vi: set sw=4 ts=4: */ 2/* 3 * cat -v implementation for busybox 4 * 5 * Copyright (C) 2006 Rob Landley <rob@landley.net> 6 * 7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 8 */ 9 10/* See "Cat -v considered harmful" at 11 * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */ 12 13#include "libbb.h" 14 15int catv_main(int argc, char **argv); 16int catv_main(int argc, char **argv) 17{ 18 int retval = EXIT_SUCCESS; 19 int fd; 20 unsigned flags; 21 22 flags = getopt32(argv, "etv"); 23#define CATV_OPT_e (1<<0) 24#define CATV_OPT_t (1<<1) 25#define CATV_OPT_v (1<<2) 26 flags ^= CATV_OPT_v; 27 argv += optind; 28 29 /* Read from stdin if there's nothing else to do. */ 30 fd = 0; 31 if (!argv[0]) { 32 argv--; 33 goto jump_in; 34 } 35 do { 36 fd = open_or_warn(*argv, O_RDONLY); 37 if (fd < 0) { 38 retval = EXIT_FAILURE; 39 continue; 40 } 41 jump_in: 42 for (;;) { 43 int i, res; 44 45#define read_buf bb_common_bufsiz1 46 res = read(fd, read_buf, COMMON_BUFSIZE); 47 if (res < 0) 48 retval = EXIT_FAILURE; 49 if (res < 1) 50 break; 51 for (i = 0; i < res; i++) { 52 unsigned char c = read_buf[i]; 53 54 if (c > 126 && (flags & CATV_OPT_v)) { 55 if (c == 127) { 56 printf("^?"); 57 continue; 58 } 59 printf("M-"); 60 c -= 128; 61 } 62 if (c < 32) { 63 if (c == 10) { 64 if (flags & CATV_OPT_e) 65 putchar('$'); 66 } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) { 67 printf("^%c", c+'@'); 68 continue; 69 } 70 } 71 putchar(c); 72 } 73 } 74 if (ENABLE_FEATURE_CLEAN_UP && fd) 75 close(fd); 76 } while (*++argv); 77 78 fflush_stdout_and_exit(retval); 79} 80