1*e177ba53Skre /* $NetBSD: echo.c,v 1.15 2021/05/18 21:39:06 kre Exp $ */
249f0ad86Scgd
361f28255Scgd /*-
405a9db8eSjtc * Copyright (c) 1991, 1993
505a9db8eSjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * This code is derived from software contributed to Berkeley by
861f28255Scgd * Kenneth Almquist.
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
18b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3305a9db8eSjtc *
3449f0ad86Scgd * @(#)echo.c 8.1 (Berkeley) 5/31/93
3561f28255Scgd */
3661f28255Scgd
3761f28255Scgd /*
3861f28255Scgd * Echo command.
39c02b3bbdSchristos *
40c02b3bbdSchristos * echo is steeped in tradition - several of them!
41c02b3bbdSchristos * netbsd has supported 'echo [-n | -e] args' in spite of -e not being
42c02b3bbdSchristos * documented anywhere.
43c02b3bbdSchristos * Posix requires that -n be supported, output from strings containing
44c02b3bbdSchristos * \ is implementation defined
45c02b3bbdSchristos * The Single Unix Spec requires that \ escapes be treated as if -e
46c02b3bbdSchristos * were set, but that -n not be treated as an option.
47c02b3bbdSchristos * (ksh supports 'echo [-eEn] args', but not -- so that it is actually
48617b132aSwiz * impossible to actually output '-n')
49c02b3bbdSchristos *
50c02b3bbdSchristos * It is suggested that 'printf "%b" "string"' be used to get \ sequences
51c02b3bbdSchristos * expanded. printf is now a builtin of netbsd's sh and csh.
5261f28255Scgd */
5361f28255Scgd
54bcf893f4Slukem #include <sys/cdefs.h>
55*e177ba53Skre __RCSID("$NetBSD: echo.c,v 1.15 2021/05/18 21:39:06 kre Exp $");
56bcf893f4Slukem
5761f28255Scgd #define main echocmd
5861f28255Scgd
5961f28255Scgd #include "bltin.h"
6061f28255Scgd
61846dce0eSchristos int
main(int argc,char ** argv)62c02b3bbdSchristos main(int argc, char **argv)
63c02b3bbdSchristos {
643fa24d78Sperry char **ap;
653fa24d78Sperry char *p;
663fa24d78Sperry char c;
6761f28255Scgd int count;
6861f28255Scgd int nflag = 0;
6961f28255Scgd int eflag = 0;
7061f28255Scgd
71*e177ba53Skre clearerr(stdout);
72*e177ba53Skre
7361f28255Scgd ap = argv;
7461f28255Scgd if (argc)
7561f28255Scgd ap++;
76c02b3bbdSchristos
7761f28255Scgd if ((p = *ap) != NULL) {
7861f28255Scgd if (equal(p, "-n")) {
79c02b3bbdSchristos nflag = 1;
8061f28255Scgd ap++;
8161f28255Scgd } else if (equal(p, "-e")) {
82c02b3bbdSchristos eflag = 1;
8361f28255Scgd ap++;
8461f28255Scgd }
8561f28255Scgd }
86c02b3bbdSchristos
8761f28255Scgd while ((p = *ap++) != NULL) {
8861f28255Scgd while ((c = *p++) != '\0') {
8961f28255Scgd if (c == '\\' && eflag) {
9061f28255Scgd switch (*p++) {
91c02b3bbdSchristos case 'a': c = '\a'; break; /* bell */
9261f28255Scgd case 'b': c = '\b'; break;
9361f28255Scgd case 'c': return 0; /* exit */
94c02b3bbdSchristos case 'e': c = 033; break; /* escape */
9561f28255Scgd case 'f': c = '\f'; break;
9661f28255Scgd case 'n': c = '\n'; break;
9761f28255Scgd case 'r': c = '\r'; break;
9861f28255Scgd case 't': c = '\t'; break;
9961f28255Scgd case 'v': c = '\v'; break;
10061f28255Scgd case '\\': break; /* c = '\\' */
10161f28255Scgd case '0':
10261f28255Scgd c = 0;
10361f28255Scgd count = 3;
10461f28255Scgd while (--count >= 0 && (unsigned)(*p - '0') < 8)
10561f28255Scgd c = (c << 3) + (*p++ - '0');
10661f28255Scgd break;
10761f28255Scgd default:
108c02b3bbdSchristos /* Output the '/' and char following */
10961f28255Scgd p--;
11061f28255Scgd break;
11161f28255Scgd }
11261f28255Scgd }
11361f28255Scgd putchar(c);
11461f28255Scgd }
11561f28255Scgd if (*ap)
11661f28255Scgd putchar(' ');
11761f28255Scgd }
11861f28255Scgd if (! nflag)
11961f28255Scgd putchar('\n');
12030a14162Sdholland fflush(stdout);
121*e177ba53Skre if (ferror(stdout)) {
122*e177ba53Skre clearerr(stdout);
123*e177ba53Skre err(1, "write error");
124*e177ba53Skre }
12561f28255Scgd return 0;
12661f28255Scgd }
127