1 /* echo.c, derived from code echo.c in Bash.
2 Copyright (C) 1987-2012 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 <http://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
22 /* The official name of this program (e.g., no 'g' prefix). */
23 #define PROGRAM_NAME "echo"
26 proper_name ("Brian Fox"), \
27 proper_name ("Chet Ramey")
29 /* If true, interpret backslash escapes by default. */
30 #ifndef DEFAULT_ECHO_TO_XPG
31 enum { DEFAULT_ECHO_TO_XPG
= false };
37 if (status
!= EXIT_SUCCESS
)
42 Usage: %s [SHORT-OPTION]... [STRING]...\n\
44 "), program_name
, program_name
);
46 Echo the STRING(s) to standard output.\n\
48 -n do not output the trailing newline\n\
50 fputs (_(DEFAULT_ECHO_TO_XPG
52 -e enable interpretation of backslash escapes (default)\n\
53 -E disable interpretation of backslash escapes\n")
55 -e enable interpretation of backslash escapes\n\
56 -E disable interpretation of backslash escapes (default)\n")),
58 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
59 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
62 If -e is in effect, the following sequences are recognized:\n\
69 \\c produce no further output\n\
73 \\r carriage return\n\
78 \\0NNN byte with octal value NNN (1 to 3 digits)\n\
79 \\xHH byte with hexadecimal value HH (1 to 2 digits)\n\
81 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
82 emit_ancillary_info ();
87 /* Convert C from hexadecimal character to integer. */
89 hextobin (unsigned char c
)
93 default: return c
- '0';
94 case 'a': case 'A': return 10;
95 case 'b': case 'B': return 11;
96 case 'c': case 'C': return 12;
97 case 'd': case 'D': return 13;
98 case 'e': case 'E': return 14;
99 case 'f': case 'F': return 15;
103 /* Print the words in LIST to standard output. If the first word is
104 '-n', then don't print a trailing newline. We also support the
105 echo syntax from Version 9 unix systems. */
108 main (int argc
, char **argv
)
110 bool display_return
= true;
112 (! getenv ("POSIXLY_CORRECT")
113 || (! DEFAULT_ECHO_TO_XPG
&& 1 < argc
&& STREQ (argv
[1], "-n")));
115 /* System V machines already have a /bin/sh with a v9 behavior.
116 Use the identical behavior for these machines so that the
117 existing system shell scripts won't barf. */
118 bool do_v9
= DEFAULT_ECHO_TO_XPG
;
120 initialize_main (&argc
, &argv
);
121 set_program_name (argv
[0]);
122 setlocale (LC_ALL
, "");
123 bindtextdomain (PACKAGE
, LOCALEDIR
);
124 textdomain (PACKAGE
);
126 atexit (close_stdout
);
128 /* We directly parse options, rather than use parse_long_options, in
129 order to avoid accepting abbreviations. */
130 if (allow_options
&& argc
== 2)
132 if (STREQ (argv
[1], "--help"))
133 usage (EXIT_SUCCESS
);
135 if (STREQ (argv
[1], "--version"))
137 version_etc (stdout
, PROGRAM_NAME
, PACKAGE_NAME
, Version
, AUTHORS
,
147 while (argc
> 0 && *argv
[0] == '-')
149 char const *temp
= argv
[0] + 1;
152 /* If it appears that we are handling options, then make sure that
153 all of the options specified are actually valid. Otherwise, the
154 string should just be echoed. */
156 for (i
= 0; temp
[i
]; i
++)
159 case 'e': case 'E': case 'n':
168 /* All of the options in TEMP are valid options to ECHO.
182 display_return
= false;
196 char const *s
= argv
[0];
205 case 'a': c
= '\a'; break;
206 case 'b': c
= '\b'; break;
207 case 'c': exit (EXIT_SUCCESS
);
208 case 'e': c
= '\x1B'; break;
209 case 'f': c
= '\f'; break;
210 case 'n': c
= '\n'; break;
211 case 'r': c
= '\r'; break;
212 case 't': c
= '\t'; break;
213 case 'v': c
= '\v'; break;
216 unsigned char ch
= *s
;
225 c
= c
* 16 + hextobin (ch
);
231 if (! ('0' <= *s
&& *s
<= '7'))
235 case '1': case '2': case '3':
236 case '4': case '5': case '6': case '7':
238 if ('0' <= *s
&& *s
<= '7')
239 c
= c
* 8 + (*s
++ - '0');
240 if ('0' <= *s
&& *s
<= '7')
241 c
= c
* 8 + (*s
++ - '0');
246 default: putchar ('\\'); break;
261 fputs (argv
[0], stdout
);