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
)
43 Usage: %s [SHORT-OPTION]... [STRING]...\n\
45 "), program_name
, program_name
);
47 Echo the STRING(s) to standard output.\n\
49 -n do not output the trailing newline\n\
51 fputs (_(DEFAULT_ECHO_TO_XPG
53 -e enable interpretation of backslash escapes (default)\n\
54 -E disable interpretation of backslash escapes\n")
56 -e enable interpretation of backslash escapes\n\
57 -E disable interpretation of backslash escapes (default)\n")),
59 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
60 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
63 If -e is in effect, the following sequences are recognized:\n\
70 \\c produce no further output\n\
74 \\r carriage return\n\
79 \\0NNN byte with octal value NNN (1 to 3 digits)\n\
80 \\xHH byte with hexadecimal value HH (1 to 2 digits)\n\
82 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
83 emit_ancillary_info ();
88 /* Convert C from hexadecimal character to integer. */
90 hextobin (unsigned char c
)
94 default: return c
- '0';
95 case 'a': case 'A': return 10;
96 case 'b': case 'B': return 11;
97 case 'c': case 'C': return 12;
98 case 'd': case 'D': return 13;
99 case 'e': case 'E': return 14;
100 case 'f': case 'F': return 15;
104 /* Print the words in LIST to standard output. If the first word is
105 `-n', then don't print a trailing newline. We also support the
106 echo syntax from Version 9 unix systems. */
109 main (int argc
, char **argv
)
111 bool display_return
= true;
113 (! getenv ("POSIXLY_CORRECT")
114 || (! DEFAULT_ECHO_TO_XPG
&& 1 < argc
&& STREQ (argv
[1], "-n")));
116 /* System V machines already have a /bin/sh with a v9 behavior.
117 Use the identical behavior for these machines so that the
118 existing system shell scripts won't barf. */
119 bool do_v9
= DEFAULT_ECHO_TO_XPG
;
121 initialize_main (&argc
, &argv
);
122 set_program_name (argv
[0]);
123 setlocale (LC_ALL
, "");
124 bindtextdomain (PACKAGE
, LOCALEDIR
);
125 textdomain (PACKAGE
);
127 atexit (close_stdout
);
129 /* We directly parse options, rather than use parse_long_options, in
130 order to avoid accepting abbreviations. */
131 if (allow_options
&& argc
== 2)
133 if (STREQ (argv
[1], "--help"))
134 usage (EXIT_SUCCESS
);
136 if (STREQ (argv
[1], "--version"))
138 version_etc (stdout
, PROGRAM_NAME
, PACKAGE_NAME
, Version
, AUTHORS
,
148 while (argc
> 0 && *argv
[0] == '-')
150 char const *temp
= argv
[0] + 1;
153 /* If it appears that we are handling options, then make sure that
154 all of the options specified are actually valid. Otherwise, the
155 string should just be echoed. */
157 for (i
= 0; temp
[i
]; i
++)
160 case 'e': case 'E': case 'n':
169 /* All of the options in TEMP are valid options to ECHO.
183 display_return
= false;
197 char const *s
= argv
[0];
206 case 'a': c
= '\a'; break;
207 case 'b': c
= '\b'; break;
208 case 'c': exit (EXIT_SUCCESS
);
209 case 'e': c
= '\x1B'; break;
210 case 'f': c
= '\f'; break;
211 case 'n': c
= '\n'; break;
212 case 'r': c
= '\r'; break;
213 case 't': c
= '\t'; break;
214 case 'v': c
= '\v'; break;
217 unsigned char ch
= *s
;
226 c
= c
* 16 + hextobin (ch
);
232 if (! ('0' <= *s
&& *s
<= '7'))
236 case '1': case '2': case '3':
237 case '4': case '5': case '6': case '7':
239 if ('0' <= *s
&& *s
<= '7')
240 c
= c
* 8 + (*s
++ - '0');
241 if ('0' <= *s
&& *s
<= '7')
242 c
= c
* 8 + (*s
++ - '0');
247 default: putchar ('\\'); break;
262 fputs (argv
[0], stdout
);