Add basic support for mini2440 board to barebox.
[barebox-mini2440.git] / commands / echo.c
blobdfa14d67b94e72e38b808b3cb65547d5f054ffaf
1 /*
2 * echo.c - echo arguments to stdout or into a file
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <common.h>
24 #include <command.h>
25 #include <fs.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <libbb.h>
30 static int do_echo(struct command *cmdtp, int argc, char *argv[])
32 int i, optind = 1;
33 int fd = stdout, opt, newline = 1;
34 char *file = NULL;
35 int oflags = O_WRONLY | O_CREAT;
36 #ifdef CONFIG_CMD_ECHO_E
37 char str[CONFIG_CBSIZE];
38 int process_escape = 0;
39 #endif
40 /* We can't use getopt() here because we want to
41 * echo all things we don't understand.
43 while (optind < argc && *argv[optind] == '-') {
44 if (!*(argv[optind] + 1) || *(argv[optind] + 2))
45 break;
47 opt = *(argv[optind] + 1);
48 switch (opt) {
49 case 'n':
50 newline = 0;
51 break;
52 case 'a':
53 oflags |= O_APPEND;
54 if (optind + 1 < argc)
55 file = argv[optind + 1];
56 else
57 goto no_optarg_out;
58 optind++;
59 break;
60 case 'o':
61 oflags |= O_TRUNC;
62 file = argv[optind + 1];
63 if (optind + 1 < argc)
64 file = argv[optind + 1];
65 else
66 goto no_optarg_out;
67 optind++;
68 break;
69 #ifdef CONFIG_CMD_ECHO_E
70 case 'e':
71 process_escape = 1;
72 break;
73 #endif
74 default:
75 goto exit_parse;
77 optind++;
80 exit_parse:
81 if (file) {
82 fd = open(file, oflags);
83 if (fd < 0) {
84 perror("open");
85 return 1;
89 for (i = optind; i < argc; i++) {
90 if (i > optind)
91 fputc(fd, ' ');
92 #ifdef CONFIG_CMD_ECHO_E
93 if (process_escape) {
94 process_escape_sequence(argv[i], str, CONFIG_CBSIZE);
95 fputs(fd, str);
96 } else
97 #endif
98 fputs(fd, argv[i]);
101 if (newline)
102 fputc(fd, '\n');
104 if (file)
105 close(fd);
107 return 0;
109 no_optarg_out:
110 printf("option requires an argument -- %c\n", opt);
111 return 1;
114 BAREBOX_CMD_START(echo)
115 .cmd = do_echo,
116 .usage = "echo args to console",
117 BAREBOX_CMD_END