blob: 00c38d486ade6b718999b7b0026ce6fb951ca8c2 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * cat implementation for busybox
Eric Andersencc8ed391999-10-05 16:24:54 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Eric Andersencc8ed391999-10-05 16:24:54 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersencc8ed391999-10-05 16:24:54 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
12
Denys Vlasenkoe4070cb2010-06-04 19:59:49 +020013//kbuild:lib-$(CONFIG_CAT) += cat.o
14//kbuild:lib-$(CONFIG_MORE) += cat.o # more uses it if stdout isn't a tty
15//kbuild:lib-$(CONFIG_LESS) += cat.o # less too
16//kbuild:lib-$(CONFIG_CRONTAB) += cat.o # crontab -l
17
18//config:config CAT
19//config: bool "cat"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020020//config: default y
Denys Vlasenkoe4070cb2010-06-04 19:59:49 +020021//config: help
22//config: cat is used to concatenate files and print them to the standard
23//config: output. Enable this option if you wish to enable the 'cat' utility.
24
Pere Orga34425382011-03-31 14:43:25 +020025//usage:#define cat_trivial_usage
26//usage: "[FILE]..."
27//usage:#define cat_full_usage "\n\n"
28//usage: "Concatenate FILEs and print them to stdout"
29//usage:
30//usage:#define cat_example_usage
31//usage: "$ cat /proc/uptime\n"
32//usage: "110716.72 17.67"
33
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Denis Vlasenko99912ca2007-04-10 15:43:37 +000036/* This is a NOFORK applet. Be very careful! */
37
38
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000039int bb_cat(char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000040{
Denis Vlasenko50f7f442007-04-11 23:20:53 +000041 int fd;
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 int retval = EXIT_SUCCESS;
Matt Kraaie7c1af12000-09-27 03:01:40 +000043
Denis Vlasenko99912ca2007-04-10 15:43:37 +000044 if (!*argv)
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000045 argv = (char**) &bb_argv_dash;
Denis Vlasenkoe865e812006-12-21 13:24:58 +000046
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000048 fd = open_or_warn_stdin(*argv);
Denis Vlasenko50f7f442007-04-11 23:20:53 +000049 if (fd >= 0) {
Denis Vlasenko97bd0e02008-02-08 15:41:01 +000050 /* This is not a xfunc - never exits */
Denis Vlasenko50f7f442007-04-11 23:20:53 +000051 off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);
52 if (fd != STDIN_FILENO)
53 close(fd);
Denis Vlasenko714701c2006-12-22 00:21:07 +000054 if (r >= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 retval = EXIT_FAILURE;
58 } while (*++argv);
Erik Andersen029011b2000-03-04 21:19:32 +000059
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 return retval;
61}
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000062
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000063int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000064int cat_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000065{
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000066 getopt32(argv, "u");
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000067 argv += optind;
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000068 return bb_cat(argv);
69}