Add basic support for mini2440 board to barebox.
[barebox-mini2440.git] / commands / cat.c
blob41b33243008d848196b1356275286b1a7a740aa5
1 /*
2 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
4 * See file CREDITS for list of people who contributed to this
5 * project.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /**
22 * @file
23 * @brief Concatenate files to stdout command
26 #include <common.h>
27 #include <command.h>
28 #include <fs.h>
29 #include <fcntl.h>
30 #include <linux/ctype.h>
31 #include <errno.h>
32 #include <xfuncs.h>
33 #include <malloc.h>
35 #define BUFSIZE 1024
37 /**
38 * @param[in] cmdtp FIXME
39 * @param[in] argc Argument count from command line
40 * @param[in] argv List of input arguments
42 static int do_cat(struct command *cmdtp, int argc, char *argv[])
44 int ret;
45 int fd, i;
46 char *buf;
47 int err = 0;
48 int args = 1;
50 if (argc < 2) {
51 perror("cat");
52 return 1;
55 buf = xmalloc(BUFSIZE);
57 while (args < argc) {
58 fd = open(argv[args], O_RDONLY);
59 if (fd < 0) {
60 err = 1;
61 printf("could not open %s: %s\n", argv[args], errno_str());
62 goto out;
65 while((ret = read(fd, buf, BUFSIZE)) > 0) {
66 for(i = 0; i < ret; i++) {
67 if (isprint(buf[i]) || buf[i] == '\n' || buf[i] == '\t')
68 putchar(buf[i]);
69 else
70 putchar('.');
72 if(ctrlc()) {
73 err = 1;
74 close(fd);
75 goto out;
78 close(fd);
79 args++;
82 out:
83 free(buf);
85 return err;
88 static const __maybe_unused char cmd_cat_help[] =
89 "Usage: cat [FILES]\n"
90 "Concatenate files on stdout. Currently only printable characters\n"
91 "and \\n and \\t are printed, but this should be optional\n";
93 BAREBOX_CMD_START(cat)
94 .cmd = do_cat,
95 .usage = "concatenate file(s)",
96 BAREBOX_CMD_HELP(cmd_cat_help)
97 BAREBOX_CMD_END
99 /**
100 * @page cat_command cat (concatenate)
102 * Usage is: cat \<file\> [\<file\> ...]
104 * Concatenate files to stdout. Currently only printable characters
105 * and \\n and \\t are printed, but this should be optional