1 /* echo.c, derived from code echo.c in Bash.
2 Copyright (C) 1987-2023 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://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 /* STATUS should always be EXIT_SUCCESS (unlike in most other
39 utilities which would call emit_try_help otherwise). */
40 assert (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
);
84 NOTE: printf(1) is a preferred alternative,\n\
85 which does not have issues outputting option-like strings.\n\
87 emit_ancillary_info (PROGRAM_NAME
);
91 /* Convert C from hexadecimal character to integer. */
93 hextobin (unsigned char c
)
97 default: return c
- '0';
98 case 'a': case 'A': return 10;
99 case 'b': case 'B': return 11;
100 case 'c': case 'C': return 12;
101 case 'd': case 'D': return 13;
102 case 'e': case 'E': return 14;
103 case 'f': case 'F': return 15;
107 /* Print the words in LIST to standard output. If the first word is
108 '-n', then don't print a trailing newline. We also support the
109 echo syntax from Version 9 unix systems. */
112 main (int argc
, char **argv
)
114 bool display_return
= true;
115 bool posixly_correct
= !!getenv ("POSIXLY_CORRECT");
118 || (! DEFAULT_ECHO_TO_XPG
&& 1 < argc
&& STREQ (argv
[1], "-n")));
120 /* System V machines already have a /bin/sh with a v9 behavior.
121 Use the identical behavior for these machines so that the
122 existing system shell scripts won't barf. */
123 bool do_v9
= DEFAULT_ECHO_TO_XPG
;
125 initialize_main (&argc
, &argv
);
126 set_program_name (argv
[0]);
127 setlocale (LC_ALL
, "");
128 bindtextdomain (PACKAGE
, LOCALEDIR
);
129 textdomain (PACKAGE
);
131 atexit (close_stdout
);
133 /* We directly parse options, rather than use parse_long_options, in
134 order to avoid accepting abbreviations. */
135 if (allow_options
&& argc
== 2)
137 if (STREQ (argv
[1], "--help"))
138 usage (EXIT_SUCCESS
);
140 if (STREQ (argv
[1], "--version"))
142 version_etc (stdout
, PROGRAM_NAME
, PACKAGE_NAME
, Version
, AUTHORS
,
152 while (argc
> 0 && *argv
[0] == '-')
154 char const *temp
= argv
[0] + 1;
157 /* If it appears that we are handling options, then make sure that
158 all of the options specified are actually valid. Otherwise, the
159 string should just be echoed. */
161 for (i
= 0; temp
[i
]; i
++)
164 case 'e': case 'E': case 'n':
173 /* All of the options in TEMP are valid options to ECHO.
187 display_return
= false;
197 if (do_v9
|| posixly_correct
)
201 char const *s
= argv
[0];
210 case 'a': c
= '\a'; break;
211 case 'b': c
= '\b'; break;
212 case 'c': return EXIT_SUCCESS
;
213 case 'e': c
= '\x1B'; break;
214 case 'f': c
= '\f'; break;
215 case 'n': c
= '\n'; break;
216 case 'r': c
= '\r'; break;
217 case 't': c
= '\t'; break;
218 case 'v': c
= '\v'; break;
221 unsigned char ch
= *s
;
230 c
= c
* 16 + hextobin (ch
);
236 if (! ('0' <= *s
&& *s
<= '7'))
240 case '1': case '2': case '3':
241 case '4': case '5': case '6': case '7':
243 if ('0' <= *s
&& *s
<= '7')
244 c
= c
* 8 + (*s
++ - '0');
245 if ('0' <= *s
&& *s
<= '7')
246 c
= c
* 8 + (*s
++ - '0');
251 default: putchar ('\\'); break;
266 fputs (argv
[0], stdout
);