blob: 86527576728f23f837ad2a316fa47f5311bbcb87 [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* BB_AUDIT SUSv3 compliant */
24/* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
25
26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27 *
28 * This is a new implementation of 'cat' which aims to be SUSv3 compliant.
29 *
30 * Changes from the previous implementation include:
31 * 1) Multiple '-' args are accepted as required by SUSv3. The previous
32 * implementation would close stdin and segfault on a subsequent '-'.
33 * 2) The '-u' options is required by SUSv3. Note that the specified
34 * behavior for '-u' is done by default, so all we need do is accept
35 * the option.
36 */
37
Eric Andersened3ef502001-01-27 08:24:39 +000038#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000039#include <stdio.h>
40#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000041#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000042
Eric Andersene77ae3a1999-10-19 20:03:34 +000043extern int cat_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000044{
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 FILE *f;
46 int retval = EXIT_SUCCESS;
Matt Kraaie7c1af12000-09-27 03:01:40 +000047
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_getopt_ulflags(argc, argv, "u");
49
50 argv += optind;
51 if (!*argv) {
52 *--argv = "-";
Eric Andersencc8ed391999-10-05 16:24:54 +000053 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000054
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 do {
56 if ((f = bb_wfopen_input(*argv)) != NULL) {
57 int r = bb_copyfd(fileno(f), STDOUT_FILENO, 0);
58 bb_fclose_nonstdin(f);
59 if (r >= 0) {
60 continue;
61 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 retval = EXIT_FAILURE;
64 } while (*++argv);
Erik Andersen029011b2000-03-04 21:19:32 +000065
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 return retval;
67}