blob: 81bfcafd57e0be3d19a32bc2d3e35b902b7c4596 [file] [log] [blame]
Rob Landley52b499c2008-04-04 12:19:21 -05001/* vi: set sw=4 ts=4:
2 *
Rob Landley24396bb2008-08-15 14:16:53 -05003 * cat.c - copy inputs to stdout.
Rob Landley52b499c2008-04-04 12:19:21 -05004 *
5 * Copyright 2006 Rob Landley <rob@landley.net>
6 *
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/cat.html
8
9USE_CAT(NEWTOY(cat, "u", TOYFLAG_BIN))
10
11config CAT
12 bool "cat"
13 default y
14 help
15 usage: cat [-u] [file...]
16 Copy (concatenate) files to stdout. If no files listed, copy from stdin.
Rob Landley24396bb2008-08-15 14:16:53 -050017 Filename "-" is a synonym for stdin.
Rob Landley52b499c2008-04-04 12:19:21 -050018
19 -u Copy one byte at a time (slow).
20*/
21
22#include "toys.h"
23
24static void do_cat(int fd, char *name)
25{
26 int len, size=toys.optflags ? 1 : sizeof(toybuf);
27
28 for (;;) {
Rob Landley24396bb2008-08-15 14:16:53 -050029 len = read(fd, toybuf, size);
30 if (len<0) {
31 perror_msg("%s",name);
32 toys.exitval = EXIT_FAILURE;
33 }
Rob Landley52b499c2008-04-04 12:19:21 -050034 if (len<1) break;
Rob Landleyaef99f42008-04-09 00:22:04 -050035 xwrite(1, toybuf, len);
Rob Landley52b499c2008-04-04 12:19:21 -050036 }
37}
38
39void cat_main(void)
40{
41 loopfiles(toys.optargs, do_cat);
42}