1 /* echo.c, derived from code echo.c in Bash.
2 Copyright (C) 87,89, 1991-1997, 1999-2005, 2007-2009 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\
66 \\0NNN the character whose ASCII code is NNN (octal)\n\
72 \\c produce no further output\n\
75 \\r carriage return\n\
79 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
80 emit_bug_reporting_address ();
85 /* Convert C from hexadecimal character to integer. */
87 hextobin (unsigned char c
)
91 default: return c
- '0';
92 case 'a': case 'A': return 10;
93 case 'b': case 'B': return 11;
94 case 'c': case 'C': return 12;
95 case 'd': case 'D': return 13;
96 case 'e': case 'E': return 14;
97 case 'f': case 'F': return 15;
101 /* Print the words in LIST to standard output. If the first word is
102 `-n', then don't print a trailing newline. We also support the
103 echo syntax from Version 9 unix systems. */
106 main (int argc
, char **argv
)
108 bool display_return
= true;
110 (! getenv ("POSIXLY_CORRECT")
111 || (! DEFAULT_ECHO_TO_XPG
&& 1 < argc
&& STREQ (argv
[1], "-n")));
113 /* System V machines already have a /bin/sh with a v9 behavior.
114 Use the identical behavior for these machines so that the
115 existing system shell scripts won't barf. */
116 bool do_v9
= DEFAULT_ECHO_TO_XPG
;
118 initialize_main (&argc
, &argv
);
119 set_program_name (argv
[0]);
120 setlocale (LC_ALL
, "");
121 bindtextdomain (PACKAGE
, LOCALEDIR
);
122 textdomain (PACKAGE
);
124 atexit (close_stdout
);
126 /* We directly parse options, rather than use parse_long_options, in
127 order to avoid accepting abbreviations. */
128 if (allow_options
&& argc
== 2)
130 if (STREQ (argv
[1], "--help"))
131 usage (EXIT_SUCCESS
);
133 if (STREQ (argv
[1], "--version"))
135 version_etc (stdout
, PROGRAM_NAME
, PACKAGE_NAME
, Version
, AUTHORS
,
145 while (argc
> 0 && *argv
[0] == '-')
147 char const *temp
= argv
[0] + 1;
150 /* If it appears that we are handling options, then make sure that
151 all of the options specified are actually valid. Otherwise, the
152 string should just be echoed. */
154 for (i
= 0; temp
[i
]; i
++)
157 case 'e': case 'E': case 'n':
166 /* All of the options in TEMP are valid options to ECHO.
180 display_return
= false;
194 char const *s
= argv
[0];
203 case 'a': c
= '\a'; break;
204 case 'b': c
= '\b'; break;
205 case 'c': exit (EXIT_SUCCESS
);
206 case 'f': c
= '\f'; break;
207 case 'n': c
= '\n'; break;
208 case 'r': c
= '\r'; break;
209 case 't': c
= '\t'; break;
210 case 'v': c
= '\v'; break;
213 unsigned char ch
= *s
;
222 c
= c
* 16 + hextobin (ch
);
228 if (! ('0' <= *s
&& *s
<= '7'))
232 case '1': case '2': case '3':
233 case '4': case '5': case '6': case '7':
235 if ('0' <= *s
&& *s
<= '7')
236 c
= c
* 8 + (*s
++ - '0');
237 if ('0' <= *s
&& *s
<= '7')
238 c
= c
* 8 + (*s
++ - '0');
243 default: putchar ('\\'); break;
258 fputs (argv
[0], stdout
);