mxc_nand: remove unused defines
[barebox-mini2440.git] / commands / echo.c
blob44206ad555020d93e80c601f69b73110dd2d093e
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>
29 static int do_echo (cmd_tbl_t *cmdtp, int argc, char *argv[])
31 int i, optind = 1;
32 int fd = stdout, opt, newline = 1;
33 char *file = NULL;
34 int oflags = O_WRONLY | O_CREAT;
36 /* We can't use getopt() here because we want to
37 * echo all things we don't understand.
39 while (optind < argc && *argv[optind] == '-') {
40 if (!*(argv[optind] + 1) || *(argv[optind] + 2))
41 break;
43 opt = *(argv[optind] + 1);
44 switch (opt) {
45 case 'n':
46 newline = 0;
47 break;
48 case 'a':
49 oflags |= O_APPEND;
50 if (optind + 1 < argc)
51 file = argv[optind + 1];
52 else
53 goto no_optarg_out;
54 optind++;
55 break;
56 case 'o':
57 oflags |= O_TRUNC;
58 file = argv[optind + 1];
59 if (optind + 1 < argc)
60 file = argv[optind + 1];
61 else
62 goto no_optarg_out;
63 optind++;
64 break;
65 default:
66 goto exit_parse;
68 optind++;
71 exit_parse:
72 if (file) {
73 fd = open(file, oflags);
74 if (fd < 0) {
75 perror("open");
76 return 1;
80 for (i = optind; i < argc; i++) {
81 if (i > optind)
82 fputc(fd, ' ');
83 fputs(fd, argv[i]);
86 if (newline)
87 fputc(fd, '\n');
89 if (file)
90 close(fd);
92 return 0;
94 no_optarg_out:
95 printf("option requires an argument -- %c\n", opt);
96 return 1;
99 U_BOOT_CMD_START(echo)
100 .cmd = do_echo,
101 .usage = "echo args to console",
102 U_BOOT_CMD_END