blob: 2b7c6035f83eaa2c5fb61c2cb9ad503ac67dbf00 [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 *
Denis Vlasenko7039a662006-10-08 17:54:47 +00007 * Licensed under GPLv2, see file License in this tarball for details.
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
Eric Andersencbe31da2001-02-20 06:14:08 +000013#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000014
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000015int bb_cat(char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000016{
Denis Vlasenkoe865e812006-12-21 13:24:58 +000017 static char *const argv_dash[] = { "-", NULL };
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 FILE *f;
19 int retval = EXIT_SUCCESS;
Matt Kraaie7c1af12000-09-27 03:01:40 +000020
Denis Vlasenkoe865e812006-12-21 13:24:58 +000021 if (!*argv) argv = (char**) &argv_dash;
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000024 f = fopen_or_warn_stdin(*argv);
Denis Vlasenko7039a662006-10-08 17:54:47 +000025 if (f) {
26 off_t r = bb_copyfd_eof(fileno(f), STDOUT_FILENO);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000027 fclose_if_not_stdin(f);
Denis Vlasenko714701c2006-12-22 00:21:07 +000028 if (r >= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000029 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +000030 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 retval = EXIT_FAILURE;
32 } while (*++argv);
Erik Andersen029011b2000-03-04 21:19:32 +000033
Manuel Novoa III cad53642003-03-19 09:13:01 +000034 return retval;
35}
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000036
37int cat_main(int argc, char **argv)
38{
39 getopt32(argc, argv, "u");
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000040 argv += optind;
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000041 return bb_cat(argv);
42}