10f66f451Sopenharmony_ci/* cat.c - copy inputs to stdout. 20f66f451Sopenharmony_ci * 30f66f451Sopenharmony_ci * Copyright 2006 Rob Landley <rob@landley.net> 40f66f451Sopenharmony_ci * 50f66f451Sopenharmony_ci * See http://opengroup.org/onlinepubs/9699919799/utilities/cat.html 60f66f451Sopenharmony_ci 70f66f451Sopenharmony_ciUSE_CAT(NEWTOY(cat, "uvte", TOYFLAG_BIN)) 80f66f451Sopenharmony_ci 90f66f451Sopenharmony_ciconfig CAT 100f66f451Sopenharmony_ci bool "cat" 110f66f451Sopenharmony_ci default y 120f66f451Sopenharmony_ci help 130f66f451Sopenharmony_ci usage: cat [-etuv] [FILE...] 140f66f451Sopenharmony_ci 150f66f451Sopenharmony_ci Copy (concatenate) files to stdout. If no files listed, copy from stdin. 160f66f451Sopenharmony_ci Filename "-" is a synonym for stdin. 170f66f451Sopenharmony_ci 180f66f451Sopenharmony_ci -e Mark each newline with $ 190f66f451Sopenharmony_ci -t Show tabs as ^I 200f66f451Sopenharmony_ci -u Copy one byte at a time (slow) 210f66f451Sopenharmony_ci -v Display nonprinting characters as escape sequences with M-x for 220f66f451Sopenharmony_ci high ascii characters (>127), and ^x for other nonprinting chars 230f66f451Sopenharmony_ci*/ 240f66f451Sopenharmony_ci 250f66f451Sopenharmony_ci#define FOR_cat 260f66f451Sopenharmony_ci#define FORCE_FLAGS 270f66f451Sopenharmony_ci#include "toys.h" 280f66f451Sopenharmony_ci 290f66f451Sopenharmony_cistatic void do_cat(int fd, char *name) 300f66f451Sopenharmony_ci{ 310f66f451Sopenharmony_ci int i, len, size = FLAG(u) ? 1 : sizeof(toybuf); 320f66f451Sopenharmony_ci 330f66f451Sopenharmony_ci for(;;) { 340f66f451Sopenharmony_ci len = read(fd, toybuf, size); 350f66f451Sopenharmony_ci if (len<0) perror_msg_raw(name); 360f66f451Sopenharmony_ci if (len<1) break; 370f66f451Sopenharmony_ci if (toys.optflags&~FLAG_u) { 380f66f451Sopenharmony_ci for (i = 0; i<len; i++) { 390f66f451Sopenharmony_ci char c = toybuf[i]; 400f66f451Sopenharmony_ci 410f66f451Sopenharmony_ci if (c>126 && FLAG(v)) { 420f66f451Sopenharmony_ci if (c>127) { 430f66f451Sopenharmony_ci printf("M-"); 440f66f451Sopenharmony_ci c -= 128; 450f66f451Sopenharmony_ci } 460f66f451Sopenharmony_ci if (c == 127) { 470f66f451Sopenharmony_ci printf("^?"); 480f66f451Sopenharmony_ci continue; 490f66f451Sopenharmony_ci } 500f66f451Sopenharmony_ci } 510f66f451Sopenharmony_ci if (c<32) { 520f66f451Sopenharmony_ci if (c == 10) { 530f66f451Sopenharmony_ci if (FLAG(e)) xputc('$'); 540f66f451Sopenharmony_ci } else if (toys.optflags & (c==9 ? FLAG_t : FLAG_v)) { 550f66f451Sopenharmony_ci printf("^%c", c+'@'); 560f66f451Sopenharmony_ci continue; 570f66f451Sopenharmony_ci } 580f66f451Sopenharmony_ci } 590f66f451Sopenharmony_ci xputc(c); 600f66f451Sopenharmony_ci } 610f66f451Sopenharmony_ci } else xwrite(1, toybuf, len); 620f66f451Sopenharmony_ci } 630f66f451Sopenharmony_ci} 640f66f451Sopenharmony_ci 650f66f451Sopenharmony_civoid cat_main(void) 660f66f451Sopenharmony_ci{ 670f66f451Sopenharmony_ci loopfiles(toys.optargs, do_cat); 680f66f451Sopenharmony_ci} 69