1 /* echo.c, derived from code echo.c in Bash.
2 Copyright (C) 87,89, 1991-1997, 1999-2002 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <sys/types.h>
23 #include "long-options.h"
25 /* The official name of this program (e.g., no `g' prefix). */
26 #define PROGRAM_NAME "echo"
28 #define AUTHORS "FIXME unknown"
30 /* echo [-neE] [arg ...]
31 Output the ARGs. If -n is specified, the trailing newline is
32 suppressed. If the -e option is given, interpretation of the
33 following backslash-escaped characters is turned on:
36 \c suppress trailing newline
43 \num the character whose ASCII code is NUM (octal).
45 You can explicitly turn off the interpretation of the above characters
46 on System V systems with the -E option.
49 /* If defined, interpret backslash escapes if -e is given. */
52 /* If defined, interpret backslash escapes unless -E is given.
53 V9_ECHO must also be defined. */
54 /* #define V9_DEFAULT */
57 # if defined (V9_DEFAULT)
58 # define VALID_ECHO_OPTIONS "neE"
60 # define VALID_ECHO_OPTIONS "ne"
61 # endif /* !V9_DEFAULT */
63 # define VALID_ECHO_OPTIONS "n"
66 /* The name this program was run with. */
73 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
77 printf (_("Usage: %s [OPTION]... [STRING]...\n"), program_name
);
79 Echo the STRING(s) to standard output.\n\
81 -n do not output the trailing newline\n\
82 -e enable interpretation of the backslash-escaped characters\n\
84 -E disable interpretation of those sequences in STRINGs\n\
86 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
87 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
90 Without -E, the following sequences are recognized and interpolated:\n\
92 \\NNN the character whose ASCII code is NNN (octal)\n\
98 \\c suppress trailing newline\n\
101 \\r carriage return\n\
102 \\t horizontal tab\n\
105 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
110 /* Print the words in LIST to standard output. If the first word is
111 `-n', then don't print a trailing newline. We also support the
112 echo syntax from Version 9 unix systems. */
115 main (int argc
, char **argv
)
117 int display_return
= 1, do_v9
= 0;
118 int allow_options
= 1;
120 program_name
= argv
[0];
121 setlocale (LC_ALL
, "");
122 bindtextdomain (PACKAGE
, LOCALEDIR
);
123 textdomain (PACKAGE
);
125 atexit (close_stdout
);
127 /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
128 if (getenv ("POSIXLY_CORRECT") == NULL
)
129 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
134 /* System V machines already have a /bin/sh with a v9 behaviour. We
135 use the identical behaviour for these machines so that the
136 existing system shell scripts won't barf. */
137 #if defined (V9_ECHO) && defined (V9_DEFAULT)
138 do_v9
= allow_options
;
144 while (argc
> 0 && *argv
[0] == '-')
149 /* If it appears that we are handling options, then make sure that
150 all of the options specified are actually valid. Otherwise, the
151 string should just be echoed. */
154 for (i
= 0; temp
[i
]; i
++)
156 if (strrchr (VALID_ECHO_OPTIONS
, temp
[i
]) == 0)
163 /* All of the options in TEMP are valid options to ECHO.
167 if (allow_options
&& *temp
== 'n')
169 #if defined (V9_ECHO)
170 else if (allow_options
&& *temp
== 'e')
172 # if defined (V9_DEFAULT)
173 else if (allow_options
&& *temp
== 'E')
175 # endif /* V9_DEFAULT */
190 #if defined (V9_ECHO)
195 register char *s
= argv
[0];
204 case 'a': c
= '\007'; break;
205 case 'b': c
= '\b'; break;
206 case 'c': display_return
= 0; continue;
207 case 'f': c
= '\f'; break;
208 case 'n': c
= '\n'; break;
209 case 'r': c
= '\r'; break;
210 case 't': c
= '\t'; break;
211 case 'v': c
= (int) 0x0B; break;
212 case '0': case '1': case '2': case '3':
213 case '4': case '5': case '6': case '7':
215 if (*s
>= '0' && *s
<= '7')
216 c
= c
* 8 + (*s
++ - '0');
217 if (*s
>= '0' && *s
<= '7')
218 c
= c
* 8 + (*s
++ - '0');
221 default: putchar ('\\'); break;
237 fputs (argv
[0], stdout
);