1 /* echo.c, derived from code echo.c in Bash.
2 Copyright (C) 1987, 1989, 1991-1997, 1999-2005, 2007-2012 Free Software
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
23 /* The official name of this program (e.g., no `g' prefix). */
24 #define PROGRAM_NAME "echo"
27 proper_name ("Brian Fox"), \
28 proper_name ("Chet Ramey")
30 /* If true, interpret backslash escapes by default. */
31 #ifndef DEFAULT_ECHO_TO_XPG
32 enum { DEFAULT_ECHO_TO_XPG
= false };
38 if (status
!= EXIT_SUCCESS
)
39 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
44 Usage: %s [SHORT-OPTION]... [STRING]...\n\
46 "), program_name
, program_name
);
48 Echo the STRING(s) to standard output.\n\
50 -n do not output the trailing newline\n\
52 fputs (_(DEFAULT_ECHO_TO_XPG
54 -e enable interpretation of backslash escapes (default)\n\
55 -E disable interpretation of backslash escapes\n")
57 -e enable interpretation of backslash escapes\n\
58 -E disable interpretation of backslash escapes (default)\n")),
60 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
61 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
64 If -e is in effect, the following sequences are recognized:\n\
71 \\c produce no further output\n\
75 \\r carriage return\n\
80 \\0NNN byte with octal value NNN (1 to 3 digits)\n\
81 \\xHH byte with hexadecimal value HH (1 to 2 digits)\n\
83 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
84 emit_ancillary_info ();
89 /* Convert C from hexadecimal character to integer. */
91 hextobin (unsigned char c
)
95 default: return c
- '0';
96 case 'a': case 'A': return 10;
97 case 'b': case 'B': return 11;
98 case 'c': case 'C': return 12;
99 case 'd': case 'D': return 13;
100 case 'e': case 'E': return 14;
101 case 'f': case 'F': return 15;
105 /* Print the words in LIST to standard output. If the first word is
106 `-n', then don't print a trailing newline. We also support the
107 echo syntax from Version 9 unix systems. */
110 main (int argc
, char **argv
)
112 bool display_return
= true;
114 (! getenv ("POSIXLY_CORRECT")
115 || (! DEFAULT_ECHO_TO_XPG
&& 1 < argc
&& STREQ (argv
[1], "-n")));
117 /* System V machines already have a /bin/sh with a v9 behavior.
118 Use the identical behavior for these machines so that the
119 existing system shell scripts won't barf. */
120 bool do_v9
= DEFAULT_ECHO_TO_XPG
;
122 initialize_main (&argc
, &argv
);
123 set_program_name (argv
[0]);
124 setlocale (LC_ALL
, "");
125 bindtextdomain (PACKAGE
, LOCALEDIR
);
126 textdomain (PACKAGE
);
128 atexit (close_stdout
);
130 /* We directly parse options, rather than use parse_long_options, in
131 order to avoid accepting abbreviations. */
132 if (allow_options
&& argc
== 2)
134 if (STREQ (argv
[1], "--help"))
135 usage (EXIT_SUCCESS
);
137 if (STREQ (argv
[1], "--version"))
139 version_etc (stdout
, PROGRAM_NAME
, PACKAGE_NAME
, Version
, AUTHORS
,
149 while (argc
> 0 && *argv
[0] == '-')
151 char const *temp
= argv
[0] + 1;
154 /* If it appears that we are handling options, then make sure that
155 all of the options specified are actually valid. Otherwise, the
156 string should just be echoed. */
158 for (i
= 0; temp
[i
]; i
++)
161 case 'e': case 'E': case 'n':
170 /* All of the options in TEMP are valid options to ECHO.
184 display_return
= false;
198 char const *s
= argv
[0];
207 case 'a': c
= '\a'; break;
208 case 'b': c
= '\b'; break;
209 case 'c': exit (EXIT_SUCCESS
);
210 case 'e': c
= '\x1B'; break;
211 case 'f': c
= '\f'; break;
212 case 'n': c
= '\n'; break;
213 case 'r': c
= '\r'; break;
214 case 't': c
= '\t'; break;
215 case 'v': c
= '\v'; break;
218 unsigned char ch
= *s
;
227 c
= c
* 16 + hextobin (ch
);
233 if (! ('0' <= *s
&& *s
<= '7'))
237 case '1': case '2': case '3':
238 case '4': case '5': case '6': case '7':
240 if ('0' <= *s
&& *s
<= '7')
241 c
= c
* 8 + (*s
++ - '0');
242 if ('0' <= *s
&& *s
<= '7')
243 c
= c
* 8 + (*s
++ - '0');
248 default: putchar ('\\'); break;
263 fputs (argv
[0], stdout
);