blob: 64e697eb3ac9dc2591a3ae0aa5f26a2ddceee839 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000014
Denis Vlasenko99912ca2007-04-10 15:43:37 +000015/* This is a NOFORK applet. Be very careful! */
16
17
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000018int bb_cat(char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000019{
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000020 static const char *const argv_dash[] = { "-", NULL };
Denis Vlasenko99912ca2007-04-10 15:43:37 +000021
Denis Vlasenko50f7f442007-04-11 23:20:53 +000022 int fd;
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 int retval = EXIT_SUCCESS;
Matt Kraaie7c1af12000-09-27 03:01:40 +000024
Denis Vlasenko99912ca2007-04-10 15:43:37 +000025 if (!*argv)
26 argv = (char**) &argv_dash;
Denis Vlasenkoe865e812006-12-21 13:24:58 +000027
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 do {
Denis Vlasenko50f7f442007-04-11 23:20:53 +000029 fd = STDIN_FILENO;
30 if (!LONE_DASH(*argv))
31 fd = open_or_warn(*argv, O_RDONLY);
32 if (fd >= 0) {
Denis Vlasenko99912ca2007-04-10 15:43:37 +000033 /* This is not an xfunc - never exits */
Denis Vlasenko50f7f442007-04-11 23:20:53 +000034 off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);
35 if (fd != STDIN_FILENO)
36 close(fd);
Denis Vlasenko714701c2006-12-22 00:21:07 +000037 if (r >= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 retval = EXIT_FAILURE;
41 } while (*++argv);
Erik Andersen029011b2000-03-04 21:19:32 +000042
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 return retval;
44}
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000045
Denis Vlasenko06af2162007-02-03 17:28:39 +000046int cat_main(int argc, char **argv);
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000047int cat_main(int argc, char **argv)
48{
49 getopt32(argc, argv, "u");
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000050 argv += optind;
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000051 return bb_cat(argv);
52}